How soric91/gatewayems works
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: task.py Task · config.py Core config · app.py Modbus app · read.py Read path
Components 21
Runtime
Process entrypoint and orchestration
Configuration
Static config, reload, and device maps
- Core config · Central config parsing and distribution
src/Core/config.py - config.ini · Polling, protocol, and device settings
src/Config/config.ini - Modbus map · Per-device register definitions
src/Modbus/maps/Modbus_DTSU666.json - Watchdog · Detects config changes and reloads
src/Core/watchdog.py
Acquisition
Modbus polling and normalization
- Modbus app · Runs the device acquisition workflow
src/Modbus/app.py - Client · Builds Modbus transport connections
src/Modbus/client.py - Read path · Groups reads and fetches registers
src/Modbus/read.py - Map logic · Interprets registers and block layout
src/Modbus/modbusmap.py - Modbus util · Read-side helpers and decoding support
src/Modbus/util.py - Measurement · Normalized data passed between layers
src/Models/model.py
Persistence
InfluxDB client, repository, and batching
- DB service · Consumes queued measurements and batches writes
src/Database/service.py - Repository · Maps models to InfluxDB operations
src/Database/repository.py - Connection · Constructs the InfluxDB client
src/Database/connection.py
Connections 28
- 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