# kn9annihilator/cyber-physical-smart-grid-load-balancer architecture

> Architecture diagram of the GitHub repository kn9annihilator/cyber-physical-smart-grid-load-balancer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kn9annihilator/cyber-physical-smart-grid-load-balancer
- Repository: https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer

## Overview

##### Project type and purpose
- This is a full-stack IoT dashboard project combining a React/TypeScript single-page application (SPA) with Arduino/ESP8266 firmware.
- Purpose: provide a browser-based UI for monitoring and controlling a “socket sentinel” power outlet; it displays power charts, stats, allows isolation control, and runs simple on-device or in-browser predictions.

##### File structure
- Top level:
- arduino_code/… contains two firmware projects (socket_sentinel_arduino and socket_sentinel_esp8266)
- package.json, vite.config.ts, tsconfig*.json, tailwind.config.ts, postcss.config.js → front-end build and config
- public/, index.html → static assets for SPA
- src/ → React application source
- Inside src/:
- components/ → presentational UI (AlertsDialog, Header, PowerChart, etc.)
- components/ui/ → shadcn-ui primitives (buttons, cards, dialogs, etc.)
- hooks/ → custom React hooks (use-mobile, use-toast)
- lib/ → business logic and utilities (api.ts, mockData.ts, prediction.ts, utils.ts, types.ts)
- pages/ → router targets (Index, NotFound)
- main.tsx, App.tsx → bootstrapping SPA
- Mock data and api.ts imply the front-end can run without a real backend during development; real device communication lives in firmware.

##### Key insights from the README
- Built with Vite, TypeScript, React, shadcn-ui, Tailwind CSS.
- No dedicated backend folder—this is a static site that communicates directly with the ESP8266 or uses mocks.
- Deployment via Lovable’s platform or any static host (e.g., Netlify).

a. Main components to show
- Developer workstation & CI/CD pipeline
- Git repository & build system (Vite, Node.js, npm)
- Static hosting / CDN (Lovable / Netlify)
- Client device (web browser)
- React SPA layers: UI components, hooks, business logic (api.ts, prediction.ts)
- IoT device: ESP8266/Arduino firmware – HTTP or WebSocket server, sensor & relay hardware
- (Optional) External services if any (e.g., cloud time server, analytics)
b. Relationships & interactions
- Developer → Git → CI/CD → build assets → deploy to CDN
- Browser → HTTP GET index.html & JS → renders UI → uses api.ts to send HTTP requests (GET/POST) to ESP8266 on local network
- ESP8266 ↔ sensors (current, voltage) & relay → responds with JSON data → browser updates PowerChart, StatsCard
- prediction.ts may process data in-browser or off-device (if firmware provides raw data)
c. Architectural patterns & design principles
- SPA pattern (client-side routing via pages/)
- Component-based UI (atomic + composite components under components/ui and components/)
- Separation of concerns: view layer (components), state & side effects (hooks), business logic (lib/)
- Firmware layering: hardware abstraction → network interface → sensor/control logic
d. Key technologies
- Front-end: React, TypeScript, Vite, Tailwind CSS, shadcn-ui
- Firmware: Arduino framework, ESP8266 Wi-Fi library
- Build: Node.js, npm, bun.lockb (optional runner), ESLint, PostCSS

