# nam077/smart-home architecture

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

- Interactive diagram: https://gitdiagram.com/nam077/smart-home
- Repository: https://github.com/nam077/smart-home

## Overview

##### Core System Components
- NestJS Backend Server
- MQTT Broker (Aedes)
- PostgreSQL Database
- Authentication System (JWT)
- WebSocket Server
- IoT Devices Layer

##### Module Organization
- Auth Module
- Device Module
- Room Module
- User Module
- MQTT Module
- Controller Module
- Voice Controller Module

##### Communication Flows
Distinct communication patterns:
- REST API calls (HTTP/HTTPS)
- MQTT message flows
- WebSocket connections
- Database interactions

##### Top Level (Client Layer)
- ┌─────────────────────────────────────────┐
- Client Applications           │
- (Web Browsers, Mobile Apps, IoT Devices)│
- ┘

##### Communication Layer
- ┌─────────────┐  ┌─────────────┐  ┌─────────────┐
- REST API   │  │   MQTT      │  │  WebSocket  │
- (Port 3000)│  │ (Port 1883) │  │ (Port 8883) │
- ┘  └─────────────┘  └─────────────┘

##### Core Application Layer
- ┌─────────────────────────────────────────────────┐
- NestJS Application                 │
- ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌───────┐ │
- Auth   │ │ Device  │ │  Room   │ │ User  │ │
- Module  │ │ Module  │ │ Module  │ │Module │ │
- ┘ └─────────┘ └─────────┘ └───────┘ │
- ┌─────────┐ ┌─────────┐ ┌─────────────────┐   │
- MQTT   │ │ Voice   │ │   Controller    │   │
- Module  │ │ Control │ │     Module      │   │
- ┘ └─────────┘ └─────────────────┘   │
- ┘

##### Service Layer
- ┌─────────────┐  ┌─────────────┐  ┌─────────────┐
- Device    │  │    MQTT     │  │    Auth     │
- Services   │  │  Services   │  │  Services   │
- ┘  └─────────────┘  └─────────────┘

##### Data Layer
- ┌─────────────────────────────────────────┐
- PostgreSQL Database          │

- Users | Devices | Rooms | Controllers  │
- ┘

- Blue: Client-facing components
- Green: Core application modules
- Yellow: Communication layers
- Purple: Database and storage
- Red: Security components

Important Elements to Highlight:

##### MQTT Flow
- Topic structure: home/{roomId}/{deviceId}/{type}
- Message types (status/control/data/error)
- Heartbeat monitoring flow

##### Security Layer
- JWT authentication flow
- MQTT authentication
- API security middleware

##### Device Management
- Device discovery process
- Status monitoring
- Command routing

##### Database Relationships
- User-Device relationships
- Room-Device associations
- Controller hierarchies

