How 2pk03/bacnet-mqtt-gateway works
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).