# amaanali70/assestsense architecture

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

- Interactive diagram: https://gitdiagram.com/amaanali70/assestsense
- Repository: https://github.com/amaanali70/assestsense

## Overview

#### Purpose and Entry Points
AssetSense is an IIoT predictive-maintenance system: ESP32 telemetry is analyzed server-side, persisted, broadcast to a React operations dashboard, and can trigger failover and email alerts. Frontend bootstraps at `src/main.jsx` into `src/App.jsx`; backend runtime starts at `server/index.js`; edge firmware starts at `code/code_1/code_1.ino`.

#### Edge Telemetry Ingestion
The ESP32 acquires temperature, vibration, and current readings, packages JSON, and publishes MQTT telemetry under `assetsense/nodes/{nodeId}`. The cloud boundary is MQTT: hardware remains a producer and does not directly access analytics, storage, or UI.
- `code/code_1/code_1.ino`
- `server/index.js`
- `physical_implementation.md`

#### Backend Event Orchestrator
Express, MQTT subscription, Socket.IO, REST alert dispatch, and operational safety logic converge in the backend process. It subscribes to node telemetry, enriches each reading, persists it, broadcasts `sensor_update` to browsers, and exposes manual alert dispatch (`POST /api/send-alert`).
- `server/index.js`
- `server/package.json`

#### Analytics and Decision Engine
Telemetry passes through online ML and rules-based health assessment: Isolation Forest anomaly scoring, Brain.js neural-network classification/training, EWMA drift tracking, health scoring, fault diagnosis, and linear-regression RUL estimation. This layer emits enriched status, fault, confidence, anomaly, and RUL fields used by downstream safety and presentation flows.
- `server/mlModel.js`
- `server/index.js`
- `server/generate_fig4.py`

#### Persistence and Notification Boundaries
Telemetry/event persistence prefers MongoDB through Mongoose and degrades to local JSON logs when unavailable. SMTP notifications use configured credentials and cooldowns, separating external delivery failure from telemetry handling. Required environment concerns include MongoDB URI, email credentials, and backend port.
- `server/database.js`
- `server/index.js`
- `.env`

#### Resilience and Closed-Loop Control
When an active asset health score crosses the critical threshold, backend control logic marks it unavailable and activates a standby spare, with cooldown/throttling intended to prevent oscillation and alert storms. Dashboard fault injection and repair actions support exercising and restoring this state machine rather than controlling physical PLC equipment directly.
- `server/index.js`
- `src/components/FaultControlPanel.jsx`
- `src/hooks/useSimulation.js`

#### Dashboard State and Operator Views
The Vite/React client maintains live socket-driven simulation/telemetry state in a central hook and coordinates views in the root app. Node cards, detail panels, charts, alerts, analytics, rankings, RUL, and system-status components render the enriched backend payload rather than performing core ML locally.
- `src/App.jsx`
- `src/hooks/useSimulation.js`
- `src/components/Analytics.jsx`

#### Runtime and External Infrastructure
Material runtimes are Node.js 18+, browser-based React 19/Vite, and optional ESP32/Arduino firmware. External services are a HiveMQ-compatible MQTT broker, MongoDB when configured, and an SMTP provider (such as Gmail); Socket.IO provides the server-to-browser real-time channel.

