# shohinsan/mortesense architecture

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

- Interactive diagram: https://gitdiagram.com/shohinsan/mortesense
- Repository: https://github.com/shohinsan/mortesense

## Overview

##### Main Components
- Frontend – A React application (written in TypeScript with Tailwind CSS) that serves as the customer-facing web portal. It includes components for authentication, dashboard pages (alerts, history, settings, etc.), and uses contexts and hooks for state management.
- Backend – A Flask-based API written in Python. It contains routes (alerts, auth, device management, Twilio integration, and user management), a structured “src” folder for the application logic, and uses a virtual environment. It handles RESTful communications, processes data, and serves as the business logic layer.
- Database & Caching – A MySQL database for storing users, sensor data, and similar records. A Redis server is also employed (likely for caching or fast data handling).
- Hardware – A Raspberry Pi Pico W with a connected microwave motion sensor. The “hardware” folder (with files such as main.py and setup_server.py) likely deals with sensor interfacing, processing sensor events, and might communicate with the backend.
- External Integrations – Twilio API (for messaging/notifications), AWS (for deployment/storage via RDS or other AWS services), and API testing via Postman.

##### Relationships and Interactions Between Components
- User requests and interactions (for login, sensor data viewing, account management, etc.) originate at the Frontend.
- The Backend processes requests (authentication, sensor alerts, etc.), then interacts with the MySQL database to read/write data and with Redis for caching or sessions.
- The Hardware sensor module (running on the Pico W) detects motion events and sends data (possibly via network calls or designated API endpoints) to the Backend.
- Twilio and potentially AWS are integrated by the Backend to deliver notifications or support deployment/infrastructure needs.

##### Important Architectural Patterns and Design Principles
- Separation of Concerns: A clear decoupling between the frontend UI, the backend business logic, and the hardware layer.
- Client–Server Architecture: The client (frontend) remains responsible for the user experience while the backend handles data processing and API management.
- Modular Organization: The directory structure (frontend, backend, hardware) suggests that each component is independently developed and maintained.
- Integration with External Services: The backend connects to Twilio for SMS/notification purposes and leverages AWS and Redis for scalability and performance.

##### Relevant Technologies and Frameworks
- Frontend: React, TypeScript, Tailwind CSS, React Router.
- Backend: Python with Flask, SQLAlchemy (implied by the models and usage of Flask extensions), Alembic for migrations, and integration with MySQL and Redis.
- Hardware: Python scripts running on Raspberry Pi Pico W, interfacing with a microwave motion sensor.
- External Services: Twilio API, AWS (RDS or storage), and Postman for API validation.

- Use clear labels for each component (e.g., “React Frontend,” “Flask API Server,” “MySQL Database,” “Redis Cache,” “Raspberry Pi Pico W – Sensor Module,” “Twilio API”).
- Optionally, add brief notes on technologies used (e.g., “Flask (Python) for REST API,” “React (TypeScript) for UI”) near each component.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface
    subgraph "User Interface"
        U("User"):::user
        FE("React Frontend"):::frontend
    end

    %% Backend System
    subgraph "Backend System"
        BAPI("Flask API Server"):::backend
        DB("MySQL Database & Redis Cache"):::database
        TWI("Twilio API Service"):::external
        AWS("AWS Services"):::external
    end

    %% Testing Tools
    subgraph "API Testing"
        PM("Postman"):::external
    end

    %% Hardware Layer
    subgraph "Hardware Layer"
        HW("Raspberry Pi Pico W - Sensor Module"):::hardware
    end

    %% Relationships
    U -->|"initiates"| FE
    FE -->|"HTTP/REST"| BAPI
    BAPI -->|"Read/Write"| DB
    HW -->|"Sensor Data"| BAPI
    BAPI -->|"Notification"| TWI
    BAPI -->|"Deployment/Storage"| AWS
    PM -->|"SimulatedRequests"| BAPI

    %% Click Events
    click FE "https://github.com/shohinsan/mortesense/tree/main/frontend/"
    click BAPI "https://github.com/shohinsan/mortesense/blob/main/backend/src/app.py"
    click BAPI "https://github.com/shohinsan/mortesense/blob/main/backend/run.py"
    click DB "https://github.com/shohinsan/mortesense/blob/main/backend/.env"
    click DB "https://github.com/shohinsan/mortesense/blob/main/backend/src/database.py"
    click HW "https://github.com/shohinsan/mortesense/blob/main/hardware/main.py"
    click HW "https://github.com/shohinsan/mortesense/blob/main/hardware/setup_server.py"
    click TWI "https://github.com/shohinsan/mortesense/blob/main/backend/src/routes/twilio.py"

    %% Styles
    classDef user fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef database fill:#FFDEAD,stroke:#333,stroke-width:2px;
    classDef hardware fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
