# sumitzanje/riverguardianai architecture

> Architecture diagram of the GitHub repository sumitzanje/riverguardianai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sumitzanje/riverguardianai
- Repository: https://github.com/sumitzanje/riverguardianai

## Overview

#### Purpose and Entry Points
RiverGuardianAI is an edge-first flood-access prediction system for Arduino UNO Q: STM32 firmware owns sensor/actuator interaction, while Linux-side Python performs local intelligence and resilient telemetry handling. The canonical device entry point is `python/main_runtime.py`, which launches `python/riverguardian_runtime.py`. Firmware is rooted at `sketch/sketch.ino`.

#### Device Runtime Orchestration
`python/riverguardian_runtime.py` coordinates recurring collection, inference, persistence, upload, and optional direct WhatsApp delivery. Runtime settings are loaded from a device-local settings file plus environment overrides through `python/config_loader.py` and `python/runtime_config.py`. Deployment installs this as a system service via `deploy/riverguardian.service`.

#### Sensor and Actuator Boundary
The MCU/firmware boundary is the rugged hardware interface: firmware reads and filters physical measurements and supports bench versus production filter profiles in `sketch/sketch.ino`. Python-side bridge modules isolate MCU/sensor communications from decision logic: `python/bridge_interface.py`, `python/bridge_Int_Senor.py`, and `python/actuator_controller.py`. Hardware-specific RS485 and cellular behavior remains deployment-dependent.

#### Intelligence Pipeline
The runtime transforms sensor observations and weather context into risk and action decisions. Weather acquisition and fusion are handled by `python/weather_fetcher.py` and `python/weather_fusion_engine.py`; prediction/assessment stages are represented by `python/confidence_engine.py`, `python/risk_engine.py`, and `python/recommendation_engine.py`. This separation preserves the distinction between measurement confidence, flood risk state, and recommended response.

#### State, Alerts, and Local Resilience
Each cycle persists locally before cloud dependence through `python/local_database.py`. `python/alert_manager.py` derives send eligibility from risk transitions and cooldowns, so notification failures do not halt sensing or risk processing. Standalone operational entry points include `python/main_alert.py`, `python/main_database.py`, and `python/main_confidence.py`.

#### Cloud Telemetry and Notifications
Telemetry can upload directly to Supabase through `python/api_uploader.py`; schema starts at `supabase/001_riverguardian_events.sql` and is extended by `supabase/migrations/20260806120000_sensor_observability_fields.sql`. Direct device WhatsApp delivery is encapsulated by `python/whatsapp_sender.py`. An optional trusted notifier worker, `python/main_alert_notifier.py` and `python/supabase_alert_notifier.py`, polls cloud alert events and checkpoints its progress locally.

#### Supabase Edge and Dashboard Surfaces
Supabase Edge Functions keep Telegram credentials and privileged data access off the device. `supabase/functions/telegram-webhook/index.ts` serves bot commands/webhooks, while `supabase/functions/alert-dispatcher/index.ts` processes inserted alert events for proactive delivery; subscriber/delivery state is defined in `supabase/migrations/20260801171050_telegram_bot_platform.sql`. A Next.js dashboard consumes RiverGuardian data through `web-dashboard/src/app/page.tsx` and its server-side API route `web-dashboard/src/app/api/riverguardian/route.ts`. The separate `dashboard/dashboard_app.py` is an additional Python dashboard entry point.

#### Infrastructure and Operations
Production device deployment is manually tag-approved, with service installation and layout migration under `deploy/install-deployment.sh` and `deploy/riverguardian-migrate-layout`. Connectivity resilience is supported by `python/connectivity_manager.py` plus the network failover systemd unit/timer files under `deploy/`. Supabase, Ambient Weather, Telegram, Twilio/webhook WhatsApp, and optionally Tailscale are the material external integrations.

