Skip to main content

Kiến trúc hệ thống

Tổng quan về kiến trúc và các thành phần của Smart Forecast.


Sơ đồ kiến trúc tổng thể

Architecture Overview
Sơ đồ kiến trúc tổng quan hệ thống Smart-Forecast
Architecture Detail
Kiến trúc chi tiết các thành phần
                    ┌─────────────────────────────────────────┐
│ DATA SOURCES │
│ (OpenWeatherMap APIs) │
└─────────────────┬───────────────────────┘


┌─────────────────┐ ┌─────────────────────────────────────┐
│ Mobile App │ │ Backend (NestJS) │
│ (Expo RN) │◄───►│ • Data Ingestion (NGSI-LD) │
└─────────────────┘ │ • Alert Management │
│ • Incident Reports │
┌─────────────────┐ │ • Push Notifications (FCM) │
│ Web Dashboard │◄───►│ • REST API │
│ (Next.js) │ └─────────────────┬───────────────────┘
└─────────────────┘ │
┌─────────────┼─────────────┐
│ │ │
┌────▼────┐ ┌───▼────┐ ┌───▼────┐
│ Orion │ │Postgres│ │ MinIO │
│ -LD │ │ DB │ │Storage │
└────┬────┘ └────────┘ └────────┘

┌────▼────┐
│ MongoDB │
└─────────┘

Cấu trúc Monorepo

smart-forecast/
├── backend/ # NestJS API Server
├── web/ # Next.js Admin Dashboard
├── mobile/ # Expo React Native App
├── shared/ # Shared TypeScript Models & Constants
├── docs-site/ # Docusaurus Documentation
├── scripts/ # Utility Scripts
├── docker-compose.yml
└── pnpm-workspace.yaml

Mô tả các thành phần

Thư mụcCông nghệMô tả
backend/NestJSREST API, xử lý business logic, tích hợp NGSI-LD
web/Next.js 15Dashboard quản trị cho Admin/Manager
mobile/ExpoỨng dụng di động cho người dân
shared/TypeScriptTypes, interfaces, constants dùng chung
docs-site/DocusaurusTrang tài liệu (đang xem)

Luồng dữ liệu

1. Thu thập dữ liệu (Data Ingestion)

OpenWeatherMap API


┌───────────────────┐
│ Ingestion Module │
│ (Backend NestJS) │
└─────────┬─────────┘


┌───────────────────┐
│ Transform to │
│ NGSI-LD Entities │
│ (JSON-LD format) │
└─────────┬─────────┘

┌─────┴─────┐
│ │
▼ ▼
┌───────┐ ┌─────────┐
│Orion │ │Postgres │
│ -LD │ │(History)│
└───────┘ └─────────┘

2. Xử lý cảnh báo (Alert Flow)

┌─────────────────┐
│ Admin Dashboard │
│ (Next.js) │
└────────┬────────┘
│ POST /alerts

┌─────────────────┐
│ Alert Module │
│ (Backend) │
└────────┬────────┘

┌────┴────┐
│ │
▼ ▼
┌───────┐ ┌─────────┐
│ FCM │ │Postgres │
│(Push) │ │(Storage)│
└───┬───┘ └─────────┘


┌─────────────────┐
│ Mobile App │
│ (Push Notification)
└─────────────────┘

3. Báo cáo sự cố (Incident Flow)

┌─────────────────┐
│ Mobile App │
│ (Chụp ảnh + │
│ GPS + Mô tả) │
└────────┬────────┘
│ POST /incidents

┌─────────────────┐
│ Incident Module │
│ (Backend) │
└────────┬────────┘

┌────┴────┐
│ │
▼ ▼
┌───────┐ ┌─────────┐
│ MinIO │ │Postgres │
│(Photos) │(Metadata)
└───────┘ └─────────┘


┌─────────────────┐
│ Admin Dashboard │
│(Xem & Xử lý) │
└─────────────────┘

Các thành phần chi tiết

Backend (NestJS)

