# khuongkayyyyy/green_fairm architecture

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

- Interactive diagram: https://gitdiagram.com/khuongkayyyyy/green_fairm
- Repository: https://github.com/khuongkayyyyy/green_fairm

## Overview

##### Project type and purpose
- This is a full-stack, cross-platform farm management application.
- Frontend is a Flutter app (mobile, web, desktop).
- Backend is implemented as Firebase services (Authentication, Firestore, Cloud Functions) plus external integrations (MQTT sensor network, Weather API, custom ML).
- It provides real-time environmental monitoring, satellite imagery, AI-driven recommendations, and water-usage forecasting.

##### File structure
- Top-level “android/”, “ios/”, “web/”, “linux/”, “macos/”, “windows/” directories → Flutter platform targets.
- “lib/” → Flutter app code, organized by Clean Architecture:
- core/ (constants, services, utilities)
- data/ (models, repositories)
- presentation/ (BLoC state management, views)
- main.dart
- “functions/” → Node.js Cloud Functions (backend logic, REST endpoints, triggers).
- “assets/” and “public/” → static images, hosting assets.
- Firebase config files: firebase.json, storage.rules, remoteconfig.template.json, apphosting.yaml.
- Third-party configs: mqtt.dart integration under presentation/view/mqtt.dart.

##### Extract architecture hints from README
- Tech stack: Flutter frontend; NestJS (implied) or Node.js functions; Firebase Authentication; MongoDB (if used by backend); external Weather API; MQTT broker.
- Features listing confirms real-time data flow (MQTT → app), REST/API calls (Weather API), ML/AI inference (Gemini/ML model).

a. Frontend (Flutter app)
- Presentation layer (Widgets + BLoC)
- Data layer (Repositories)
- Core services (FirestoreService, LocalStorageService, NotiService)
b. Backend services
- Firebase Authentication
- Firebase Firestore (NoSQL database)
- Firebase Cloud Functions (Node.js / NestJS)
- (Optional) MongoDB if external to Firestore
c. External systems
- MQTT Broker (sensor network)
- Weather API provider
- ML/AI service (Gemini assistant or custom TensorFlow model)
- Satellite imagery service
d. Deployment & hosting
- Firebase Hosting (web assets)
- Platform-specific app stores (Android/iOS stores, desktop installers)

##### Relationships and interactions
- Flutter → Firebase Auth: sign-in/up, token-based calls.
- Flutter → Firestore: CRUD operations for user, field, sensor, water history.
- Flutter → Cloud Functions: HTTP triggers for machine-learning inference, notifications, satellite imagery requests.
- MQTT Broker ↔ Cloud Functions (or Flutter directly): sensor publishes telemetry → Functions subscribe or app subscribes.
- Cloud Functions → External Weather API: HTTP REST calls for forecast data.
- Cloud Functions → ML Model: invoke prediction endpoint or embedded logic.
- Flutter ← Remote Config: feature flags, app settings.

##### Architectural patterns & principles
- Clean Architecture in Flutter: separation of UI (presentation), domain/data, infrastructure.
- BLoC for state management → event-state mapping.
- Publish/Subscribe for real-time MQTT data.
- Serverless microservices using Cloud Functions.
- Layered approach: UI ↔ BLoC ↔ Repository ↔ Service ↔ Backend.

