# vuxpn/vuxpn-smart-home-safety-and-security-system architecture

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

- Interactive diagram: https://gitdiagram.com/vuxpn/vuxpn-smart-home-safety-and-security-system
- Repository: https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system

## Overview

##### Main Components
- Backend: Represents the core server-side logic. It handles authentication, user/home management, IoT device management, real-time updates (WebSocket), and interactions with a MongoDB database. Within the backend’s architecture, highlight modules such as authentication (with JWT and role-based access), devices, gas warning, home management, notification, posts, mqtt, and TCP handling.
- Frontend: The client-side web application built using React, Vite, and TailwindCSS. It is responsible for providing a responsive UI, dashboards, device control components, real-time sensor monitoring, and user context management.
- Hardware: Physical IoT components comprised of ESP32-based devices (ESP32-CAM for image capture, Gas Warning System, LED control units, and PIR motion sensors). These devices connect to the backend via MQTT and provide sensor data, images, and real-time alerts.

##### Component Relationships & Interactions
- Backend’s interaction with the MongoDB database as a separate module that stores user information, device data, and other configurations.
- Bidirectional communication between Hardware devices and the Backend using MQTT. Optionally, indicate that some hardware might use additional protocols (like HTTP when needed for file or image uploads).
- Make clear that the backend modules (authentication, devices, notifications, etc.) work together to process, validate, and route data between the database, API, and real-time messaging systems.

##### Architectural Patterns & Design Principles
- Modular Architecture: The backend is divided into clearly separated modules (such as auth, devices, home, etc.) reflecting separation of concerns.
- Event-driven & Real-Time Communication: Real-time updates via WebSocket in the backend and MQTT for hardware interactions should be emphasized.
- API-First Design: Frontend interacts via defined REST endpoints, ensuring the decoupling between the client and server.
- Security: JWT Authentication for user security and role-based access control form key design considerations.
- IoT Patterns: The hardware segment follows IoT design principles where sensors (ESP32, PIR, etc.) publish data to the cloud (backend) for processing and then display results on the dashboard.

##### Detail and Clarity
- To capture key architectural elements such as security, real-time communication, modularization, and hardware integration.
- Avoid oversimplification: represent even internal modules (e.g., authentication and device management) if space and clarity permit, to ensure that all architectural nuances are visible.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Backend Subgraph
    subgraph "Backend – NestJS Application"
        BE_Nest["Backend – NestJS Application"]:::backend
        BE_Auth["Authentication Module"]:::backend
        BE_Devices["Devices Module"]:::backend
        BE_Gas["Gas Warning Module"]:::backend
        BE_Home["Home Management Module"]:::backend
        BE_Notification["Notification Module"]:::backend
        BE_Posts["Posts Module"]:::backend
        BE_MQTT["MQTT Integration"]:::backend
        BE_TCP["TCP Handling Module"]:::backend
        DB["MongoDB Database"]:::database
    end

    %% Frontend Subgraph
    subgraph "Frontend – React/Vite Application"
        FE_Main["Frontend – React/Vite Application"]:::frontend
        FE_Auth["Auth Components"]:::frontend
        FE_Dashboard["Dashboard Components"]:::frontend
        FE_Device["Device Management Components"]:::frontend
        FE_Home["Home Management Components"]:::frontend
        FE_Context["State Management Contexts"]:::frontend
    end

    %% Hardware Subgraph
    subgraph "Hardware – IoT Devices"
        HW_Gas["Gas Warning System"]:::hardware
        HW_CAM1["ESP32-CAM"]:::hardware
        HW_CAM2["ESP32-CAM"]:::hardware
        HW_LED["LED Control Unit"]:::hardware
        HW_PIR["PIR Motion Sensor"]:::hardware
    end

    %% Internal Backend Module Connections
    BE_Nest -->|"module"| BE_Auth
    BE_Nest -->|"module"| BE_Devices
    BE_Nest -->|"module"| BE_Gas
    BE_Nest -->|"module"| BE_Home
    BE_Nest -->|"module"| BE_Notification
    BE_Nest -->|"module"| BE_Posts
    BE_Nest -->|"module"| BE_MQTT
    BE_Nest -->|"module"| BE_TCP
    BE_Nest -->|"CRUD"| DB

    %% Internal Frontend Component Connections
    FE_Main --> FE_Auth
    FE_Main --> FE_Dashboard
    FE_Main --> FE_Device
    FE_Main --> FE_Home
    FE_Main --> FE_Context

    %% Communication between Frontend and Backend
    FE_Main -->|"HTTP/REST+WS"| BE_Nest

    %% Communication between Backend and Hardware via MQTT
    BE_MQTT ---|"MQTT"| HW_Gas
    BE_MQTT ---|"MQTT"| HW_CAM1
    BE_MQTT ---|"MQTT"| HW_CAM2
    BE_MQTT ---|"MQTT"| HW_LED
    BE_MQTT ---|"MQTT"| HW_PIR

    %% Additional Communication: ESP32-CAM HTTP Upload
    HW_CAM1 -->|"HTTP"| BE_Nest

    %% Styles
    classDef backend fill:#a9d0f5,stroke:#1f4e79,stroke-width:2px;
    classDef frontend fill:#c3f7d0,stroke:#2d7a45,stroke-width:2px;
    classDef hardware fill:#f7d6ad,stroke:#b36b00,stroke-width:2px;
    classDef database fill:#ffcccb,stroke:#d9534f,stroke-width:2px;

    %% Click Events
    click BE_Nest "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src"
    click BE_Auth "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/auth"
    click BE_Devices "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/devices"
    click BE_Gas "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/gaswarning"
    click BE_Home "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/home"
    click BE_Notification "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/notification"
    click BE_Posts "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/posts"
    click BE_MQTT "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/mqtt"
    click BE_TCP "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Backend/src/tcp"
    click FE_Main "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project"
    click FE_Auth "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project/src/components/auth"
    click FE_Dashboard "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project/src/components/dashboard"
    click FE_Device "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project/src/components/device"
    click FE_Home "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project/src/components/home"
    click FE_Context "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Frontend/my-project/src/contexts"
    click HW_Gas "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Hardware/Gas_warning"
    click HW_CAM1 "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Hardware/esp32cam"
    click HW_CAM2 "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Hardware/esp32cam(1)"
    click HW_LED "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Hardware/led"
    click HW_PIR "https://github.com/vuxpn/vuxpn-smart-home-safety-and-security-system/tree/master/Hardware/pir"
```
