# hypfer/valetudo architecture

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

- Interactive diagram: https://gitdiagram.com/hypfer/valetudo
- Repository: https://github.com/hypfer/valetudo

## Overview

##### Project type and purpose
- This is a full-stack, on-premises “cloud replacement” for vacuum robots called **Valetudo**.
- It consists of:
- A **Node.js** backend that handles communication with the robot (via Miio), exposes a REST-Swagger API, an MQTT interface (Homie + Home Assistant autodiscovery), a webserver (HTTP + SSE), an updater, scheduler, SSDP/NTP clients, etc.
- A **React/TypeScript** single-page application (SPA) frontend served by that backend.
- Integration points: the physical robot (LAN), an MQTT broker of your choice, NTP servers, and GitHub for update checks.

##### Top-level components and directory structure
- **frontend/** – React SPA, calls REST/SSE endpoints and MQTT indirectly via backend
- **backend/** – Node.js monolith, subdivided into:
– **lib/webserver/** – HTTP server, routers for every capability, SSE middleware
– **lib/miio/** – Miio protocol adapter (UDP sockets) to send/receive robot commands
– **lib/mqtt/** – MQTT controller/adapters for Homie/HASS, publishes state + accepts commands
– **lib/core/** + **lib/entities/** – Domain model (robots, capabilities, events)
– **lib/scheduler/** – Local task scheduler for timers
– **lib/updater/** – Firmware‐update logic + GitHub provider
– **lib/utils/** + **lib/valetudo_events/** – Logging, caching, event handling, SSDP/NTP support
- **util/** – Build scripts, code generators (OpenAPI, MQTT docs, release manifests)

##### Architectural style & patterns
- The backend uses a **modular/hexagonal** approach:
- **Adapters** for protocols (Miio, MQTT, HTTP, SSDP, NTP)
- **Ports**/services in **lib/core**, **lib/entities**, and **valetudo_events**
- **Separation** of concerns: routers ➔ controllers ➔ capability handlers ➔ domain model
- The SPA is a **containerized** React app that interacts via REST/SSE; no direct Miio/MQTT calls.

##### Main components & interactions
A. Frontend (React SPA in user’s browser)
- Served over HTTP by the backend’s WebServer module
- Fetches REST API endpoints (CapabilitiesRouter, RobotRouter, etc.)
- Subscribes to Server-Sent Events for live state updates
B. Backend (Node.js service running on the vacuum or gateway)
1. **WebServer** – HTTP(S) + SSE endpoint
2. **MQTTController** – connects to external MQTT broker, publishes state, subscribes to commands
3. **MiioAdapter** – UDP socket to talk to the robot’s firmware
4. **Scheduler** – Timer engine triggering actions via core capabilities
5. **Updater** – Checks GitHub, downloads/releases firmware updates
6. **NTPClient** – Synchronizes clock to NTP servers
7. **SSDPServer** – Broadcasts service availability on LAN
8. **Core Domain** – ValetudoRobotFactory, Capability implementations, Entities, EventStore
C. External services
- **Vacuum Robot** – accepts Miio packets (commands) and sends status/map data
- **MQTT Broker** – receives Homie/HA-autodiscovery announcements + telemetry
- **NTP Server** – time synchronization
- **GitHub Update Provider** – firmware metadata & binaries

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend" 
        F["React SPA"]:::frontend
    end

    subgraph "Backend"
        subgraph "Adapters"
            WS["WebServer"]:::module
            MQTT["MQTTController"]:::module
            MIIO["MiioAdapter"]:::module
            SSDP["SSDPServer"]:::module
            NTP["NTPClient"]:::module
        end
        subgraph "Services"
            SCH["Scheduler"]:::module
            UPD["Updater"]:::module
        end
        subgraph "Core Domain"
            CORE["Core Domain"]:::core
        end
        UTIL["Utilities"]:::module
        CONF["Configuration & Bootstrapping"]:::module
    end

    subgraph "External Services"
        ROBOT["Vacuum Robot"]:::external
        BROKER["MQTT Broker"]:::external
        NTP_S["NTP Server"]:::external
        GITHUB["GitHub"]:::external
    end

    F -->|"HTTP REST"| WS
    WS -->|"serves assets & SSE"| F
    WS -->|"invokes capabilities"| CORE
    CORE -->|"commands"| MIIO
    MQTT <-->|"MQTT"| BROKER
    MIIO <-->|"UDP/Miio"| ROBOT
    UPD -->|"HTTPS"| GITHUB
    NTP <-->|"NTP"| NTP_S
    SSDP -->|"SSDP multicast"| ROBOT
    SCH -->|"triggers actions"| CORE

    click F "https://github.com/hypfer/valetudo/blob/master/frontend/package.json"
    click WS "https://github.com/hypfer/valetudo/blob/master/backend/lib/webserver/WebServer.js"
    click MQTT "https://github.com/hypfer/valetudo/blob/master/backend/lib/mqtt/MqttController.js"
    click MIIO "https://github.com/hypfer/valetudo/blob/master/backend/lib/miio/MiioSocket.js"
    click SCH "https://github.com/hypfer/valetudo/blob/master/backend/lib/scheduler/Scheduler.js"
    click UPD "https://github.com/hypfer/valetudo/blob/master/backend/lib/updater/Updater.js"
    click NTP "https://github.com/hypfer/valetudo/blob/master/backend/lib/NTPClient.js"
    click SSDP "https://github.com/hypfer/valetudo/blob/master/backend/lib/utils/SSDPServer.js"
    click CORE "https://github.com/hypfer/valetudo/blob/master/backend/lib/core/ValetudoRobotFactory.js"
    click UTIL "https://github.com/hypfer/valetudo/tree/master/backend/lib/utils/"
    click CONF "https://github.com/hypfer/valetudo/blob/master/backend/index.js"

    classDef module fill:#D6EAF8,stroke:#1B4F72,color:#1B2631;
    classDef core fill:#FAD7A0,stroke:#B9770E,color:#1B2631;
    classDef external fill:#A9DFBF,stroke:#196F3D,color:#1B2631;
    classDef frontend fill:#EBDEF0,stroke:#7D3C98,color:#1B2631;
```