##### Detailed capture of essential elements
- UI layer (components/)
- Logic layer (hooks/, lib/)
- Routing (pages/)
- Under firmware: separate “Network Interface”, “Sensor Manager”, “Relay Controller”.
- Which files live in each area (e.g., src/lib/api.ts under Logic layer).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Development & Deployment
    subgraph "Development & Deployment"
        Dev["Developer Workstation"]:::infra
        Git["Git Repository"]:::infra
        CI["CI/CD Pipeline"]:::infra
        CDN["Static Host / CDN (Lovable/Netlify)"]:::infra
        Dev --> Git --> CI -->|"Build Assets"| CDN
    end

    %% Runtime Environment
    subgraph "Runtime Environment"
        subgraph "Local Network"
            Browser["Web Browser (Client)"]:::frontend
            RS["React SPA (src/)"]:::frontend

            RS -->|"renders UI"| Browser
            Browser -->|"GET /index.html & JS"| CDN
            Browser -->|"HTTP GET/POST"| ESPAPI["ESP8266 HTTP API"]:::device

            %% React SPA Layers
            subgraph "UI Layer"
                Alerts["AlertsDialog"]:::frontend
                Header["Header"]:::frontend
                Isolation["IsolationControl"]:::frontend
                Chart["PowerChart"]:::frontend
                Stats["PowerStatsCard"]:::frontend
                Settings["SettingsDialog"]:::frontend
                SocketCard["SocketCard"]:::frontend
            end

            subgraph "UI Primitives (shadcn-ui)"
                UIPrims["ui/"]:::frontend
            end

            subgraph "Hooks"
                MobileHook["use-mobile"]:::frontend
                ToastHook["use-toast"]:::frontend
            end

            subgraph "Logic & Utils"
                API["api.ts"]:::frontend
                Mock["mockData.ts"]:::frontend
                Predict["prediction.ts"]:::frontend
                Types["types.ts"]:::frontend
                Utils["utils.ts"]:::frontend
            end

            subgraph "Routing"
                IndexPage["Index.tsx"]:::frontend
                NotFound["NotFound.tsx"]:::frontend
            end

            subgraph "Bootstrap"
                Main["main.tsx"]:::frontend
                App["App.tsx"]:::frontend
            end

            %% Inter-layer interactions
            Browser -->|"invokes hooks & logic"| API
            API -->|"(mock/dev or real)"| ESPAPI
            Mock --> API
            Predict --> API
        end
    end

    %% IoT Device
    subgraph "IoT Device"
        subgraph "Firmware"
            subgraph "Network Interface"
                ESPAPI
            end
            subgraph "Sensor Manager"
                Sensors["Current & Voltage Sensors"]:::device
            end
            subgraph "Relay Controller"
                Relay["Relay Control"]:::device
            end
            DeviceCore["socket_sentinel_esp8266.ino"]:::device
            ESPAPI --> DeviceCore
            DeviceCore --> Sensors
            DeviceCore --> Relay
            ESPAPI -->|"JSON Data"| Browser
        end
    end

    %% Click Events
    click Alerts "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/AlertsDialog.tsx"
    click Header "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/Header.tsx"
    click Isolation "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/IsolationControl.tsx"
    click Chart "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/PowerChart.tsx"
    click Stats "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/PowerStatsCard.tsx"
    click Settings "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/SettingsDialog.tsx"
    click SocketCard "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/components/SocketCard.tsx"
    click UIPrims "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/tree/main/src/components/ui/"
    click MobileHook "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/hooks/use-mobile.tsx"
    click ToastHook "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/hooks/use-toast.ts"
    click API "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/lib/api.ts"
    click Mock "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/lib/mockData.ts"
    click Predict "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/lib/prediction.ts"
    click Types "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/lib/types.ts"
    click Utils "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/lib/utils.ts"
    click IndexPage "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/pages/Index.tsx"
    click NotFound "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/pages/NotFound.tsx"
    click Main "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/main.tsx"
    click App "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/src/App.tsx"
    click RS "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/tree/main/src/"
    click ESPAPI "https://github.com/kn9annihilator/cyber-physical-smart-grid-load-balancer/blob/main/arduino_code/socket_sentinel_esp8266/socket_sentinel_esp8266.ino"

    %% Style Definitions
    classDef frontend fill:#E0F7FA,stroke:#00ACC1,color:#006064
    classDef device fill:#E8F5E9,stroke:#43A047,color:#1B5E20
    classDef infra fill:#EEEEEE,stroke:#9E9E9E,color:#424242
    class Alerts,Header,Isolation,Chart,Stats,Settings,SocketCard,UIPrims,MobileHook,ToastHook,API,Mock,Predict,Types,Utils,IndexPage,NotFound,Main,App,RS,Browser frontend
    class ESPAPI,DeviceCore,Sensors,Relay device
    class Dev,Git,CI,CDN infra
```
