# freightcognition/temp_monitor architecture

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

- Interactive diagram: https://gitdiagram.com/freightcognition/temp_monitor
- Repository: https://github.com/freightcognition/temp_monitor

## Overview

##### Main components
a. Hardware Layer
- Raspberry Pi Zero 2 W (system host)
- Sense HAT (environmental sensor + LED matrix)
b. Software Layer (all in `temp_monitor.py`)
- Data Acquisition Module (reads raw temperature & humidity via Sense HAT API)
- Temperature Compensation Logic (applies compensation formula using CPU temp)
- Logging Module (writes timestamped readings to a local log file)
- LED Display Module (renders current temperature on Sense HAT LEDs)
- Web Server & API Module (Flask app serves dashboard & JSON endpoints)
- Asset Store (static assets directory for logos, CSS if expanded)
- OpenAPI Specification (`openapi.yaml` describing JSON endpoints)
c. System Integration
- Systemd Service (manages automatic start/restart of the Python service)
d. External Clients
- Web Browser (dashboard UI consuming HTML + JSON)
- Third‑party Monitoring Tools (poll `/api/temp` or `/api/raw`)

##### Relationships & data flows
- Sensor → Acquisition Module: raw readings (temperature, humidity)
- Acquisition → Compensation Logic: raw + CPU temp → compensated temp
- Compensation →
- Logging Module → Log File
- LED Display Module → Sense HAT LEDs
- Web/API Module → in-memory state for endpoints
- Web/API Module →
- Dashboard HTML (auto‑refresh JS pulling `/api/temp`)
- JSON endpoints (`/api/temp`, `/api/raw`)
- Browser/Tools → HTTP calls to Flask server on port 8080
- Systemd → Python process lifecycle management

##### Architectural patterns & tech
- Monolithic single‑process service (no microservices)
- Modular logical separation within one Python script
- RESTful API layer (Flask + OpenAPI)
- Hardware abstraction via Sense HAT Python library
- Persistent logging to flat file (no DB)
- Systemd integration for process control

##### Detail without overthinking
- Every logical component even if implemented in one file
- Runtime interactions (sensor reads, HTTP requests, file writes, LED updates)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Layer
    subgraph "Hardware Layer"
        direction TB
        RaspberryPi["Raspberry Pi Zero 2 W"]:::hardware
        SenseHAT["Sense HAT"]:::hardware
    end

    RaspberryPi -->|"hosts"| SenseHAT

    %% System Manager
    Systemd["systemd Service"]:::manager
    Systemd -->|"manages process"| DataAcq

    %% Software Layer
    subgraph "Software Layer (temp_monitor.py)"
        direction TB
        DataAcq["Data Acquisition Module"]:::modules
        CompLogic["Temperature Compensation Logic"]:::modules
        Logging["Logging Module"]:::modules
        LED["LED Display Module"]:::modules
        WebAPI["Web Server & API Module"]:::modules
    end

    click DataAcq "https://github.com/freightcognition/temp_monitor/blob/master/temp_monitor.py"
    click CompLogic "https://github.com/freightcognition/temp_monitor/blob/master/temp_monitor.py"
    click Logging "https://github.com/freightcognition/temp_monitor/blob/master/temp_monitor.py"
    click LED "https://github.com/freightcognition/temp_monitor/blob/master/temp_monitor.py"
    click WebAPI "https://github.com/freightcognition/temp_monitor/blob/master/temp_monitor.py"

    SenseHAT -->|"I²C / Sense HAT library calls\n(raw readings)"| DataAcq
    DataAcq -->|"raw temp & humidity"| CompLogic
    CompLogic --> Logging
    CompLogic --> LED
    CompLogic --> WebAPI
    Logging -->|"writes file"| LogFile
    LED -->|"updates LEDs"| SenseHAT

    %% Storage
    LogFile["Log File"]:::storage

    %% Static Assets & Spec
    AssetStore["Asset Store"]:::modules
    OpenAPI["OpenAPI Specification"]:::modules

    click AssetStore "https://github.com/freightcognition/temp_monitor/tree/master/assets/"
    click OpenAPI "https://github.com/freightcognition/temp_monitor/blob/master/openapi.yaml"

    WebAPI -->|"serves static assets"| AssetStore
    WebAPI -->|"serves OpenAPI spec"| OpenAPI

    %% External Clients
    Browser["Web Browser"]:::external
    Monitoring["Third‑party Monitoring Tools"]:::external

    Browser -->|HTTP port 8080| WebAPI
    Monitoring -->|HTTP port 8080| WebAPI
    WebAPI -->|"Dashboard HTML & auto-refresh JS"| Browser
    WebAPI -->|"JSON endpoints (/api/temp, /api/raw)"| Monitoring

    %% Styles
    classDef hardware fill:#cccccc,stroke:#333,stroke-width:1px
    classDef modules fill:#add8e6,stroke:#333,stroke-width:1px
    classDef storage fill:#ffeb99,stroke:#333,stroke-width:1px
    classDef external fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef manager fill:#ffcc99,stroke:#333,stroke-width:1px
```
