# mengxi47/red-safe architecture

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

- Interactive diagram: https://gitdiagram.com/mengxi47/red-safe
- Repository: https://github.com/mengxi47/red-safe

## Overview

##### Project Type and Purpose
- Full-stack, distributed edge-computing system
- Deep-learning inference at camera/edge, C++ API server, iOS client, SQL database, APNs integration

##### File Structure
- Top-level:
– api-server/ (C++ back-end, layered MVC-style: core, model, service, util)
– pytorch/ (Python deep-learning scripts: data prep, training, fall logic, packaging)
– docs/ (architecture image, interaction spec)
– server.crt/key (TLS for API server)
- Layering in api-server:
– core: HTTP server, controllers, session management
– service: business logic (user/device registration, login/logout, event routing)
– model: SQL connection manager, ORM-style SQL model
– util: logging, timestamp helpers
- pytorch:
– download_youtube.py, extract_data.py (data pipeline)
– fall_logic/{rule_based, model_based}
– model/lstm_model.py, ONNX/TensorRT packaging
– train.py, package_project.py, init_project.py, run_all.py

##### README for Insights
- Real-time fall/immobilization detection (<50 ms) on edge
- APNs for iOS push alerts
- Extensible to multi-camera, wearable heart-rate

##### Main Components
A. Edge Device (Camera + Edge Inference)
– Python runtime, ONNX Runtime/TensorRT
– Modules: data capture → preprocess → fall logic (rule/model-based) → event generation
B. API Server (C++ Service)
– Ingress: HTTPS/TLS
– Controller layer: request parsing, routing
– Service layer: registration, login, bind, event handling
– Model layer: SQL DB access
– Util layer: logging, timestamps
C. Database
– SQL (MySQL/SQLite) storing users, devices, sessions, events
D. External Services
– Apple Push Notification Service (APNs)
E. Clients
– iOS App (mobile)
– Future: Watch/heart-rate wearable

##### Relationships and Interactions
1. Edge device captures video → runs inferencing → on detection, POST event to API server
2. API server authenticates session, invokes Service layer → writes to DB → triggers APNs push
3. iOS App receives push → user views alert
4. User registration/login flows: mobile ↔ API server ↔ DB
5. Device registration/binding: mobile ↔ API server ↔ DB