Main entry points: [`server/mlModel.js`](https://github.com/amaanali70/assestsense/blob/main/server/mlModel.js) (Health assessment), [`server/index.js`](https://github.com/amaanali70/assestsense/blob/main/server/index.js) (Backend orchestrator), [`src/hooks/useSimulation.js`](https://github.com/amaanali70/assestsense/blob/main/src/hooks/useSimulation.js) (Live telemetry state), [`server/database.js`](https://github.com/amaanali70/assestsense/blob/main/server/database.js) (Persistence adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge device"]
  node_firmware{{"ESP32 firmware<br/>Arduino telemetry producer<br/>[code_1.ino]"}}
end

subgraph group_backend["Backend runtime"]
  node_orchestrator["Backend orchestrator<br/>[index.js]"]
  node_analytics["Health assessment<br/>ML and rules engine<br/>[mlModel.js]"]
  node_safety["Failover controller<br/>asset safety state machine<br/>[index.js]"]
  node_persistence[("Persistence adapter<br/>Mongoose + JSON fallback<br/>[database.js]")]
  node_local_logs["Local JSON logs<br/>degraded event store<br/>[index.js]"]
  node_alert_api["Manual alert API<br/>POST /api/send-alert<br/>[index.js]"]
  node_socket["Socket.IO channel<br/>real-time browser delivery<br/>[index.js]"]
end

subgraph group_dashboard["Operations dashboard"]
  node_client_bootstrap["React bootstrap<br/>Vite browser entry<br/>[main.jsx]"]
  node_app["Dashboard app<br/>React view coordinator<br/>[App.jsx]"]
  node_live_state["Live telemetry state<br/>socket-driven simulation hook<br/>[useSimulation.js]"]
  node_analytics_view["Analytics view<br/>operator analytics UI<br/>[Analytics.jsx]"]
  node_fault_controls["Fault controls<br/>operator state controls"]
end

subgraph group_external["External infrastructure"]
  node_mqtt["MQTT broker<br/>HiveMQ-compatible broker"]
  node_mongodb[("MongoDB<br/>telemetry database")]
  node_smtp["SMTP provider<br/>email delivery service"]
end

node_firmware -->|"publishes node telemetry"| node_mqtt
node_mqtt -->|"subscribed telemetry"| node_orchestrator
node_orchestrator -->|"enriched reading"| node_analytics
node_analytics -->|"health and fault decision"| node_safety
node_analytics -->|"enriched event"| node_persistence
node_persistence -->|"primary storage"| node_mongodb
node_persistence -.->|"fallback when unavailable"| node_local_logs
node_analytics -->|"enriched status"| node_socket
node_safety -->|"asset availability state"| node_socket
node_orchestrator -->|"exposes dispatch endpoint"| node_alert_api
node_alert_api -->|"sends alert email"| node_smtp
node_client_bootstrap -->|"mounts"| node_app
node_app -->|"coordinates state"| node_live_state
node_socket -->|"sensor_update"| node_live_state
node_live_state -->|"enriched payload"| node_analytics_view
node_live_state -->|"simulation actions"| node_fault_controls
node_fault_controls -->|"fault injection and repair"| node_orchestrator

click node_firmware "https://github.com/amaanali70/assestsense/blob/main/code/code_1/code_1.ino"
click node_orchestrator "https://github.com/amaanali70/assestsense/blob/main/server/index.js"
click node_analytics "https://github.com/amaanali70/assestsense/blob/main/server/mlModel.js"
click node_safety "https://github.com/amaanali70/assestsense/blob/main/server/index.js"
click node_persistence "https://github.com/amaanali70/assestsense/blob/main/server/database.js"
click node_local_logs "https://github.com/amaanali70/assestsense/blob/main/server/index.js"
click node_alert_api "https://github.com/amaanali70/assestsense/blob/main/server/index.js"
click node_socket "https://github.com/amaanali70/assestsense/blob/main/server/index.js"
click node_client_bootstrap "https://github.com/amaanali70/assestsense/blob/main/src/main.jsx"
click node_app "https://github.com/amaanali70/assestsense/blob/main/src/App.jsx"
click node_live_state "https://github.com/amaanali70/assestsense/blob/main/src/hooks/useSimulation.js"
click node_analytics_view "https://github.com/amaanali70/assestsense/blob/main/src/components/Analytics.jsx"
click node_fault_controls "https://github.com/amaanali70/assestsense/blob/main/src/components/FaultControlPanel.jsx"

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 toneBlue
class node_orchestrator,node_analytics,node_safety,node_persistence,node_local_logs,node_alert_api,node_socket toneAmber
class node_client_bootstrap,node_app,node_live_state,node_analytics_view,node_fault_controls toneMint
class node_mqtt,node_mongodb,node_smtp toneRose
```

## Components

### Edge device

- [ESP32 firmware](https://github.com/amaanali70/assestsense/blob/main/code/code_1/code_1.ino): Arduino telemetry producer · Acquires temperature, vibration, and current readings. · `code/code_1/code_1.ino`

### Backend runtime

- [Backend orchestrator](https://github.com/amaanali70/assestsense/blob/main/server/index.js): Express + MQTT + Socket.IO · Ingests telemetry and coordinates API, safety, persistence, and live delivery. · `server/index.js`
- [Health assessment](https://github.com/amaanali70/assestsense/blob/main/server/mlModel.js): ML and rules engine · Produces anomaly, fault, confidence, health, and RUL fields. · `server/mlModel.js`
- [Failover controller](https://github.com/amaanali70/assestsense/blob/main/server/index.js): asset safety state machine · Marks critical assets unavailable and activates a standby spare. · `server/index.js`
- [Persistence adapter](https://github.com/amaanali70/assestsense/blob/main/server/database.js): Mongoose + JSON fallback · Uses MongoDB when available and local JSON logs otherwise. · `server/database.js`
- [Local JSON logs](https://github.com/amaanali70/assestsense/blob/main/server/index.js): degraded event store · `server/index.js`
- [Manual alert API](https://github.com/amaanali70/assestsense/blob/main/server/index.js): POST /api/send-alert · `server/index.js`
- [Socket.IO channel](https://github.com/amaanali70/assestsense/blob/main/server/index.js): real-time browser delivery · Emits sensor_update payloads. · `server/index.js`

### Operations dashboard

- [React bootstrap](https://github.com/amaanali70/assestsense/blob/main/src/main.jsx): Vite browser entry · `src/main.jsx`
- [Dashboard app](https://github.com/amaanali70/assestsense/blob/main/src/App.jsx): React view coordinator · `src/App.jsx`
- [Live telemetry state](https://github.com/amaanali70/assestsense/blob/main/src/hooks/useSimulation.js): socket-driven simulation hook · Owns live state plus fault injection and repair actions. · `src/hooks/useSimulation.js`
- [Analytics view](https://github.com/amaanali70/assestsense/blob/main/src/components/Analytics.jsx): operator analytics UI · Renders enriched server-side assessment results. · `src/components/Analytics.jsx`
- [Fault controls](https://github.com/amaanali70/assestsense/blob/main/src/components/FaultControlPanel.jsx): operator state controls · Exercises and restores the backend safety state machine. · `src/components/FaultControlPanel.jsx`

### External infrastructure

- **MQTT broker**: HiveMQ-compatible broker
- **MongoDB**: telemetry database · Optional primary event store.
- **SMTP provider**: email delivery service · Delivery is cooldown-throttled and isolated from telemetry handling.

## Connections

- ESP32 firmware → MQTT broker: publishes node telemetry · assetsense/nodes/{nodeId}
- MQTT broker → Backend orchestrator: subscribed telemetry
- Backend orchestrator → Health assessment: enriched reading
- Health assessment → Failover controller: health and fault decision
- Health assessment → Persistence adapter: enriched event
- Persistence adapter → MongoDB: primary storage
- Persistence adapter → Local JSON logs: fallback when unavailable
- Health assessment → Socket.IO channel: enriched status
- Failover controller → Socket.IO channel: asset availability state
- Backend orchestrator → Manual alert API: exposes dispatch endpoint
- Manual alert API → SMTP provider: sends alert email
- React bootstrap → Dashboard app: mounts
- Dashboard app → Live telemetry state: coordinates state
- Socket.IO channel → Live telemetry state: sensor_update
- Live telemetry state → Analytics view: enriched payload
- Live telemetry state → Fault controls: simulation actions
- Fault controls → Backend orchestrator: fault injection and repair
