# karansarda-cell/smart-home-cade architecture

> Architecture diagram of the GitHub repository karansarda-cell/smart-home-cade, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/karansarda-cell/smart-home-cade
- Repository: https://github.com/karansarda-cell/smart-home-cade

## Overview

CADE receives classroom telemetry over MQTT, filters and evaluates it, applies safety/mode priorities, publishes actuator commands, and records events for API logs and metrics. A separate data-preparation and Random Forest training workflow produces the model used by runtime predictions. The README also describes the FastAPI/MQTT product; source excerpts expose the runtime decisions and training path. The tree and imports disagree on anomaly-filter and model module names, so those links are omitted where their implementation cannot be matched confidently.

Main entry points: [`backend/app/mqtt_client.py`](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/mqtt_client.py) (MQTT handler), [`backend/app/main.py`](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/main.py) (FastAPI endpoints), [`backend/app/database.py`](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/database.py) (Database sessions), [`backend/app/ml_model.py`](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/ml_model.py) (Hazard predictions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Telemetry decisions"]
  node_mqtt_handler["MQTT handler<br/>[mqtt_client.py]"]
  node_anomaly_filter["Anomaly filter<br/>[anomly_filter.py]"]
  node_fuzzy_engine["Fuzzy decisions<br/>[fuzzy_engine.py]"]
  node_conflict_resolver["Priority overrides"]
  node_settings["Runtime settings<br/>[config.py]"]
  node_hazard_model["Hazard predictions<br/>[ml_model.py]"]
end

subgraph group_api["Backend API"]
  node_fastapi["FastAPI endpoints<br/>[main.py]"]
end

subgraph group_storage["Event storage"]
  node_audit_model["Audit event model<br/>[model.py]"]
  node_database[("Database sessions<br/>[database.py]")]
end

subgraph group_mlops["Model data and training"]
  node_dataset_builder["Dataset preparation"]
  node_dataset_files["Prepared datasets"]
  node_db_export["Telemetry export<br/>[export_db_data.py]"]
  node_model_training["Random Forest training<br/>[train_model.py]"]
  node_model_artifact["Trained model<br/>[cade_ml_model.pkl]"]
end

node_sensors(("Classroom sensors"))
node_mqtt_broker{{"MQTT broker"}}
node_actuators(("Classroom actuators"))
node_operator(("API operator"))

node_sensors -->|"publish telemetry"| node_mqtt_broker
node_mqtt_broker -->|"deliver telemetry"| node_mqtt_handler
node_mqtt_handler -->|"filter readings"| node_anomaly_filter
node_mqtt_handler -->|"evaluate readings"| node_fuzzy_engine
node_mqtt_handler -->|"resolve priorities"| node_conflict_resolver
node_mqtt_handler -->|"predict hazards"| node_hazard_model
node_mqtt_handler -->|"publish commands"| node_mqtt_broker
node_mqtt_broker -->|"deliver commands"| node_actuators
node_mqtt_handler -->|"read topics"| node_settings
node_fastapi -->|"start service"| node_mqtt_handler
node_operator -->|"request endpoints"| node_fastapi
node_fastapi -->|"load and predict"| node_hazard_model
node_fastapi -->|"query logs"| node_database
node_mqtt_handler -->|"save events"| node_database
node_mqtt_handler -->|"create audit event"| node_audit_model
node_fastapi -.->|"query audit events"| node_audit_model
node_audit_model -->|"map to tables"| node_database
node_dataset_builder -->|"write datasets"| node_dataset_files
node_db_export -->|"export telemetry"| node_dataset_files
node_database -.->|"supply telemetry"| node_db_export
node_dataset_files -->|"provide training data"| node_model_training
node_model_training -->|"save model"| node_model_artifact
node_hazard_model -->|"load model"| node_model_artifact

click node_mqtt_handler "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/mqtt_client.py"
click node_anomaly_filter "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/anomly_filter.py"
click node_fuzzy_engine "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/fuzzy_engine.py"
click node_conflict_resolver "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/conflict_resolver.py"
click node_settings "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/config.py"
click node_fastapi "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/main.py"
click node_audit_model "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/model.py"
click node_database "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/database.py"
click node_dataset_builder "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/download_dataset.py"
click node_dataset_files "https://github.com/karansarda-cell/smart-home-cade/tree/main/backend/data/processed"
click node_db_export "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/export_db_data.py"
click node_model_training "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/train_model.py"
click node_model_artifact "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/cade_ml_model.pkl"
click node_hazard_model "https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/ml_model.py"

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_mqtt_handler,node_anomaly_filter,node_fuzzy_engine,node_conflict_resolver,node_settings,node_hazard_model toneBlue
class node_fastapi toneAmber
class node_audit_model,node_database,node_operator toneMint
class node_dataset_builder,node_dataset_files,node_db_export,node_model_training,node_model_artifact toneRose
class node_sensors,node_mqtt_broker,node_actuators toneIndigo
```

## Components

### Telemetry decisions

- [MQTT handler](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/mqtt_client.py): `backend/app/mqtt_client.py`
- [Anomaly filter](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/anomly_filter.py): `backend/app/anomly_filter.py`
- [Fuzzy decisions](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/fuzzy_engine.py): `backend/app/fuzzy_engine.py`
- [Priority overrides](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/conflict_resolver.py): `backend/app/conflict_resolver.py`
- [Runtime settings](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/config.py): `backend/app/config.py`
- [Hazard predictions](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/ml_model.py): `backend/app/ml_model.py`

### Backend API

- [FastAPI endpoints](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/main.py): `backend/app/main.py`

### Event storage

- [Audit event model](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/model.py): `backend/app/model.py`
- [Database sessions](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/database.py): `backend/app/database.py`

### Model data and training

- [Dataset preparation](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/download_dataset.py): `backend/ml/download_dataset.py`
- [Prepared datasets](https://github.com/karansarda-cell/smart-home-cade/tree/main/backend/data/processed): `backend/data/processed`
- [Telemetry export](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/export_db_data.py): `backend/ml/export_db_data.py`
- [Random Forest training](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/ml/train_model.py): `backend/ml/train_model.py`
- [Trained model](https://github.com/karansarda-cell/smart-home-cade/blob/main/backend/app/cade_ml_model.pkl): `backend/app/cade_ml_model.pkl`

### Other components

- **Classroom sensors**
- **MQTT broker**
- **Classroom actuators**
- **API operator**

## Connections

- Classroom sensors → MQTT broker: publish telemetry (evidence: `README.md`)
- MQTT broker → MQTT handler: deliver telemetry (evidence: `backend/app/mqtt_client.py`)
- MQTT handler → Anomaly filter: filter readings
- MQTT handler → Fuzzy decisions: evaluate readings (evidence: `backend/app/mqtt_client.py`)
- MQTT handler → Priority overrides: resolve priorities (evidence: `backend/app/mqtt_client.py`)
- MQTT handler → Hazard predictions: predict hazards (evidence: `backend/app/mqtt_client.py`)
- MQTT handler → MQTT broker: publish commands (evidence: `backend/app/mqtt_client.py`)
- MQTT broker → Classroom actuators: deliver commands (evidence: `README.md`)
- MQTT handler → Runtime settings: read topics (evidence: `backend/app/mqtt_client.py`)
- FastAPI endpoints → MQTT handler: start service (evidence: `backend/app/main.py`)
- API operator → FastAPI endpoints: request endpoints (evidence: `backend/app/main.py`)
- FastAPI endpoints → Hazard predictions: load and predict (evidence: `backend/app/main.py`)
- FastAPI endpoints → Database sessions: query logs (evidence: `backend/app/main.py`)
- MQTT handler → Database sessions: save events (evidence: `backend/app/mqtt_client.py`)
- MQTT handler → Audit event model: create audit event (evidence: `backend/app/mqtt_client.py`)
- FastAPI endpoints → Audit event model: query audit events
- Audit event model → Database sessions: map to tables (evidence: `backend/app/model.py`)
- Dataset preparation → Prepared datasets: write datasets (evidence: `backend/ml/download_dataset.py`)
- Telemetry export → Prepared datasets: export telemetry (evidence: `backend/ml/export_db_data.py`)
- Database sessions → Telemetry export: supply telemetry
- Prepared datasets → Random Forest training: provide training data (evidence: `backend/ml/train_model.py`)
- Random Forest training → Trained model: save model (evidence: `backend/ml/train_model.py`)
- Hazard predictions → Trained model: load model (evidence: `backend/app/ml_model.py`)
