How andreicatanoiu/guardianhivesync works
Project Type and Purpose
• GuardianHiveSync is a single-process Node.js “backend” microservice.
• It performs event-driven ingestion of MQTT messages from IoT sensors and persists them into Firestore, while also monitoring device heartbeats to mark devices offline.
Main Components
a. External IoT Sensors
b. MQTT Broker (e.g., Mosquitto, EMQX)
c. GuardianHiveSync Service
– MQTT Client (subscribes to topics)
– Message Processor (parses topic payload, enriches data)
– Firestore Adapter (writes to mqtt_messages and deviceStatus collections)
– Scheduler / Offline Checker (runs every 60 s)
– Configuration Loader (reads Firebase service account JSON, MQTT settings)
d. Firestore Database
– Collection mqtt_messages
– Collection deviceStatus
e. Firebase Admin SDK (embedded in the service, authenticated via service account file)
Component Interactions and Data Flows
1. Sensors → (MQTT publish) → MQTT Broker
2. GuardianHiveSync → (MQTT subscribe) → MQTT Broker → delivers messages to the MQTT Client
3. MQTT Client → Message Processor → Firestore Adapter → writes new records into mqtt_messages
4. Message Processor → updates device status (online or maintenance) in deviceStatus
5. Scheduler → every 60 s reads deviceStatus → marks stale devices offline → logs events → updates Firestore
Architectural Patterns and Design Principles
• Event-driven / Pub-Sub: Leveraging MQTT topics as the primary data channel.
• Single Responsibility: Separate modules for connectivity, processing, persistence, and scheduling.
• Resilience: Automatic offline detection via polling timer.
• Externalized Configuration: Service account JSON file + MQTT credentials loaded at runtime.
– Component names (e.g., “MQTT Client,” “Message Processor”)
– Notes on timing (e.g., “Scheduler runs every 60 s”)