# dmytrys/u-hub architecture

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

- Interactive diagram: https://gitdiagram.com/dmytrys/u-hub
- Repository: https://github.com/dmytrys/u-hub

## Overview

##### Project Type & Purpose
- This is a microservice-based IoT “u-hub” platform. It unifies device telemetry (via MQTT), event‐driven logic (via RabbitMQ), data storage (MongoDB), a GraphQL API (Apollo), a React client, automation jobs, and voice-assistant integrations (Apple HomeKit & Google Home).

##### Main Components
- Edge Devices (real or fake)—publish telemetry over MQTT
- MQTT Broker (Mosquitto)—receives device messages
- MQTT Listener service—consumes MQTT, repackages as AMQP events
- Message Bus (RabbitMQ)—central AMQP pub/sub
- Microservices (all Node.js/Lerna‐managed):
- apollo—GraphQL API, resolvers, pub/sub, Mongo persistence
- automatic-action—reacts to events to trigger immediate commands
- scheduled-action—runs cron‐style jobs, emits events/commands
- apple-homekit & google-home—voice‐assistant adapters (consume events, send commands)
- client—React SPA talking to Apollo over HTTP
- Database (MongoDB)—stores sensor & action history
- Infrastructure (Docker Compose & Kubernetes manifests)—Ingress‐NGINX, NFS, config maps, deployments

##### Component Interactions & Data Flows
- Edge Device → MQTT Broker (topic: telemetry)
- MQTT Listener → consumes MQTT → publishes event to RabbitMQ
- RabbitMQ → fan-out events → apollo/pubsub, automatic-action, scheduled-action, homekit/google adapters
- apollo → writes to MongoDB; subscribes to bus for real-time updates → GraphQL subscriptions
- client → issues GraphQL queries/mutations to apollo → displays real-time dashboards
- automatic/scheduled adapters → on trigger → publish “command” events to RabbitMQ →
MQTT Listener → publishes MQTT commands back to devices
- apple-homekit/google-home → expose external API endpoints (ingress) → consume bus events & publish commands

##### Architectural Patterns & Principles
- Event-driven microservices (AMQP pub/sub)
- Clear separation of concerns: ingestion (MQTT), transformation (listener), domain logic (services), API (GraphQL), UI (React), integrations (adapters)
- Kubernetes deployment per service, with configmaps, services, ingress resources
- Monorepo (Lerna) sharing common libs (amqp.js, logger.js, etc.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External World
    subgraph "External World"
        FD["Edge Devices"]:::external
        ClientBrowser["React Client UI"]:::client
    end

    %% Kubernetes Cluster
    subgraph "Kubernetes Cluster"
        direction TB
        ING["Ingress-NGINX"]:::infra
        NFS["NFS Storage"]:::infra
        MESQ["Mosquitto (MQTT Broker)"]:::infra
        MQTTR["mqtt-listener"]:::core
        RAB["RabbitMQ"]:::infra
        AP["apollo (GraphQL API)"]:::core
        AA["automatic-action"]:::core
        SA["scheduled-action"]:::core
        AH["apple-homekit"]:::adapter
        GH["google-home"]:::adapter
        MON["MongoDB"]:::infra
    end

    %% Data Flows
    FD -->|MQTT| MESQ
    MESQ -->|MQTT| MQTTR
    MQTTR -->|AMQP| RAB

    RAB -->|AMQP| AP
    RAB -->|AMQP| AA
    RAB -->|AMQP| SA
    RAB -->|AMQP| AH
    RAB -->|AMQP| GH

    AP -->|writes| MON
    ClientBrowser -->|HTTP/GraphQL| AP

    AA -->|command| RAB
    SA -->|cron| RAB

    RAB -->|AMQP| MQTTR
    MQTTR -->|MQTT| MESQ

    %% Ingress Routing
    ING -->|routes to UI| ClientBrowser
    ING -->|routes to API| AP
    ING -->|routes to HomeKit| AH
    ING -->|routes to GoogleHome| GH

    %% Click Events
    click FD "https://github.com/dmytrys/u-hub/tree/master/fake-devices/"
    click MESQ "https://github.com/dmytrys/u-hub/tree/master/infrastructure/mosquitto/"
    click MQTTR "https://github.com/dmytrys/u-hub/tree/master/services/mqtt-listener/"
    click RAB "https://github.com/dmytrys/u-hub/tree/master/infrastructure/rabbit/"
    click AP "https://github.com/dmytrys/u-hub/tree/master/services/apollo/"
    click AA "https://github.com/dmytrys/u-hub/tree/master/services/automatic-action/"
    click SA "https://github.com/dmytrys/u-hub/tree/master/services/scheduled-action/"
    click AH "https://github.com/dmytrys/u-hub/tree/master/services/apple-homekit/"
    click GH "https://github.com/dmytrys/u-hub/tree/master/services/google-home/"
    click ClientBrowser "https://github.com/dmytrys/u-hub/tree/master/services/client/"
    click MON "https://github.com/dmytrys/u-hub/tree/master/infrastructure/mongo/"
    click ING "https://github.com/dmytrys/u-hub/tree/master/infrastructure/ingress-nginx/"
    click NFS "https://github.com/dmytrys/u-hub/tree/master/infrastructure/nfs/"

    %% Styles
    classDef infra fill:#bbdefb,stroke:#0d47a1,color:#0d47a1;
    classDef core fill:#c8e6c9,stroke:#2e7d32,color:#2e7d32;
    classDef adapter fill:#ffe0b2,stroke:#ef6c00,color:#e65100;
    classDef client fill:#e1bee7,stroke:#6a1b9a,color:#4a148c;
    classDef external fill:#eceff1,stroke:#607d8b,color:#37474f;
```
