# wirenboard/wb-mqtt-iec104 architecture

> Architecture diagram of the GitHub repository wirenboard/wb-mqtt-iec104, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wirenboard/wb-mqtt-iec104
- Repository: https://github.com/wirenboard/wb-mqtt-iec104

## Overview

##### Main Components
- Gateway Core (Main Process): This is the heart of the application (src/main.cpp). It initializes the application, reads the configuration file (wb-mqtt-iec104.conf) and ties all the key modules together.
- Configuration Parser: Modules such as src/config_parser.cpp/h parse and validate the configuration with details like IEC104 settings, MQTT broker settings, and groups of channels. This component is responsible for auto-generating the /etc/wb-mqtt-iec104.conf on startup if needed.
- IEC 60870-5-104 Server: Implemented in src/IEC104Server.cpp/h, it listens on the specified host and port to accept incoming IEC 104 TCP/IP connections and handles the ASDU packets.
- MQTT Client: Although not in a single source file, the gateway (src/gateway.cpp/h) establishes a connection to the MQTT broker, subscribes to topics, and manages the message exchange between MQTT and IEC104.
- Logger: Modules (src/log.cpp/h) provide logging capabilities, which are essential for debugging and monitoring both the MQTT and IEC104 message flows.
- Exception Handling: Files like src/iec104_exception.cpp/h capture protocol or runtime errors ensuring system robustness.
- Build/Integration and Testing: The presence of a Jenkinsfile, Makefile, Debian packaging (debian/ folder) and test suite (test/ directory) indicates an established CI/CD pipeline and rigorous testing, even though these aren’t drawn as runtime components, they are part of the overall ecosystem.

##### Relationships and Data Flow
- The Gateway Core uses the Configuration Parser to load the settings (including MQTT broker details and IEC104 parameters) at startup.
- The MQTT Client (part of the gateway component) establishes a connection with the external MQTT broker, subscribes to the configured topics, and sends/receives messages.
- The IEC104 Server accepts incoming TCP/IP connections from external IEC104-compliant systems, acting as the controlled station.
- Trade of Data:
- Outbound: The gateway captures MQTT messages (from configured topics) and converts them into IEC104 ASDUs, sending them over the TCP/IP connection.
- Inbound: For commands (and potentially general interrogation messages), the IEC104 Server receives the ASDU, checks for a matching enabled channel in the configuration, and translates it into MQTT messages.
- The Logger captures significant events across both message flows, aiding in debugging and operational monitoring.

##### Architectural Patterns and Design Principles
- Modular Design: Each core functionality (config parsing, MQTT handling, and IEC104 communication) is separated into its own module to ensure maintainability.
- Decoupled Communication: The design clearly differentiates the incoming data streams (MQTT and IEC104) while having a central dispatcher (gateway) that handles message translation.
- Configuration-Driven Behavior: The gateway’s operation is heavily influenced by the configuration file, allowing dynamic control over which channels are enabled and how messages are routed.
- Robust Error Handling: Use of dedicated exception modules shows an emphasis on robust error management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Internal Gateway Service
    subgraph "Gateway Service"
        CP["Configuration Parser"]:::internal
        GC["Gateway Core (Main Process)"]:::internal
        LE["Logger & Exception Handler"]:::internal
        MC["MQTT Client"]:::internal
        IS["IEC104 Server"]:::internal
    end

    %% External Systems
    MB["External MQTT Broker"]:::external
    IE["External IEC104 Devices/Stations"]:::external

    %% CI/CD & Build/Testing
    subgraph "Build & Testing"
        jn["Jenkinsfile"]:::ci
        mk["Makefile"]:::ci
        deb["Debian Packaging"]:::ci
        tst["Test Suite"]:::ci
    end

    %% Data Flow Connections
    CP -->|"ConfigurationData"| GC
    GC -->|"InitMQTT"| MC
    GC -->|"InitIEC104"| IS
    GC -->|"Logs/Error"| LE
    MC -->|"MQTTMessage"| MB
    IS -->|"IEC104ASDU"| IE

    %% Click Events for Gateway Service Components
    click GC "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/main.cpp"
    click CP "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/config_parser.cpp"
    click CP "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/config_parser.h"
    click IS "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/IEC104Server.cpp"
    click IS "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/IEC104Server.h"
    click MC "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/gateway.cpp"
    click MC "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/gateway.h"
    click LE "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/log.cpp"
    click LE "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/log.h"
    click LE "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/iec104_exception.cpp"
    click LE "https://github.com/wirenboard/wb-mqtt-iec104/blob/main/src/iec104_exception.h"

    %% Click Events for CI/CD Components
    click jn "https://github.com/wirenboard/wb-mqtt-iec104/tree/main/Jenkinsfile"
    click mk "https://github.com/wirenboard/wb-mqtt-iec104/tree/main/Makefile"
    click deb "https://github.com/wirenboard/wb-mqtt-iec104/tree/main/debian/"
    click tst "https://github.com/wirenboard/wb-mqtt-iec104/tree/main/test/"

    %% Styling Classes
    classDef internal fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#B9770E,stroke-dasharray: 5, 5,stroke-width:2px;
    classDef ci fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
```