Main entry points: [`python/riverguardian_runtime.py`](https://github.com/sumitzanje/riverguardianai/blob/main/python/riverguardian_runtime.py) (Runtime Orchestrator), [`supabase/001_riverguardian_events.sql`](https://github.com/sumitzanje/riverguardianai/blob/main/supabase/001_riverguardian_events.sql) (RiverGuardian Data & Events), [`python/risk_engine.py`](https://github.com/sumitzanje/riverguardianai/blob/main/python/risk_engine.py) (Flood Risk Engine), [`python/api_uploader.py`](https://github.com/sumitzanje/riverguardianai/blob/main/python/api_uploader.py) (Telemetry Uploader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_device["Edge Device"]
  node_firmware{{"STM32 Firmware<br/>Arduino sketch<br/>[sketch.ino]"}}
  node_bridge["Hardware Bridge<br/>Python bridge"]
  node_actuators["Actuator Control<br/>Python controller"]
  node_entrypoint["Device Entry Point<br/>Python launcher<br/>[main_runtime.py]"]
  node_runtime["Runtime Orchestrator<br/>Python runtime"]
  node_config["Runtime Configuration<br/>Python config<br/>[config_loader.py]"]
  node_weather["Weather Fusion<br/>Python pipeline"]
  node_confidence["Measurement Confidence<br/>Python engine"]
  node_risk["Flood Risk Engine<br/>Python engine<br/>[risk_engine.py]"]
  node_recommendations["Response Recommendations<br/>Python engine"]
  node_localdb[("Local State Store<br/>Local database<br/>[local_database.py]")]
  node_alerts["Alert Manager<br/>Python alert policy<br/>[alert_manager.py]"]
  node_uploader["Telemetry Uploader<br/>Supabase client<br/>[api_uploader.py]"]
  node_whatsapp["Device WhatsApp Sender<br/>Python notifier<br/>[whatsapp_sender.py]"]
  node_connectivity["Connectivity Manager<br/>Python failover"]
end

subgraph group_cloud["Supabase Cloud"]
  node_supabase[("RiverGuardian Data &amp; Events<br/>Supabase database")]
  node_dispatcher["Alert Dispatcher<br/>Supabase Edge Function<br/>[index.ts]"]
  node_telegram["Telegram Webhook<br/>Supabase Edge Function<br/>[index.ts]"]
  node_notifier["Trusted Alert Notifier<br/>Python worker"]
end

subgraph group_clients["Dashboard &amp; Operations"]
  node_webdashboard["Next.js Dashboard<br/>Web dashboard<br/>[page.tsx]"]
  node_dashboardapi["Dashboard API Route<br/>Next.js server route<br/>[route.ts]"]
  node_deployment["Device Deployment<br/>Systemd deployment"]
end

node_firmware -->|"sensor and MCU I/O"| node_bridge
node_bridge -->|"observations"| node_runtime
node_runtime -->|"action commands"| node_actuators
node_entrypoint -->|"launches"| node_runtime
node_config -->|"settings"| node_runtime
node_runtime -->|"weather context"| node_weather
node_runtime -->|"measurements"| node_confidence
node_weather -->|"fused context"| node_risk
node_confidence -->|"confidence"| node_risk
node_risk -->|"risk state"| node_recommendations
node_runtime -->|"persist cycle"| node_localdb
node_risk -->|"risk transitions"| node_alerts
node_alerts -->|"eligible direct alerts"| node_whatsapp
node_runtime -->|"telemetry"| node_uploader
node_connectivity -.->|"network availability"| node_uploader
node_uploader -->|"upload events"| node_supabase
node_supabase -->|"inserted alert events"| node_dispatcher
node_supabase -.->|"polled alerts"| node_notifier
node_telegram -->|"subscriber and delivery state"| node_supabase
node_webdashboard -->|"data requests"| node_dashboardapi
node_dashboardapi -->|"server-side data access"| node_supabase
node_deployment -->|"installs service for"| node_entrypoint

click node_firmware "https://github.com/sumitzanje/riverguardianai/blob/main/sketch/sketch.ino"
click node_bridge "https://github.com/sumitzanje/riverguardianai/blob/main/python/bridge_interface.py"
click node_actuators "https://github.com/sumitzanje/riverguardianai/blob/main/python/actuator_controller.py"
click node_entrypoint "https://github.com/sumitzanje/riverguardianai/blob/main/python/main_runtime.py"
click node_runtime "https://github.com/sumitzanje/riverguardianai/blob/main/python/riverguardian_runtime.py"
click node_config "https://github.com/sumitzanje/riverguardianai/blob/main/python/config_loader.py"
click node_weather "https://github.com/sumitzanje/riverguardianai/blob/main/python/weather_fusion_engine.py"
click node_confidence "https://github.com/sumitzanje/riverguardianai/blob/main/python/confidence_engine.py"
click node_risk "https://github.com/sumitzanje/riverguardianai/blob/main/python/risk_engine.py"
click node_recommendations "https://github.com/sumitzanje/riverguardianai/blob/main/python/recommendation_engine.py"
click node_localdb "https://github.com/sumitzanje/riverguardianai/blob/main/python/local_database.py"
click node_alerts "https://github.com/sumitzanje/riverguardianai/blob/main/python/alert_manager.py"
click node_uploader "https://github.com/sumitzanje/riverguardianai/blob/main/python/api_uploader.py"
click node_whatsapp "https://github.com/sumitzanje/riverguardianai/blob/main/python/whatsapp_sender.py"
click node_connectivity "https://github.com/sumitzanje/riverguardianai/blob/main/python/connectivity_manager.py"
click node_supabase "https://github.com/sumitzanje/riverguardianai/blob/main/supabase/001_riverguardian_events.sql"
click node_dispatcher "https://github.com/sumitzanje/riverguardianai/blob/main/supabase/functions/alert-dispatcher/index.ts"
click node_telegram "https://github.com/sumitzanje/riverguardianai/blob/main/supabase/functions/telegram-webhook/index.ts"
click node_notifier "https://github.com/sumitzanje/riverguardianai/blob/main/python/main_alert_notifier.py"
click node_webdashboard "https://github.com/sumitzanje/riverguardianai/blob/main/web-dashboard/src/app/page.tsx"
click node_dashboardapi "https://github.com/sumitzanje/riverguardianai/blob/main/web-dashboard/src/app/api/riverguardian/route.ts"
click node_deployment "https://github.com/sumitzanje/riverguardianai/blob/main/deploy/install-deployment.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_firmware,node_bridge,node_actuators,node_entrypoint,node_runtime,node_config,node_weather,node_confidence,node_risk,node_recommendations,node_localdb,node_alerts,node_uploader,node_whatsapp,node_connectivity toneBlue
class node_supabase,node_dispatcher,node_telegram,node_notifier toneAmber
class node_webdashboard,node_dashboardapi,node_deployment toneMint
```

## Components

### Edge Device

Arduino UNO Q firmware and local Linux runtime.

- [STM32 Firmware](https://github.com/sumitzanje/riverguardianai/blob/main/sketch/sketch.ino): Arduino sketch · Reads, filters, and controls physical hardware. · `sketch/sketch.ino`
- [Hardware Bridge](https://github.com/sumitzanje/riverguardianai/blob/main/python/bridge_interface.py): Python bridge · Isolates MCU and sensor communication from decisions. · `python/bridge_interface.py`
- [Actuator Control](https://github.com/sumitzanje/riverguardianai/blob/main/python/actuator_controller.py): Python controller · `python/actuator_controller.py`
- [Device Entry Point](https://github.com/sumitzanje/riverguardianai/blob/main/python/main_runtime.py): Python launcher · Starts the canonical device runtime. · `python/main_runtime.py`
- [Runtime Orchestrator](https://github.com/sumitzanje/riverguardianai/blob/main/python/riverguardian_runtime.py): Python runtime · Runs recurring collection, decisions, persistence, and delivery. · `python/riverguardian_runtime.py`
- [Runtime Configuration](https://github.com/sumitzanje/riverguardianai/blob/main/python/config_loader.py): Python config · Combines local settings with environment overrides. · `python/config_loader.py`
- [Weather Fusion](https://github.com/sumitzanje/riverguardianai/blob/main/python/weather_fusion_engine.py): Python pipeline · Acquires weather context and fuses it with observations. · `python/weather_fusion_engine.py`
- [Measurement Confidence](https://github.com/sumitzanje/riverguardianai/blob/main/python/confidence_engine.py): Python engine · `python/confidence_engine.py`
- [Flood Risk Engine](https://github.com/sumitzanje/riverguardianai/blob/main/python/risk_engine.py): Python engine · `python/risk_engine.py`
- [Response Recommendations](https://github.com/sumitzanje/riverguardianai/blob/main/python/recommendation_engine.py): Python engine · `python/recommendation_engine.py`
- [Local State Store](https://github.com/sumitzanje/riverguardianai/blob/main/python/local_database.py): Local database · Persists cycles before cloud dependence. · `python/local_database.py`
- [Alert Manager](https://github.com/sumitzanje/riverguardianai/blob/main/python/alert_manager.py): Python alert policy · Applies risk transitions and cooldown eligibility. · `python/alert_manager.py`
- [Telemetry Uploader](https://github.com/sumitzanje/riverguardianai/blob/main/python/api_uploader.py): Supabase client · `python/api_uploader.py`
- [Device WhatsApp Sender](https://github.com/sumitzanje/riverguardianai/blob/main/python/whatsapp_sender.py): Python notifier · `python/whatsapp_sender.py`
- [Connectivity Manager](https://github.com/sumitzanje/riverguardianai/blob/main/python/connectivity_manager.py): Python failover · Supports resilient network connectivity. · `python/connectivity_manager.py`

### Supabase Cloud

Telemetry, alert automation, and bot surfaces.

- [RiverGuardian Data & Events](https://github.com/sumitzanje/riverguardianai/blob/main/supabase/001_riverguardian_events.sql): Supabase database · Stores telemetry and alert events. · `supabase/001_riverguardian_events.sql`
- [Alert Dispatcher](https://github.com/sumitzanje/riverguardianai/blob/main/supabase/functions/alert-dispatcher/index.ts): Supabase Edge Function · Processes inserted alert events for proactive delivery. · `supabase/functions/alert-dispatcher/index.ts`
- [Telegram Webhook](https://github.com/sumitzanje/riverguardianai/blob/main/supabase/functions/telegram-webhook/index.ts): Supabase Edge Function · Handles bot commands without exposing credentials to devices. · `supabase/functions/telegram-webhook/index.ts`
- [Trusted Alert Notifier](https://github.com/sumitzanje/riverguardianai/blob/main/python/main_alert_notifier.py): Python worker · Polls cloud alert events and checkpoints locally. · `python/main_alert_notifier.py`

### Dashboard & Operations

Operator interfaces and deployment support.

- [Next.js Dashboard](https://github.com/sumitzanje/riverguardianai/blob/main/web-dashboard/src/app/page.tsx): Web dashboard · Displays RiverGuardian data to operators. · `web-dashboard/src/app/page.tsx`
- [Dashboard API Route](https://github.com/sumitzanje/riverguardianai/blob/main/web-dashboard/src/app/api/riverguardian/route.ts): Next.js server route · `web-dashboard/src/app/api/riverguardian/route.ts`
- [Device Deployment](https://github.com/sumitzanje/riverguardianai/blob/main/deploy/install-deployment.sh): Systemd deployment · Installs the tagged-approved runtime service. · `deploy/install-deployment.sh`

## Connections

- STM32 Firmware → Hardware Bridge: sensor and MCU I/O
- Hardware Bridge → Runtime Orchestrator: observations
- Runtime Orchestrator → Actuator Control: action commands
- Device Entry Point → Runtime Orchestrator: launches
- Runtime Configuration → Runtime Orchestrator: settings
- Runtime Orchestrator → Weather Fusion: weather context
- Runtime Orchestrator → Measurement Confidence: measurements
- Weather Fusion → Flood Risk Engine: fused context
- Measurement Confidence → Flood Risk Engine: confidence
- Flood Risk Engine → Response Recommendations: risk state
- Runtime Orchestrator → Local State Store: persist cycle
- Flood Risk Engine → Alert Manager: risk transitions
- Alert Manager → Device WhatsApp Sender: eligible direct alerts
- Runtime Orchestrator → Telemetry Uploader: telemetry
- Connectivity Manager → Telemetry Uploader: network availability
- Telemetry Uploader → RiverGuardian Data & Events: upload events
- RiverGuardian Data & Events → Alert Dispatcher: inserted alert events
- RiverGuardian Data & Events → Trusted Alert Notifier: polled alerts
- Telegram Webhook → RiverGuardian Data & Events: subscriber and delivery state
- Next.js Dashboard → Dashboard API Route: data requests
- Dashboard API Route → RiverGuardian Data & Events: server-side data access
- Device Deployment → Device Entry Point: installs service for
