# openloaf/openlems architecture

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

- Interactive diagram: https://gitdiagram.com/openloaf/openlems
- Repository: https://github.com/openloaf/openlems

## Overview

LEMS is a local energy-management runtime for storage, microgrid, and edge-gateway deployments. The principal workflow is field-device communications through protocol and driver plugins into a unified runtime, then monitoring, alarms, policy/automation, and control; Web APIs support configuration and operations, while MQTT and Modbus expose data externally. The graph preserves documented capabilities, but several subsystem links are README-level because their implementations were not excerpted; those edges cite the README rather than implying inspected source.

Main entry points: [`application/internal/cmd/main.go`](https://github.com/openloaf/openlems/blob/main/application/internal/cmd/main.go) (EMS runtime), [`application/internal/controller`](https://github.com/openloaf/openlems/tree/main/application/internal/controller) (Web API), [`application/internal/controller/policy/policy_v1_energy_storage.go`](https://github.com/openloaf/openlems/blob/main/application/internal/controller/policy/policy_v1_energy_storage.go) (Policy management), [`services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go`](https://github.com/openloaf/openlems/blob/main/services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go) (Strategy database service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and devices"]
  node_runtime_app["EMS runtime<br/>[main.go]"]
  node_drivers["Device drivers"]
  node_driver_manager["Driver manager"]
  node_protocols["Protocol plugins"]
  node_modbus_client["Modbus client"]
end

subgraph group_operations["Web operations"]
  node_web_api["Web API"]
  node_device_ops["Device monitoring"]
  node_alarm_ops["Alarm handling"]
  node_policy_ops["Policy management"]
  node_storage_ops["Storage queries"]
end

subgraph group_decision["Policies and automation"]
  node_policy_service["Policy service"]
  node_ess_policy["Storage policy"]
  node_task_matcher["Task matcher"]
  node_automation["Automation"]
  node_price["Price management"]
end

subgraph group_persistence["Data services"]
  node_db_service["Strategy database service"]
  node_strategy_model[("Strategy records")]
  node_storage_manager["Telemetry storage"]
  node_storage_backend[("Time-series store")]
end

subgraph group_integration["External integration"]
  node_mqtt_export["MQTT export"]
  node_modbus_export["Modbus export"]
  node_prediction["MPC and Kalman<br/>[hexlib.go]"]
end

node_operator(("Operator"))
node_field(("Field devices"))
node_upstream(("Upstream platform"))

node_operator -->|"operates"| node_runtime_app
node_runtime_app -->|"serves"| node_web_api
node_field -->|"communicates through"| node_protocols
node_protocols -.->|"connects devices"| node_driver_manager
node_driver_manager -.->|"loads plugins"| node_drivers
node_web_api -.->|"routes device requests"| node_device_ops
node_web_api -.->|"routes alarm requests"| node_alarm_ops
node_web_api -.->|"routes policy requests"| node_policy_ops
node_web_api -.->|"serves history"| node_storage_ops
node_policy_ops -->|"validates strategy"| node_policy_service
node_policy_ops -->|"manages strategies"| node_db_service
node_db_service -->|"reads and writes"| node_strategy_model
node_ess_policy -->|"selects active task"| node_task_matcher
node_task_matcher -->|"queries enabled tasks"| node_db_service
node_runtime_app -.->|"coordinates automation"| node_automation
node_runtime_app -.->|"runs policies"| node_policy_service
node_runtime_app -.->|"manages pricing"| node_price
node_storage_manager -->|"creates store"| node_storage_backend
node_storage_manager -->|"samples telemetry"| node_field
node_price -->|"saves price data"| node_storage_backend
node_upstream -->|"receives telemetry"| node_mqtt_export
node_upstream -->|"reads exported data"| node_modbus_export
node_modbus_client -.->|"communicates"| node_field
node_runtime_app -->|"offers forecasting"| node_prediction

click node_runtime_app "https://github.com/openloaf/openlems/blob/main/application/internal/cmd/main.go"
click node_drivers "https://github.com/openloaf/openlems/tree/main/plugins/plug_drivers"
click node_driver_manager "https://github.com/openloaf/openlems/blob/main/services/s_driver/internal/s_driver_device_manager_impl_sf.go"
click node_protocols "https://github.com/openloaf/openlems/tree/main/plugins/plug_protocols"
click node_modbus_client "https://github.com/openloaf/openlems/blob/main/plugins/plug_protocols/p_modbus/internal/modbus_client_new_f.go"
click node_web_api "https://github.com/openloaf/openlems/tree/main/application/internal/controller"
click node_device_ops "https://github.com/openloaf/openlems/tree/main/application/internal/controller/device"
click node_alarm_ops "https://github.com/openloaf/openlems/tree/main/application/internal/controller/alarm"
click node_policy_ops "https://github.com/openloaf/openlems/blob/main/application/internal/controller/policy/policy_v1_energy_storage.go"
click node_storage_ops "https://github.com/openloaf/openlems/tree/main/application/internal/controller/device"
click node_policy_service "https://github.com/openloaf/openlems/blob/main/services/s_policy/internal/s_policy_manager_impl_s.go"
click node_ess_policy "https://github.com/openloaf/openlems/blob/main/plugins/plug_policy/p_policy_energy_storage/internal/p_policy_energy_storage_main_s_f.go"
click node_task_matcher "https://github.com/openloaf/openlems/blob/main/services/s_policy/internal/s_policy_energy_storage_matcher_f.go"
click node_automation "https://github.com/openloaf/openlems/blob/main/services/s_automation/internal/s_automation_manager_s.go"
click node_price "https://github.com/openloaf/openlems/blob/main/services/s_price/internal/s_price_manager_impl_s.go"
click node_db_service "https://github.com/openloaf/openlems/blob/main/services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go"
click node_strategy_model "https://github.com/openloaf/openlems/blob/main/services/s_db/s_db_model/s_db_model_energy_storage_s.go"
click node_storage_manager "https://github.com/openloaf/openlems/blob/main/services/s_storage/internal/s_storage_manager_s.go"
click node_storage_backend "https://github.com/openloaf/openlems/blob/main/services/s_storage/s_storage_manager_si.go"
click node_mqtt_export "https://github.com/openloaf/openlems/blob/main/services/s_export_mqtt/internal/s_mqtt_manager_s.go"
click node_modbus_export "https://github.com/openloaf/openlems/blob/main/services/s_export_modbus/internal/s_modbus_manager_s.go"
click node_prediction "https://github.com/openloaf/openlems/blob/main/%40cpp/hexlib/hexlib.go"

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_runtime_app,node_drivers,node_driver_manager,node_protocols,node_modbus_client toneBlue
class node_web_api,node_device_ops,node_alarm_ops,node_policy_ops,node_storage_ops toneAmber
class node_policy_service,node_ess_policy,node_task_matcher,node_automation,node_price toneMint
class node_db_service,node_strategy_model,node_storage_manager,node_storage_backend toneRose
class node_mqtt_export,node_modbus_export,node_prediction,node_operator,node_field,node_upstream toneIndigo
```

## Components

### Runtime and devices

- [EMS runtime](https://github.com/openloaf/openlems/blob/main/application/internal/cmd/main.go): `application/internal/cmd/main.go`
- [Device drivers](https://github.com/openloaf/openlems/tree/main/plugins/plug_drivers): `plugins/plug_drivers`
- [Driver manager](https://github.com/openloaf/openlems/blob/main/services/s_driver/internal/s_driver_device_manager_impl_sf.go): `services/s_driver/internal/s_driver_device_manager_impl_sf.go`
- [Protocol plugins](https://github.com/openloaf/openlems/tree/main/plugins/plug_protocols): `plugins/plug_protocols`
- [Modbus client](https://github.com/openloaf/openlems/blob/main/plugins/plug_protocols/p_modbus/internal/modbus_client_new_f.go): `plugins/plug_protocols/p_modbus/internal/modbus_client_new_f.go`

### Web operations

- [Web API](https://github.com/openloaf/openlems/tree/main/application/internal/controller): `application/internal/controller`
- [Device monitoring](https://github.com/openloaf/openlems/tree/main/application/internal/controller/device): `application/internal/controller/device`
- [Alarm handling](https://github.com/openloaf/openlems/tree/main/application/internal/controller/alarm): `application/internal/controller/alarm`
- [Policy management](https://github.com/openloaf/openlems/blob/main/application/internal/controller/policy/policy_v1_energy_storage.go): `application/internal/controller/policy/policy_v1_energy_storage.go`
- [Storage queries](https://github.com/openloaf/openlems/tree/main/application/internal/controller/device): `application/internal/controller/device`

### Policies and automation

- [Policy service](https://github.com/openloaf/openlems/blob/main/services/s_policy/internal/s_policy_manager_impl_s.go): `services/s_policy/internal/s_policy_manager_impl_s.go`
- [Storage policy](https://github.com/openloaf/openlems/blob/main/plugins/plug_policy/p_policy_energy_storage/internal/p_policy_energy_storage_main_s_f.go): `plugins/plug_policy/p_policy_energy_storage/internal/p_policy_energy_storage_main_s_f.go`
- [Task matcher](https://github.com/openloaf/openlems/blob/main/services/s_policy/internal/s_policy_energy_storage_matcher_f.go): `services/s_policy/internal/s_policy_energy_storage_matcher_f.go`
- [Automation](https://github.com/openloaf/openlems/blob/main/services/s_automation/internal/s_automation_manager_s.go): `services/s_automation/internal/s_automation_manager_s.go`
- [Price management](https://github.com/openloaf/openlems/blob/main/services/s_price/internal/s_price_manager_impl_s.go): `services/s_price/internal/s_price_manager_impl_s.go`

### Data services

- [Strategy database service](https://github.com/openloaf/openlems/blob/main/services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go): `services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go`
- [Strategy records](https://github.com/openloaf/openlems/blob/main/services/s_db/s_db_model/s_db_model_energy_storage_s.go): `services/s_db/s_db_model/s_db_model_energy_storage_s.go`
- [Telemetry storage](https://github.com/openloaf/openlems/blob/main/services/s_storage/internal/s_storage_manager_s.go): `services/s_storage/internal/s_storage_manager_s.go`
- [Time-series store](https://github.com/openloaf/openlems/blob/main/services/s_storage/s_storage_manager_si.go): `services/s_storage/s_storage_manager_si.go`

### External integration

- [MQTT export](https://github.com/openloaf/openlems/blob/main/services/s_export_mqtt/internal/s_mqtt_manager_s.go): `services/s_export_mqtt/internal/s_mqtt_manager_s.go`
- [Modbus export](https://github.com/openloaf/openlems/blob/main/services/s_export_modbus/internal/s_modbus_manager_s.go): `services/s_export_modbus/internal/s_modbus_manager_s.go`
- [MPC and Kalman](https://github.com/openloaf/openlems/blob/main/%40cpp/hexlib/hexlib.go): `@cpp/hexlib/hexlib.go`

### Other components

- **Operator**
- **Field devices**
- **Upstream platform**

## Connections

- Operator → EMS runtime: operates (evidence: `README.md`)
- EMS runtime → Web API: serves (evidence: `README.md`)
- Field devices → Protocol plugins: communicates through (evidence: `README.md`)
- Protocol plugins → Driver manager: connects devices
- Driver manager → Device drivers: loads plugins
- Web API → Device monitoring: routes device requests
- Web API → Alarm handling: routes alarm requests
- Web API → Policy management: routes policy requests
- Web API → Storage queries: serves history
- Policy management → Policy service: validates strategy (evidence: `application/internal/controller/policy/policy_v1_energy_storage.go`)
- Policy management → Strategy database service: manages strategies (evidence: `application/internal/controller/policy/policy_v1_energy_storage.go`)
- Strategy database service → Strategy records: reads and writes (evidence: `services/s_db/internal/impl/s_db_impl_energy_storage_service_s.go`)
- Storage policy → Task matcher: selects active task (evidence: `plugins/plug_policy/p_policy_energy_storage/internal/p_policy_energy_storage_main_s_f.go`)
- Task matcher → Strategy database service: queries enabled tasks (evidence: `services/s_policy/internal/s_policy_energy_storage_matcher_f.go`)
- EMS runtime → Automation: coordinates automation
- EMS runtime → Policy service: runs policies
- EMS runtime → Price management: manages pricing
- Telemetry storage → Time-series store: creates store (evidence: `services/s_storage/s_storage_manager_si.go`)
- Telemetry storage → Field devices: samples telemetry (evidence: `services/s_storage/internal/s_storage_manager_s.go`)
- Price management → Time-series store: saves price data
- Upstream platform → MQTT export: receives telemetry (evidence: `README.md`)
- Upstream platform → Modbus export: reads exported data (evidence: `README.md`)
- Modbus client → Field devices: communicates
- EMS runtime → MPC and Kalman: offers forecasting (evidence: `README.md`)
