# soric91/gatewayems architecture

> Architecture diagram of the GitHub repository soric91/gatewayems, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/soric91/gatewayems
- Repository: https://github.com/soric91/gatewayems

## Overview

1. **System purpose and boundary**
   GatewayEMS is a Python 3.12 asyncio service that sits between industrial **Modbus RTU/TCP devices** and **InfluxDB 2.x**. Its job is to poll devices, normalize register values, and persist timestamped measurements for monitoring and analytics.

2. **Entry point and orchestration**
   `main.py` is the runtime entry point. The `Task` layer (`src/Task/task.py`) appears to own process startup and lifecycle: it wires configuration, starts the Modbus acquisition loop, starts the database consumer, and coordinates shutdown/reconnect behavior.

3. **Configuration subsystem**
   Configuration is split by concern:
   - `.env` for InfluxDB credentials and runtime secrets
   - `src/Config/config.ini` for device definitions, polling intervals, and protocol settings
   - `src/Modbus/maps/*.json` for per-device register maps  
   The `src/Config` and `src/Core/config.py` modules likely centralize parsing, validation, and distribution of these settings to the rest of the app.

4. **Hot-reload / watchdog boundary**
   `src/Core/watchdog.py` is a separate cross-cutting subsystem that watches `config.ini` and triggers configuration reloads without restarting the process. This is an important architectural boundary because it lets device definitions and polling parameters change at runtime.

5. **Modbus acquisition layer**
   `src/Modbus` contains the device-side integration:
   - `client.py` creates RTU or TCP clients
   - `modbusmap.py` and `maps/*.json` define what registers to read and how to interpret them
   - `read.py` and `util.py` handle register reads, block grouping, and value decoding
   - `app.py` acts as the Modbus-facing application orchestrator  
   This layer is responsible for protocol communication and raw data extraction, not storage.

6. **Polling strategy and optimization**
   The read path supports **block reading**, which groups contiguous registers into a single Modbus call when enabled. That optimization changes the data acquisition behavior but stays entirely inside the Modbus subsystem; it reduces bus traffic and improves throughput for compatible devices.

7. **Producer-consumer data flow**
   The repo is explicitly structured around a **producer-consumer queue**. The Modbus side produces measurement objects, and the database side consumes them in batches. This decouples device polling from database writes so slow I/O in either direction does not block the other.

8. **Domain/data model**
   `src/Models/model.py` defines the internal measurement shape used between layers. This is the normalization boundary where raw Modbus register values become typed application data, likely with validation via `pydantic` as described in the README.

9. **Persistence layer**
   `src/Database` is split into:
   - `connection.py` for constructing the InfluxDB client
   - `repository.py` for translating application models into InfluxDB write/query operations
   - `service.py` for the consumer loop and batching logic  
   The repository boundary keeps InfluxDB-specific details isolated from the rest of the application.

10. **Operational and infrastructure concerns**
    The app is designed to run either locally or via Docker Compose. `docker-compose.yml` provisions InfluxDB and optionally the gateway container itself. External integration points that materially shape the architecture are:
    - serial/ethernet Modbus endpoints
    - InfluxDB HTTP API
    - environment/config files mounted into the runtime

11. **Shared utilities**
    `src/Utils` provides supporting concerns like logging and helper functions. These are cross-cutting, not business logic, and are used to keep the Modbus, config, and database layers focused on their own responsibilities.

12. **Overall data path**
    The end-to-end flow is: configuration load → Modbus client setup → scheduled polling/read-block assembly → normalization into model objects → async queue handoff → batched InfluxDB writes → optional config reload via watchdog. That is the core architecture an engineer should reflect in a diagram.