- Deployments (mobile devices, Firebase region, third-party API).
- Cross-platform nature: mobile, web, desktop targets all point to the same backend.
- You capture Clean Architecture layers inside the Flutter container.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Flutter App" 
        direction TB
        subgraph "Presentation Layer" 
            direction TB
            Presentation["Presentation Layer (Widgets + BLoC)"]:::frontend
            subgraph "BLoC Components"
                direction TB
                AuthBloc["AuthenticationBloc"]:::frontend
                WeatherBloc["WeatherBloc"]:::frontend
                MLBloc["MachineLearningBloc"]:::frontend
            end
            subgraph "Views"
                direction TB
                MQTTView["mqtt.dart (MQTT Integration)"]:::frontend
                SatelliteView["farm_from_satelite.dart"]:::frontend
            end
        end
        subgraph "Data Layer" 
            direction TB
            DataLayer["Repositories"]:::frontend
            WeatherRepo["weather_repository.dart"]:::frontend
            MLRepo["machine_learning_repository.dart"]:::frontend
        end
        subgraph "Core Services"
            direction TB
            FirestoreService["FirestoreService"]:::frontend
            LocalStorageService["LocalStorageService"]:::frontend
            NotiService["NotiService"]:::frontend
        end
        main["main.dart"]:::frontend
    end

    %% Backend Layer
    subgraph "Firebase Backend"
        direction TB
        AuthService["Authentication Service"]:::backend
        FirestoreDB["Firestore Database"]:::backend
        subgraph "Cloud Functions"
            direction TB
            FunctionsIndex["index.js (Functions)"]:::backend
            FunctionsPkg["package.json"]:::backend
        end
    end

    %% External Integrations
    subgraph "External Services"
        direction TB
        MQTTBroker["MQTT Broker"]:::external
        WeatherAPI["Weather API"]:::external
        MLService["ML/AI Service"]:::external
        SatelliteService["Satellite Imagery Service"]:::external
    end

    %% Deployment Targets
    subgraph "Deployment & Hosting"
        direction TB
        FirebaseHosting["Firebase Hosting"]:::deployment
        StaticAssets["public/"]:::deployment
        HostingConfig["firebase.json / apphosting.yaml"]:::deployment
        subgraph "App Platforms"
            direction TB
            Android["Android"]:::deployment
            iOS["iOS"]:::deployment
            Web["Web"]:::deployment
            Linux["Linux"]:::deployment
            MacOS["macOS"]:::deployment
            Windows["Windows"]:::deployment
        end
    end

    %% Interactions
    main -->|"uses"| Presentation
    Presentation -->|"dispatches events"| AuthBloc
    Presentation -->|"dispatches events"| WeatherBloc
    Presentation -->|"dispatches events"| MLBloc
    Presentation -->|"opens view"| MQTTView
    Presentation -->|"opens view"| SatelliteView

    AuthBloc -->|"calls"| FirestoreService
    WeatherBloc -->|"calls"| WeatherRepo
    MLBloc -->|"calls"| MLRepo

    WeatherRepo -->|"HTTP REST"| WeatherAPI
    MLRepo -->|"HTTP REST"| MLService
    MQTTView -->|"MQTT subscribe"| MQTTBroker
    SatelliteView -->|"HTTP REST"| SatelliteService

    FirestoreService -->|"Firestore read/write"| FirestoreDB
    LocalStorageService -->|"local storage"| main
    NotiService -->|"local notifications"| main

    main -->|"Auth token"| AuthService
    main -->|"Firestore CRUD"| FirestoreDB
    main -->|"HTTP REST"| FunctionsIndex

    FunctionsIndex -->|"triggers ML/imagery"| MLService
    FunctionsIndex -->|"calls Weather API"| WeatherAPI
    FunctionsIndex -->|"reads/writes"| FirestoreDB
    FunctionsIndex -->|"publishes events"| NotiService

    FunctionsIndex -->|"MQTT subscribe"| MQTTBroker

    %% Deployment flows
    main --> Android
    main --> iOS
    main --> Web
    main --> Linux
    main --> MacOS
    main --> Windows

    Web --> FirebaseHosting
    StaticAssets --> FirebaseHosting
    HostingConfig --> FirebaseHosting

    %% Click Events
    click Presentation "https://github.com/khuongkayyyyy/green_fairm/tree/main/lib/presentation"
    click DataLayer "https://github.com/khuongkayyyyy/green_fairm/tree/main/lib/data/res"
    click FirestoreService "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/core/service/firestore_service.dart"
    click LocalStorageService "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/core/service/local_storage_service.dart"
    click NotiService "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/core/service/noti_service.dart"
    click AuthBloc "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/presentation/bloc/authentication/authentication_bloc.dart"
    click WeatherBloc "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/presentation/bloc/weather/weather_bloc.dart"
    click MLBloc "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/presentation/bloc/machine_learning/ml_bloc.dart"
    click MQTTView "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/presentation/view/mqtt.dart"
    click SatelliteView "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/presentation/view/field_detail/widget/farm_from_satelite.dart"
    click FunctionsIndex "https://github.com/khuongkayyyyy/green_fairm/blob/main/functions/index.js"
    click FunctionsPkg "https://github.com/khuongkayyyyy/green_fairm/blob/main/functions/package.json"
    click StaticAssets "https://github.com/khuongkayyyyy/green_fairm/tree/main/public/"
    click HostingConfig "https://github.com/khuongkayyyyy/green_fairm/blob/main/apphosting.yaml"
    click WeatherRepo "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/data/res/weather_reposity.dart"
    click MLRepo "https://github.com/khuongkayyyyy/green_fairm/blob/main/lib/data/res/machine_learning_repository.dart"
    click Android "https://github.com/khuongkayyyyy/green_fairm/tree/main/android/"
    click iOS "https://github.com/khuongkayyyyy/green_fairm/tree/main/ios/"
    click Web "https://github.com/khuongkayyyyy/green_fairm/tree/main/web/"
    click Linux "https://github.com/khuongkayyyyy/green_fairm/tree/main/linux/"
    click MacOS "https://github.com/khuongkayyyyy/green_fairm/tree/main/macos/"
    click Windows "https://github.com/khuongkayyyyy/green_fairm/tree/main/windows/"

    %% Styles
    classDef frontend fill:#e0f3ff,stroke:#66b2ff,color:#003366;
    classDef backend fill:#e6ffe6,stroke:#33cc33,color:#004d00;
    classDef external fill:#fff0e6,stroke:#ff9933,color:#663300;
    classDef deployment fill:#f2f2f2,stroke:#888888,color:#333333;
```
