# andreicatanoiu/guardianhivesync architecture

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

- Interactive diagram: https://gitdiagram.com/andreicatanoiu/guardianhivesync
- Repository: https://github.com/andreicatanoiu/guardianhivesync

## Overview

##### 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”)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Entry point
    Entry["GuardianHiveSync Service (index.js)"]:::service
    click Entry "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"

    %% External Systems
    Sensors["External IoT Sensors"]:::external
    Broker["MQTT Broker"]:::external

    Sensors -->|"MQTT publish"|Broker

    %% Service Modules
    subgraph "GuardianHiveSync Modules"
        direction TB
        Config["Configuration Loader"]:::config
        Client["MQTT Client"]:::service
        Processor["Message Processor"]:::service
        Adapter["Firestore Adapter"]:::service
        Scheduler["Scheduler / Offline Checker<br/>(every 60s)"]:::service
    end

    Entry --> Config
    Entry --> Client
    Entry --> Processor
    Entry --> Adapter
    Entry --> Scheduler

    Broker -->|"MQTT subscribe"|Client
    Client -->|"raw message"|Processor
    Processor -->|"write new record"|Adapter
    Processor -->|"update device status"|Adapter
    Scheduler -->|"read deviceStatus"|Adapter
    Scheduler -->|"update offline status"|Adapter

    %% Firestore Database
    subgraph "Firestore Database"
        Messages["mqtt_messages"]:::db
        Status["deviceStatus"]:::db
    end

    Adapter -->|"Firestore API write"|Messages
    Adapter -->|"Firestore API write/read"|Status

    %% Project Files
    subgraph "Project Files"
        Pkg["package.json"]:::config
        Readme["README.md"]:::config
    end
    click Config "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"
    click Client "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"
    click Processor "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"
    click Adapter "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"
    click Scheduler "https://github.com/andreicatanoiu/guardianhivesync/blob/main/index.js"
    click Pkg "https://github.com/andreicatanoiu/guardianhivesync/blob/main/package.json"
    click Readme "https://github.com/andreicatanoiu/guardianhivesync/blob/main/README.md"

    %% Styles
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
    classDef service fill:#add8e6,stroke:#333,stroke-width:1px
    classDef db fill:#ffcc80,stroke:#333,stroke-width:1px
    classDef config fill:#c3f7c7,stroke:#333,stroke-width:1px
```