Main entry points: [`src/Task/task.py`](https://github.com/soric91/gatewayems/blob/main/src/Task/task.py) (Task), [`src/Core/config.py`](https://github.com/soric91/gatewayems/blob/main/src/Core/config.py) (Core config), [`src/Modbus/app.py`](https://github.com/soric91/gatewayems/blob/main/src/Modbus/app.py) (Modbus app), [`src/Modbus/read.py`](https://github.com/soric91/gatewayems/blob/main/src/Modbus/read.py) (Read path)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main_py(("main.py<br/>entrypoint<br/>[main.py]"))
  node_task_layer["Task<br/>orchestrator<br/>[task.py]"]
  node_logging_utils["Logging<br/>shared utils<br/>[logging.py]"]
  node_utils["Utils<br/>helpers<br/>[utils.py]"]
end

subgraph group_config["Configuration"]
  node_core_config["Core config<br/>config hub<br/>[config.py]"]
  node_config_ini["config.ini<br/>device config<br/>[config.ini]"]
  node_device_map["Modbus map<br/>register map"]
  node_watchdog["Watchdog<br/>reload monitor<br/>[watchdog.py]"]
end

subgraph group_acquisition["Acquisition"]
  node_modbus_app["Modbus app<br/>poller<br/>[app.py]"]
  node_modbus_client["Client<br/>[client.py]"]
  node_modbus_read["Read path<br/>reader<br/>[read.py]"]
  node_modbus_map["Map logic<br/>decoder<br/>[modbusmap.py]"]
  node_modbus_util["Modbus util<br/>helpers<br/>[util.py]"]
  node_measurement_model["Measurement<br/>domain model<br/>[model.py]"]
end

subgraph group_persistence["Persistence"]
  node_db_service["DB service<br/>consumer<br/>[service.py]"]
  node_db_repo["Repository<br/>translator<br/>[repository.py]"]
  node_db_conn["Connection<br/>influx client<br/>[connection.py]"]
end

subgraph group_infra["External"]
  node_modbus_device(("Modbus devices<br/>external system"))
  node_influxdb[("InfluxDB<br/>external system")]
  node_env["Env vars<br/>runtime config<br/>[.env.example]"]
  node_docker_compose["Compose<br/>deployment<br/>[docker-compose.yml]"]
end

node_main_py -->|"starts"| node_task_layer
node_task_layer -->|"loads"| node_core_config
node_task_layer -->|"starts poller"| node_modbus_app
node_task_layer -->|"starts consumer"| node_db_service
node_task_layer -->|"enables reload"| node_watchdog
node_watchdog -->|"watches"| node_config_ini
node_watchdog -.->|"reloads"| node_core_config
node_core_config -->|"parses"| node_config_ini
node_core_config -->|"binds maps"| node_device_map
node_modbus_app -->|"uses"| node_modbus_client
node_modbus_app -->|"orchestrates"| node_modbus_read
node_modbus_read -->|"decodes via"| node_modbus_map
node_modbus_read -->|"helpers"| node_modbus_util
node_modbus_read -->|"polls"| node_modbus_device
node_modbus_map -->|"loads"| node_device_map
node_modbus_app -->|"emits"| node_measurement_model
node_measurement_model -->|"queues"| node_db_service
node_db_service -->|"batches to"| node_db_repo
node_db_repo -->|"uses"| node_db_conn
node_db_conn -->|"connects to"| node_influxdb
node_db_repo -->|"maps"| node_measurement_model
node_core_config -->|"reads"| node_env
node_docker_compose -.->|"provisions"| node_influxdb
node_docker_compose -.->|"mounts"| node_env
node_logging_utils -->|"used by"| node_task_layer
node_logging_utils -->|"used by"| node_modbus_app
node_logging_utils -->|"used by"| node_db_service
node_utils -->|"supports"| node_modbus_util

click node_main_py "https://github.com/soric91/gatewayems/blob/main/main.py"
click node_task_layer "https://github.com/soric91/gatewayems/blob/main/src/Task/task.py"
click node_core_config "https://github.com/soric91/gatewayems/blob/main/src/Core/config.py"
click node_config_ini "https://github.com/soric91/gatewayems/blob/main/src/Config/config.ini"
click node_device_map "https://github.com/soric91/gatewayems/blob/main/src/Modbus/maps/Modbus_DTSU666.json"
click node_watchdog "https://github.com/soric91/gatewayems/blob/main/src/Core/watchdog.py"
click node_modbus_app "https://github.com/soric91/gatewayems/blob/main/src/Modbus/app.py"
click node_modbus_client "https://github.com/soric91/gatewayems/blob/main/src/Modbus/client.py"
click node_modbus_read "https://github.com/soric91/gatewayems/blob/main/src/Modbus/read.py"
click node_modbus_map "https://github.com/soric91/gatewayems/blob/main/src/Modbus/modbusmap.py"
click node_modbus_util "https://github.com/soric91/gatewayems/blob/main/src/Modbus/util.py"
click node_measurement_model "https://github.com/soric91/gatewayems/blob/main/src/Models/model.py"
click node_db_service "https://github.com/soric91/gatewayems/blob/main/src/Database/service.py"
click node_db_repo "https://github.com/soric91/gatewayems/blob/main/src/Database/repository.py"
click node_db_conn "https://github.com/soric91/gatewayems/blob/main/src/Database/connection.py"
click node_logging_utils "https://github.com/soric91/gatewayems/blob/main/src/Utils/logging.py"
click node_utils "https://github.com/soric91/gatewayems/blob/main/src/Utils/utils.py"
click node_env "https://github.com/soric91/gatewayems/blob/main/.env.example"
click node_docker_compose "https://github.com/soric91/gatewayems/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_py,node_task_layer,node_logging_utils,node_utils toneBlue
class node_core_config,node_config_ini,node_device_map,node_watchdog toneAmber
class node_modbus_app,node_modbus_client,node_modbus_read,node_modbus_map,node_modbus_util,node_measurement_model toneMint
class node_db_service,node_db_repo,node_db_conn toneRose
class node_modbus_device,node_influxdb,node_env,node_docker_compose toneIndigo
```

## Components

### Runtime

Process entrypoint and orchestration

- [main.py](https://github.com/soric91/gatewayems/blob/main/main.py): entrypoint · Starts the service process · `main.py`
- [Task](https://github.com/soric91/gatewayems/blob/main/src/Task/task.py): orchestrator · Coordinates startup, loops, and shutdown · `src/Task/task.py`
- [Logging](https://github.com/soric91/gatewayems/blob/main/src/Utils/logging.py): shared utils · Shared logging setup and helpers · `src/Utils/logging.py`
- [Utils](https://github.com/soric91/gatewayems/blob/main/src/Utils/utils.py): helpers · Cross-cutting helper functions · `src/Utils/utils.py`

### Configuration

Static config, reload, and device maps

- [Core config](https://github.com/soric91/gatewayems/blob/main/src/Core/config.py): config hub · Central config parsing and distribution · `src/Core/config.py`
- [config.ini](https://github.com/soric91/gatewayems/blob/main/src/Config/config.ini): device config · Polling, protocol, and device settings · `src/Config/config.ini`
- [Modbus map](https://github.com/soric91/gatewayems/blob/main/src/Modbus/maps/Modbus_DTSU666.json): register map · Per-device register definitions · `src/Modbus/maps/Modbus_DTSU666.json`
- [Watchdog](https://github.com/soric91/gatewayems/blob/main/src/Core/watchdog.py): reload monitor · Detects config changes and reloads · `src/Core/watchdog.py`

### Acquisition

Modbus polling and normalization

- [Modbus app](https://github.com/soric91/gatewayems/blob/main/src/Modbus/app.py): poller · Runs the device acquisition workflow · `src/Modbus/app.py`
- [Client](https://github.com/soric91/gatewayems/blob/main/src/Modbus/client.py): rtu/tcp client · Builds Modbus transport connections · `src/Modbus/client.py`
- [Read path](https://github.com/soric91/gatewayems/blob/main/src/Modbus/read.py): reader · Groups reads and fetches registers · `src/Modbus/read.py`
- [Map logic](https://github.com/soric91/gatewayems/blob/main/src/Modbus/modbusmap.py): decoder · Interprets registers and block layout · `src/Modbus/modbusmap.py`
- [Modbus util](https://github.com/soric91/gatewayems/blob/main/src/Modbus/util.py): helpers · Read-side helpers and decoding support · `src/Modbus/util.py`
- [Measurement](https://github.com/soric91/gatewayems/blob/main/src/Models/model.py): domain model · Normalized data passed between layers · `src/Models/model.py`

### Persistence

InfluxDB client, repository, and batching

- [DB service](https://github.com/soric91/gatewayems/blob/main/src/Database/service.py): consumer · Consumes queued measurements and batches writes · `src/Database/service.py`
- [Repository](https://github.com/soric91/gatewayems/blob/main/src/Database/repository.py): translator · Maps models to InfluxDB operations · `src/Database/repository.py`
- [Connection](https://github.com/soric91/gatewayems/blob/main/src/Database/connection.py): influx client · Constructs the InfluxDB client · `src/Database/connection.py`

### External

Runtime dependencies and integration points

- **Modbus devices**: external system · RTU/TCP industrial endpoints
- **InfluxDB**: external system · Time-series database backend
- [Env vars](https://github.com/soric91/gatewayems/blob/main/.env.example): runtime config · Secrets and deployment settings · `.env.example`
- [Compose](https://github.com/soric91/gatewayems/blob/main/docker-compose.yml): deployment · Local runtime and dependency wiring · `docker-compose.yml`

## Connections

- main.py → Task: starts · Entry point hands control to the task orchestrator
- Task → Core config: loads · Initializes shared configuration
- Task → Modbus app: starts poller · Launches Modbus acquisition loop
- Task → DB service: starts consumer · Launches database batching loop
- Task → Watchdog: enables reload · Keeps configuration hot-reload active
- Watchdog → config.ini: watches · Monitors config file changes
- Watchdog → Core config: reloads · Triggers config refresh without restart
- Core config → config.ini: parses · Reads device and polling settings
- Core config → Modbus map: binds maps · Associates devices with register layouts
- Modbus app → Client: uses · Creates RTU/TCP transport
- Modbus app → Read path: orchestrates · Coordinates polling and read execution
- Read path → Map logic: decodes via · Uses mapping rules and block structure
- Read path → Modbus util: helpers · Uses shared read/decoding helpers
- Read path → Modbus devices: polls · Reads registers from industrial endpoints
- Map logic → Modbus map: loads · Consumes per-device JSON register maps
- Modbus app → Measurement: emits · Normalizes raw values into domain objects
- Measurement → DB service: queues · Measurement objects cross the producer-consumer boundary
- DB service → Repository: batches to · Turns queued items into write batches
- Repository → Connection: uses · Writes through the InfluxDB connection
- Connection → InfluxDB: connects to · External time-series backend
- Repository → Measurement: maps · Translates domain models into persistence format
- Core config → Env vars: reads · Consumes runtime secrets and credentials
- Compose → InfluxDB: provisions · Defines the database service for local runs
- Compose → Env vars: mounts · Supplies deployment configuration
- Logging → Task: used by · Shared logging across orchestration
- Logging → Modbus app: used by · Shared logging across acquisition
- Logging → DB service: used by · Shared logging across persistence
- Utils → Modbus util: supports · Common helper functions used in the Modbus layer