- Component types
- Security boundaries

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Clients
        WEB["Web Browsers"]
        MOB["Mobile Apps"]
        IOT["IoT Devices"]
    end

    subgraph Communication
        REST["REST API (3000)"]:::api
        MQTT["MQTT Broker (1883)"]:::mqtt
        WS["WebSocket (8883)"]:::websocket
    end

    subgraph NestJS["NestJS Application"]
        MAIN["Main Server"]:::core
        click MAIN "https://github.com/Nam077/smart-home/blob/develop/src/main.ts"
        
        subgraph Modules
            AUTH_MOD["Auth Module"]:::auth
            click AUTH_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/auth/auth.module.ts"
            
            DEV_MOD["Device Module"]:::device
            click DEV_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/device/device.module.ts"
            
            ROOM_MOD["Room Module"]:::room
            click ROOM_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/room/room.module.ts"
            
            USER_MOD["User Module"]:::user
            click USER_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/user/user.module.ts"
            
            MQTT_MOD["MQTT Module"]:::mqtt
            click MQTT_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/mqtt/mqtt.module.ts"
            
            CTRL_MOD["Controller Module"]:::controller
            click CTRL_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/controller/controller.module.ts"
            
            VOICE_MOD["Voice Controller"]:::voice
            click VOICE_MOD "https://github.com/Nam077/smart-home/blob/develop/src/modules/voice-controller/voice-controller.module.ts"
        end

        subgraph Services
            AUTH_SVC["Auth Service"]:::auth
            click AUTH_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/auth/services/auth.service.ts"
            
            DEV_SVC["Device Service"]:::device
            click DEV_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/device/services/device.service.ts"
            
            MQTT_SVC["MQTT Handler"]:::mqtt
            click MQTT_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/mqtt/services/mqtt-handler.service.ts"
            
            HEART_SVC["Heartbeat Service"]:::mqtt
            click HEART_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/mqtt/services/mqtt-heartbeat.service.ts"
            
            ROOM_SVC["Room Service"]:::room
            click ROOM_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/room/services/room.service.ts"
            
            USER_SVC["User Service"]:::user
            click USER_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/user/services/user.service.ts"
            
            CTRL_SVC["Controller Service"]:::controller
            click CTRL_SVC "https://github.com/Nam077/smart-home/blob/develop/src/modules/controller/services/controller.service.ts"
        end
    end

    subgraph Database
        DB[(PostgreSQL)]:::database
        click DB "https://github.com/Nam077/smart-home/blob/develop/src/config/database/configuration.ts"
        
        subgraph Entities
            USER_ENT["User Entity"]:::entity
            click USER_ENT "https://github.com/Nam077/smart-home/blob/develop/src/modules/user/entities/user.entity.ts"
            
            DEV_ENT["Device Entity"]:::entity
            click DEV_ENT "https://github.com/Nam077/smart-home/blob/develop/src/modules/device/entities/device.entity.ts"
            
            ROOM_ENT["Room Entity"]:::entity
            click ROOM_ENT "https://github.com/Nam077/smart-home/blob/develop/src/modules/room/entities/room.entity.ts"
            
            CTRL_ENT["Controller Entity"]:::entity
            click CTRL_ENT "https://github.com/Nam077/smart-home/blob/develop/src/modules/controller/entities/controller.entity.ts"
        end
    end

    %% Client Connections
    WEB & MOB & IOT --> REST
    WEB & MOB & IOT --> WS
    IOT --> MQTT

    %% Communication Layer to NestJS
    REST --> MAIN
    MQTT --> MQTT_MOD
    WS --> MAIN

    %% Module Relationships
    MAIN --> AUTH_MOD & DEV_MOD & ROOM_MOD & USER_MOD & MQTT_MOD & CTRL_MOD & VOICE_MOD

    %% Service Relationships
    AUTH_MOD --> AUTH_SVC
    DEV_MOD --> DEV_SVC
    MQTT_MOD --> MQTT_SVC
    MQTT_MOD --> HEART_SVC
    ROOM_MOD --> ROOM_SVC
    USER_MOD --> USER_SVC
    CTRL_MOD --> CTRL_SVC

    %% Database Relationships
    AUTH_SVC & USER_SVC --> USER_ENT
    DEV_SVC --> DEV_ENT
    ROOM_SVC --> ROOM_ENT
    CTRL_SVC --> CTRL_ENT
    USER_ENT & DEV_ENT & ROOM_ENT & CTRL_ENT --> DB

    classDef api fill:#87CEEB,stroke:#333,stroke-width:2px
    classDef mqtt fill:#90EE90,stroke:#333,stroke-width:2px
    classDef websocket fill:#FFB6C1,stroke:#333,stroke-width:2px
    classDef core fill:#4169E1,stroke:#333,stroke-width:2px
    classDef auth fill:#FF6B6B,stroke:#333,stroke-width:2px
    classDef device fill:#48D1CC,stroke:#333,stroke-width:2px
    classDef room fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef user fill:#F0E68C,stroke:#333,stroke-width:2px
    classDef controller fill:#98FB98,stroke:#333,stroke-width:2px
    classDef voice fill:#FFA07A,stroke:#333,stroke-width:2px
    classDef database fill:#BA55D3,stroke:#333,stroke-width:2px
    classDef entity fill:#E6E6FA,stroke:#333,stroke-width:2px
```
