# 2pk03/bacnet-mqtt-gateway architecture

> Architecture diagram of the GitHub repository 2pk03/bacnet-mqtt-gateway, 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/2pk03/bacnet-mqtt-gateway
- Repository: https://github.com/2pk03/bacnet-mqtt-gateway

## Overview

##### Project Type and Purpose
- The project is a Node.js application that acts as a gateway connecting BACnet devices (using the Node BACstack library) to an MQTT broker in the cloud.
- Its primary functionalities include discovering BACnet devices, reading their properties, polling device values on a configurable schedule, and exposing a REST API alongside a web-based admin interface.

##### Key Components
a. Node.js Application Core:
- Core process (app.js) which initializes and coordinates the different modules.
- The “src” directory includes:
- bacnet_client.js – Handles communication with BACnet devices.
- mqtt_client.js – Manages connection and data publishing to the MQTT broker.
- bacnet_config.js and common.js – Likely assist with internal configuration and shared utilities.
- server.js – Implements the HTTP server that powers the REST API and serves the web interface.

b. Configuration and Data Files:
- “config/default.json” that holds global configurations (MQTT settings, BACnet settings, and HTTP server configuration).
- “devices/” directory which contains JSON files (e.g., baspi.json) for individual device polling configurations, including CRON schedules and other device-specific settings.

c. Web Admin Interface:
- “web” directory with index.html, admin.js, and admin.css that together provide a simple, browser-based interface for administration and configuration.

d. External Systems:
- BACnet network: External physical devices discovered and queried via the WhoIs/Read Property procedures.
- MQTT Broker: The gateway sends data (or receives commands) to/from an MQTT broker (e.g., infinimesh.cloud) as configured.

e. Docker Integration:
- Dockerfile and related files indicate that the system can be containerized. Optionally, add a container boundary if illustrating deployment environments.

##### Relationships and Data Flows
- The application initiates by reading configuration from “config/default.json” and device-specific polling configurations from the “devices” folder.
- The REST API (via server.js) serves endpoints for scanning BACnet devices, configuring polling, and fetching object details.
- Communication layers:
- The BACnet Client sends “WhoIs” and “Read Property” commands to physical BACnet devices on the network.
- The MQTT Client publishes polled data and other information to the configured MQTT broker.
- The web admin interface provides a UI through which administrators can interact with the REST API endpoints.
- When running in Docker, volume mounts are used for external configuration and device files so that the containerized version remains dynamic.

##### Architectural Patterns & Design Principles
- Layered architecture: Separation between the communication modules (BACnet and MQTT), HTTP server, and configuration management.
- Modularity: Each functional area (BACnet communication, MQTT integration, REST API) is encapsulated in its own module.
- Config-driven behavior: The use of JSON configuration (for both global settings and device polling) suggests high flexibility and ease of customization.
- External integrations: Clear interfaces to external systems (BACnet devices and MQTT broker), reinforcing loose coupling.

- “BACnet Client” (implemented in bacnet_client.js and bacnet_config.js)
- “MQTT Client” (implemented in mqtt_client.js)
- “HTTP Server & REST API” (implemented in server.js and app.js)
- “Utility/Shared Logic” (common.js)
- “Admin Web Interface” linked to the HTTP Server (showing both the static content from the “web” directory and the REST API endpoints).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Docker Container Boundary
    subgraph "Docker Container"
        subgraph "Node.js Application Core"
            BACnetClient["BACnet Client"]:::backend
            BACnetConfig["BACnet Config"]:::backend
            MQTTClient["MQTT Client"]:::backend
            HTTPServer["HTTP Server & REST API"]:::backend
            SharedUtil["Utility/Shared Logic"]:::backend
        end
    end

    %% External Components
    ConfigMgmt["Configuration Management"]:::config
    AdminUI["Web Admin Interface"]:::ui
    BACnetNetwork["BACnet Devices Network"]:::external
    MQTTBroker["MQTT Broker"]:::external
    DockerFile["Dockerfile"]:::docker

    %% Data Flow Connections
    ConfigMgmt -->|"loadsConfig"| BACnetClient
    ConfigMgmt -->|"loadsConfig"| BACnetConfig
    ConfigMgmt -->|"loadsConfig"| MQTTClient
    ConfigMgmt -->|"loadsConfig"| HTTPServer

    BACnetClient -->|"discoversDevices"| BACnetNetwork
    MQTTClient -->|"publishesData"| MQTTBroker
    HTTPServer -->|"servesUI"| AdminUI
    AdminUI -->|"callsAPI"| HTTPServer

    %% Click Events for Node.js Application Core Components
    click BACnetClient "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/bacnet_client.js"
    click BACnetConfig "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/bacnet_config.js"
    click MQTTClient "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/mqtt_client.js"
    click HTTPServer "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/server.js"
    click HTTPServer "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/app.js"
    click SharedUtil "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/src/common.js"

    %% Click Events for Configuration Management Components
    click ConfigMgmt "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/config/default.json"
    click ConfigMgmt "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/devices/baspi.json"

    %% Click Events for Web Admin Interface Components
    click AdminUI "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/web/index.html"
    click AdminUI "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/web/admin.js"
    click AdminUI "https://github.com/2pk03/bacnet-mqtt-gateway/blob/master/web/admin.css"

    %% Click Event for Docker Integration
    click DockerFile "https://github.com/2pk03/bacnet-mqtt-gateway/tree/master/Dockerfile"

    %% Styling Definitions
    classDef backend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef config fill:#cfc,stroke:#333,stroke-width:2px;
    classDef ui fill:#fc9,stroke:#333,stroke-width:2px;
    classDef external fill:#fdd,stroke:#333,stroke-width:2px;
    classDef docker fill:#ddf,stroke:#333,stroke-width:2px;
```