##### Architectural Patterns & Principles
- Layered (Controller → Service → Model → Util)
- Separation of concerns: Inference at edge, business logic & persistence in API
- Event-driven notifications via APNs
- Extensibility: add new services or data sources by following service layer pattern

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Edge Layer
    subgraph "Edge Layer" 
        direction TB
        dd["download_youtube.py"]:::edge
        ed["extract_data.py"]:::edge
        rb["rule_based.py"]:::edge
        mb["model_based.py"]:::edge
        lm["lstm_model.py"]:::edge
        pp["package_project.py"]:::edge
        ip["init_project.py"]:::edge
        tr["train.py"]:::edge
        ro["run_all.py"]:::edge
    end

    %% API Server Layer
    subgraph "API Server (C++ Service)"
        direction TB
        subgraph "Ingress (HTTPS/TLS)"
            maincpp["main.cpp (TLS Config)"]:::api
            apicore["api-server.cpp"]:::api
        end
        subgraph "Controller Layer"
            controller["controller.cpp/hpp"]:::api
            session["session.cpp/hpp"]:::api
        end
        subgraph "Service Layer"
            userSvc["UserRegistrationService/LoginLogoutService"]:::api
            edgeDevSvc["EdgeDeviceRegistrationService"]:::api
            iosDevSvc["IOSDeviceRegistrationService/IOSDeviceBindService"]:::api
        end
        subgraph "Model Layer"
            sqlConn["SqlConnectionManager.hpp"]:::api
            orm["sql_model.cpp/hpp & model.hpp"]:::api
        end
        subgraph "Util Layer"
            logger["logger.hpp"]:::api
            timestamp["timestamp.hpp"]:::api
        end
        maincpp --> apicore
        apicore --> controller
        controller --> session
        session --> userSvc
        controller --> edgeDevSvc
        controller --> iosDevSvc
        userSvc --> orm
        edgeDevSvc --> orm
        iosDevSvc --> orm
        orm --> sqlConn
        userSvc --> timestamp
        logger --> timestamp
    end

    %% Database and External Services
    subgraph "Data & Services"
        direction LR
        db["SQL Database"]:::db
        apns["APNs"]:::ext
        iosApp["iOS App"]:::client
    end

    %% Interactions
    ro -->|"POST event"| maincpp
    lm -->|"Inferencing"| ro
    mb --> rb
    ed --> dd
    apicore -->|"Writes/Reads"| db
    edgeDevSvc -->|"DB operations"| db
    iosDevSvc -->|"DB operations"| db
    userSvc -->|"DB operations"| db
    userSvc -->|"Trigger alert"| apns
    edgeDevSvc -->|"Trigger alert"| apns
    apns -.->|"Push Notification"| iosApp
    iosApp -->|"Register/Login/Bind"| maincpp
    iosApp -->|"Fetch Data"| apicore

    %% Click Events - Edge
    click dd "https://github.com/mengxi47/red-safe/blob/main/pytorch/download_youtube.py"
    click ed "https://github.com/mengxi47/red-safe/blob/main/pytorch/extract_data.py"
    click rb "https://github.com/mengxi47/red-safe/blob/main/pytorch/fall_logic/rule_based.py"
    click mb "https://github.com/mengxi47/red-safe/blob/main/pytorch/fall_logic/model_based.py"
    click lm "https://github.com/mengxi47/red-safe/blob/main/pytorch/model/lstm_model.py"
    click pp "https://github.com/mengxi47/red-safe/blob/main/pytorch/package_project.py"
    click ip "https://github.com/mengxi47/red-safe/blob/main/pytorch/init_project.py"
    click tr "https://github.com/mengxi47/red-safe/blob/main/pytorch/train.py"
    click ro "https://github.com/mengxi47/red-safe/blob/main/pytorch/run_all.py"

    %% Click Events - API Server
    click maincpp "https://github.com/mengxi47/red-safe/blob/main/api-server/main.cpp"
    click apicore "https://github.com/mengxi47/red-safe/blob/main/api-server/src/core/api-server.cpp"
    click controller "https://github.com/mengxi47/red-safe/blob/main/api-server/src/core/controller.cpp"
    click session "https://github.com/mengxi47/red-safe/blob/main/api-server/src/core/session.cpp"
    click userSvc "https://github.com/mengxi47/red-safe/blob/main/api-server/src/service/UserRegistrationService.cpp"
    click edgeDevSvc "https://github.com/mengxi47/red-safe/blob/main/api-server/src/service/EdgeDeviceRegistrationService.cpp"
    click iosDevSvc "https://github.com/mengxi47/red-safe/blob/main/api-server/src/service/IOSDeviceRegistrationService.cpp"
    click sqlConn "https://github.com/mengxi47/red-safe/blob/main/api-server/src/model/SqlConnectionManager.hpp"
    click orm "https://github.com/mengxi47/red-safe/blob/main/api-server/src/model/sql_model.cpp"
    click logger "https://github.com/mengxi47/red-safe/blob/main/api-server/src/util/logger.hpp"
    click timestamp "https://github.com/mengxi47/red-safe/blob/main/api-server/src/util/timestamp.hpp"

    %% Styles
    classDef edge fill:#c6f6d5,stroke:#2f855a,color:#22543d
    classDef api fill:#bee3f8,stroke:#2b6cb0,color:#2c5282
    classDef db fill:#e2e8f0,stroke:#4a5568,color:#2d3748
    classDef ext fill:#fbd38d,stroke:#dd6b20,color:#822727
    classDef client fill:#e9d8fd,stroke:#805ad5,color:#44337a
```