backend/src/
├── main.ts # Entry point
├── app.module.ts # Root module
├── config/ # Configuration
│ ├── app.config.ts
│ ├── database.config.ts
│ ├── firebase.config.ts
│ ├── google.config.ts
│ ├── jwt.config.ts
│ ├── minio.config.ts
│ ├── openweathermap.config.ts
│ └── orion.config.ts
├── modules/
│ ├── air-quality/ # Air Quality Data
│ ├── alert/ # Alert System
│ ├── auth/ # JWT Authentication
│ ├── dashboard/ # Dashboard Statistics
│ ├── file/ # File Upload
│ ├── incident/ # Incident Reports
│ ├── ingestion/ # Data Ingestion
│ ├── notification/ # Push Notifications
│ ├── persistence/ # Data Persistence
│ ├── reports/ # Reports & Export
│ ├── stations/ # Station Management
│ ├── user/ # User Management
│ └── weather/ # Weather Data
└── common/
├── decorators/
├── guards/ # RBAC Guards
└── interceptors/

Web Dashboard (Next.js)

web/src/
├── app/ # App Router
│ ├── dashboard/ # Dashboard pages
│ ├── alerts/ # Alert management
│ ├── incidents/ # Incident management
│ └── reports/ # Reports & Export
├── components/
│ ├── ui/ # shadcn/ui components
│ ├── charts/ # Recharts components
│ └── map/ # Leaflet/MapLibre
└── services/
└── api.ts # API client

Mobile App (Expo)

mobile/
├── app/ # Expo Router
│ ├── (tabs)/ # Tab navigation
│ ├── login.tsx # Auth screen
│ └── +not-found.tsx
├── components/
│ ├── AlertCard.tsx
│ ├── EnvCard.tsx
│ └── MapView/
├── services/
│ └── api.ts
└── store/ # Zustand state

Database Schema

PostgreSQL Tables

TableMô tả
usersThông tin người dùng (admin, manager, citizen)
alertsCảnh báo thiên tai, môi trường
incidentsBáo cáo sự cố từ người dân
stationsTrạm quan trắc
weather_historyLịch sử dữ liệu thời tiết
air_quality_historyLịch sử dữ liệu chất lượng không khí

MongoDB Collections (Orion-LD)

CollectionEntity TypeMô tả
entitiesWeatherObservedDữ liệu thời tiết real-time
entitiesWeatherForecastDự báo thời tiết 7 ngày
entitiesAirQualityObservedDữ liệu chất lượng không khí
entitiesAirQualityForecastDự báo chất lượng không khí

Tích hợp FIWARE

NGSI-LD Context Broker

Smart Forecast sử dụng Orion-LD làm Context Broker để quản lý dữ liệu môi trường theo chuẩn NGSI-LD.

Tại sao dùng NGSI-LD?

  • Tiêu chuẩn ETSI - Chuẩn quốc tế cho dữ liệu ngữ cảnh
  • Linked Data - Dữ liệu liên kết với JSON-LD
  • Smart Data Models - Sử dụng data models chuẩn của FIWARE
  • Interoperability - Dễ dàng tích hợp với các hệ thống khác

Smart Data Models sử dụng

ModelMô tả
WeatherObservedDữ liệu thời tiết (nhiệt độ, độ ẩm, gió...)
WeatherForecastDự báo thời tiết (7 ngày)
AirQualityObservedChất lượng không khí (AQI, PM2.5, PM10...)
AirQualityForecastDự báo chất lượng không khí (4 ngày)

Authentication & Authorization

JWT Authentication

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│ Client │────►│ Backend │────►│ Database │
│ │ │ (JWT Auth) │ │ │
└─────────────┘ └─────────────┘ └─────────────┘

Role-Based Access Control (RBAC)

RoleQuyền hạn
ADMINToàn quyền quản lý hệ thống
MANAGERQuản lý alerts, incidents, xem reports
CITIZENXem dữ liệu, nhận cảnh báo, gửi incidents

Push Notifications

Firebase Cloud Messaging (FCM)

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│ Backend │────►│ Firebase │────►│ Mobile App │
│ │ │ FCM │ │ │
└─────────────┘ └─────────────┘ └─────────────┘

Loại thông báo

  • Alert Notifications - Cảnh báo thiên tai, môi trường
  • Incident Updates - Cập nhật trạng thái sự cố
  • System Notifications - Thông báo hệ thống

Tiếp theo