# kimhamyong/eams-gateway architecture

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

- Interactive diagram: https://gitdiagram.com/kimhamyong/eams-gateway
- Repository: https://github.com/kimhamyong/eams-gateway

## Overview

##### Main Components
- End Node (external input from sensors via ZigBee)
- Home Gateway (the core of this project, which you are diagramming)
- ZigBee Receiver (located in zigbee/receiver.py): This component handles serial communication with the ZigBee USB module.
- ZigBee Parser (located in zigbee/parser.py): Processes and validates incoming ZigBee API frames.
- MQTT Publisher (inside mqtt/publisher.py): Converts parsed data into JSON format and publishes it.
- Configuration (in config/settings.py and .env file): Loads the environment variables including allowed MAC addresses and MQTT topics.
- Main Script (main.py): Orchestrates the receiver and publisher components.
- MQTT Broker (script mqtt-broker.sh indicates installation and configuration of a Mosquitto broker)

##### Out the Data Flow
- In the Gateway, the ZigBee Receiver picks up raw serial data.
- This data is then handed off to the ZigBee Parser for validation and extraction of sensor details.
- Once processed, the MQTT Publisher formats the data into JSON and sends it to the MQTT Broker using the Paho MQTT library.
- The MQTT Broker (Mosquitto) then relays these messages to the Server, which can push updates via WebSocket to a UI.

##### Architectural Patterns and Key Details
- There is a clear separation of concerns:
- Sensor data acquisition is done by the ZigBee Receiver and Parser.
- Data distribution is managed by the MQTT Publisher.
- The system relies on environment-driven configuration (see the .env example and settings.py) to adjust both sensor filtering (allowed MAC addresses) and MQTT settings.
- The README showcases a layered communication model where communication flows stage-by-stage (ZigBee → MQTT → Server → UI).
- Use of shell scripts (setup.sh, mqtt-broker.sh) for installation and configuration is another important element.

##### Relevant Technologies and Frameworks
- Python 3.12.7 is being used as the programming language.
- Libraries: python3-paho-mqtt for MQTT communication.
- Mosquitto is the chosen MQTT Broker.

## Diagram (Mermaid)

```mermaid
flowchart TD
    EN["End Node (Sensors)"]:::external

    subgraph "Home Gateway"
        MS["Main Script (Orchestrator)"]:::backend
        C["Configuration"]:::config
        ZR["ZigBee Receiver"]:::sensor
        ZP["ZigBee Parser"]:::sensor
        MP["MQTT Publisher"]:::message

        MS -->|"initiates"| ZR
        ZR -->|"passesRawData"| ZP
        ZP -->|"passesParsedData"| MP
        C ---|"configures"| MS
        C ---|"configures"| MP
    end

    EN -->|"sendsZigBeeFrames"| ZR
    MP -->|"publishesJSON"| MB["MQTT Broker"]:::message
    MB -->|"relaysMessages"| S["Server"]:::external
    S -->|"updates"| UI["UI"]:::external

    click ZR "https://github.com/kimhamyong/eams-gateway/blob/main/zigbee/receiver.py"
    click ZP "https://github.com/kimhamyong/eams-gateway/blob/main/zigbee/parser.py"
    click MP "https://github.com/kimhamyong/eams-gateway/blob/main/mqtt/publisher.py"
    click C "https://github.com/kimhamyong/eams-gateway/blob/main/config/settings.py"
    click MS "https://github.com/kimhamyong/eams-gateway/blob/main/main.py"
    click MB "https://github.com/kimhamyong/eams-gateway/blob/main/mqtt-broker.sh"

    classDef sensor fill:#a2d5c6,stroke:#333,stroke-width:2px;
    classDef backend fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef config fill:#f9d5e5,stroke:#333,stroke-width:2px;
    classDef message fill:#a1c4fd,stroke:#333,stroke-width:2px;
    classDef external fill:#fda085,stroke:#333,stroke-width:2px;
```
