# ngoctruongbui2003/smart-agriculture-system architecture

> Architecture diagram of the GitHub repository ngoctruongbui2003/smart-agriculture-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/ngoctruongbui2003/smart-agriculture-system
- Repository: https://github.com/ngoctruongbui2003/smart-agriculture-system

## Overview

##### Main Components
- NestJS Application Server – This is the core of the project that ties everything together. It’s built using Nest’s modular architecture, so each “module” (auth, field, mail, sensor, users, watering-history, weather) becomes a distinct component.
- Modules/Domain Components – Within the server, you have several modules:
◦ Auth Module – Handles authentication and related services.
◦ Field, Users, and Weather Modules – Represent domain-specific logic and API endpoints.
◦ Sensor Module – Notably includes a WebSocket gateway (sensor.gateway.ts) for real-time data updates.
◦ Mail Module – Provides email integration (using email templates, e.g., gas-alert.hbs and weekly-report.hbs).
◦ Watering History Module – Likely handles historical data logging.
- MQTT Broker – The mosquitto folder (with its configuration file) indicates that MQTT (via Mosquitto) is used, probably to ingest or relay sensor or external IoT data.
- Database (Implied) – The “schemas” directory (for fields, sensors, users, etc.) hints at a persistent layer (likely using MongoDB via Mongoose or another ORM/ODM) that stores application data.
- Deployment & Infrastructure – Dockerfile, docker-compose.yml, and CI/CD configuration under .github/workflows show that containerization and automated deployment are part of the solution.

##### Relationships and Interactions
- That controllers and services within each module interact with each other – e.g., controllers receive HTTP requests and delegate to corresponding services.
- That the Sensor Module includes a WebSocket gateway which manages real-time communication with clients.
- Optionally, add an external Email Service component connected to the Mail Module.

##### Architectural Patterns & Design Principles
- Modular Architecture – Each domain function (authentication, sensor management, mailing, etc.) is encapsulated in its own module, promoting separation of concerns.
- Service & Controller Pattern (MVC-like) – Within each module, controllers handle requests, while providers (services) encapsulate business logic.
- Inter-Service Communication – If necessary, indicate data flows between modules (e.g., sensor data might trigger notifications via the Mail Module).
- Containerization – Represent the deployment infrastructure with a Docker container group that includes the NestJS server, the MQTT broker (Mosquitto), and potentially the database.

##### Incorporate Relevant Technologies
- Framework: NestJS with TypeScript.
- WebSockets: Used in the sensor module via a gateway for real-time communication.
- MQTT Broker: Mosquitto for messaging.
- CI/CD & Deployment: GitHub Actions (.github/workflows), Docker, docker-compose, and even pointers to cloud deployment with NestJS Mau.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Server Subgraph
    subgraph "NestJS Application Server"
        direction TB
        Server["NestJS App Server"]:::server
        subgraph "Modules"
            direction TB
            Auth["Auth Module"]:::module
            Field["Field Module"]:::module
            Mail["Mail Module"]:::module
            Sensor["Sensor Module (WebSocket)"]:::module
            Users["Users Module"]:::module
            Watering["Watering History Module"]:::module
            Weather["Weather Module"]:::module
        end
    end

    %% External Components
    MQTT["MQTT Broker"]:::external
    DB["Database (Schemas)"]:::external
    Email["External Email Service"]:::external
    Deploy["Deployment & Infrastructure"]:::infra

    %% Relationships inside the system
    %% NestJS App Server orchestrates modules
    Server -->|"init"| Auth
    Server -->|"init"| Field
    Server -->|"init"| Mail
    Server -->|"init"| Sensor
    Server -->|"init"| Users
    Server -->|"init"| Watering
    Server -->|"init"| Weather

    %% External interactions
    MQTT -->|"sensor_data"| Sensor
    Auth -->|"data_access"| DB
    Field -->|"data_access"| DB
    Users -->|"data_access"| DB
    Weather -->|"data_access"| DB
    Watering -->|"data_access"| DB
    Sensor -->|"store_data"| DB
    Mail -->|"send_email"| Email

    %% Deployment configuration
    Deploy -->|"deploys"| Server
    Deploy -->|"deploys"| MQTT
    Deploy -->|"deploys"| DB
    Deploy -->|"deploys"| Email

    %% Click Events for NestJS Application Server
    click Server "https://github.com/ngoctruongbui2003/smart-agriculture-system/blob/main/src/main.ts"
    click Server "https://github.com/ngoctruongbui2003/smart-agriculture-system/blob/main/src/app.module.ts"

    %% Click Events for Modules
    click Auth "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/auth"
    click Field "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/field"
    click Mail "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/mail"
    click Sensor "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/sensor"
    click Users "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/users"
    click Watering "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/watering-history"
    click Weather "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/modules/weather"

    %% Click Events for MQTT Broker
    click MQTT "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/mosquitto"

    %% Click Event for Database Schemas
    click DB "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/src/schemas"

    %% Click Events for Deployment & Infrastructure
    click Deploy "https://github.com/ngoctruongbui2003/smart-agriculture-system/tree/main/Dockerfile"
    click Deploy "https://github.com/ngoctruongbui2003/smart-agriculture-system/blob/main/docker-compose.yml"
    click Deploy "https://github.com/ngoctruongbui2003/smart-agriculture-system/blob/main/.github/workflows/main.yml"

    %% Styles
    classDef server fill:#f9c,stroke:#333,stroke-width:2px;
    classDef module fill:#ccf,stroke:#333,stroke-width:2px;
    classDef external fill:#cfc,stroke:#333,stroke-width:2px;
    classDef infra fill:#fc9,stroke:#333,stroke-width:2px;
```
