# pt9912/bess-ems architecture

> Architecture diagram of the GitHub repository pt9912/bess-ems, 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/pt9912/bess-ems
- Repository: https://github.com/pt9912/bess-ems

## Overview

bess-ems is a battery energy management system that ingests field measurements, assesses their quality, and routes schedules or market activations through a safety-oriented control loop before issuing device commands. Its API and worker are driving entry points; protocol, optimization, persistence, and optional native components are replaceable adapters. The README documents the central control stages, while sampled source confirms selected wiring and query behavior. Several subsystems are represented at directory level because their implementation files were not sampled; the Go field simulator is a separate local counterparty, not a production EMS component.

Main entry points: [`src/hexagon/BatteryEms.Application/Control/ControlCycleUseCase.cs`](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Control/ControlCycleUseCase.cs) (Control Cycle), [`src/host/BatteryEms.Host/BessHostBuilder.cs`](https://github.com/pt9912/bess-ems/blob/main/src/host/BatteryEms.Host/BessHostBuilder.cs) (Host Composition), [`src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs`](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs) (Telemetry Ingestion), [`src/adapters/driving/BatteryEms.Api/Endpoints/BatteryEmsEndpoints.cs`](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Api/Endpoints/BatteryEmsEndpoints.cs) (API and Operator UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry Points"]
  node_api["API and Operator UI"]
  node_host["Host Composition<br/>[BessHostBuilder.cs]"]
  node_worker["Background Worker"]
end

subgraph group_core["Application Core"]
  node_ingestion["Telemetry Ingestion"]
  node_snapshot[("Realtime Snapshot<br/>[ISnapshotStore.cs]")]
  node_status["Status Queries"]
  node_schedule[("Schedule Tracking")]
  node_control["Control Cycle"]
  node_safety["Safety Limits"]
  node_ramp["Ramp Limits<br/>[RampLimiter.cs]"]
  node_optimization["Schedule Optimization"]
  node_market["Market Activation"]
end

subgraph group_adapters["Field and Solver Adapters"]
  node_modbus["Modbus Adapter"]
  node_mqtt["MQTT Adapter"]
  node_opcua["OPC-UA Adapter"]
  node_optimizer_adapter["Solver Adapters"]
  node_optimization_core["Optimization Core Client"]
  node_native["Native Control Kernel"]
end

subgraph group_state["State and Operations"]
  node_persistence[("Persistence")]
  node_auth["API Token Registry"]
end

subgraph group_sim["Simulator"]
  node_sim_command["Simulator Command<br/>[main.go]"]
  node_sim_runtime["Scenario Runtime<br/>[orchestrator.go]"]
  node_sim_modbus["Simulator Modbus<br/>[server.go]"]
  node_sim_mqtt["Simulator MQTT<br/>[paho_client.go]"]
end

node_operator(("Operator"))
node_field_device(("Battery System"))

node_operator -->|"requests status or control"| node_api
node_api -->|"authenticates writes"| node_auth
node_api -->|"queries status"| node_status
node_host -->|"registers services"| node_api
node_host -->|"configures adapter"| node_modbus
node_host -->|"configures adapter"| node_mqtt
node_host -->|"configures adapter"| node_opcua
node_host -->|"configures solver"| node_optimizer_adapter
node_host -->|"configures client"| node_optimization_core
node_worker -->|"runs ingestion"| node_ingestion
node_ingestion -->|"stores measurements"| node_snapshot
node_modbus -.->|"supplies telemetry"| node_ingestion
node_mqtt -.->|"supplies telemetry"| node_ingestion
node_opcua -.->|"supplies telemetry"| node_ingestion
node_status -->|"reads latest snapshot"| node_snapshot
node_worker -->|"runs control cycle"| node_control
node_control -->|"reads measurements"| node_snapshot
node_control -->|"resolves schedule"| node_schedule
node_control -->|"prioritizes activation"| node_market
node_control -->|"applies constraints"| node_safety
node_control -->|"limits power ramp"| node_ramp
node_control -.->|"dispatches command"| node_modbus
node_control -.->|"dispatches command"| node_mqtt
node_control -.->|"dispatches command"| node_opcua
node_optimization -.->|"requests schedule solve"| node_optimizer_adapter
node_optimization -.->|"requests remote solve"| node_optimization_core
node_schedule -.->|"stores schedules"| node_persistence
node_native -.->|"optional kernel"| node_control
node_field_device -->|"provides field signals"| node_modbus
node_field_device -->|"provides field signals"| node_mqtt
node_field_device -->|"provides field signals"| node_opcua
node_sim_command -->|"runs scenario"| node_sim_runtime
node_sim_runtime -.->|"publishes Modbus"| node_sim_modbus
node_sim_runtime -.->|"publishes MQTT"| node_sim_mqtt
node_sim_modbus -.->|"simulates protocol"| node_field_device
node_sim_mqtt -.->|"simulates protocol"| node_field_device

click node_api "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Api/Endpoints/BatteryEmsEndpoints.cs"
click node_host "https://github.com/pt9912/bess-ems/blob/main/src/host/BatteryEms.Host/BessHostBuilder.cs"
click node_worker "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs"
click node_ingestion "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs"
click node_snapshot "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Realtime/ISnapshotStore.cs"
click node_status "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Api/DefaultBatteryStatusQuery.cs"
click node_schedule "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Markets/IScheduleRepository.cs"
click node_control "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Control/ControlCycleUseCase.cs"
click node_safety "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Domain/ConstraintLimiter.cs"
click node_ramp "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Domain/RampLimiter.cs"
click node_optimization "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Optimization/ScheduleOptimizationRequest.cs"
click node_market "https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Markets/IRegelleistungActivationUseCase.cs"
click node_modbus "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Modbus/ModbusTelemetrySource.cs"
click node_mqtt "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Mqtt/MqttAdapterOptions.cs"
click node_opcua "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.OpcUa/OpcUaAdapterOptions.cs"
click node_optimizer_adapter "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Optimization/OrTools/OrToolsScheduleOptimizer.cs"
click node_optimization_core "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.OptimizationCore/OptimizationCoreOptions.cs"
click node_persistence "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Persistence/DapperScheduleRepository.cs"
click node_auth "https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Api/Auth/ApiTokenRegistry.cs"
click node_native "https://github.com/pt9912/bess-ems/blob/main/native/battery_control_core/include/battery_control_core.h"
click node_sim_command "https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/cmd/bess-field-sim/main.go"
click node_sim_runtime "https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/runtime/orchestrator.go"
click node_sim_modbus "https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/modbus/server.go"
click node_sim_mqtt "https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/mqtt/paho_client.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_api,node_host,node_worker toneBlue
class node_ingestion,node_snapshot,node_status,node_schedule,node_control,node_safety,node_ramp,node_optimization,node_market toneAmber
class node_modbus,node_mqtt,node_opcua,node_optimizer_adapter,node_optimization_core,node_native toneMint
class node_persistence,node_auth toneRose
class node_sim_command,node_sim_runtime,node_sim_modbus,node_sim_mqtt,node_operator,node_field_device toneIndigo
```

## Components

### Entry Points

- [API and Operator UI](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Api/Endpoints/BatteryEmsEndpoints.cs): `src/adapters/driving/BatteryEms.Api/Endpoints/BatteryEmsEndpoints.cs`
- [Host Composition](https://github.com/pt9912/bess-ems/blob/main/src/host/BatteryEms.Host/BessHostBuilder.cs): `src/host/BatteryEms.Host/BessHostBuilder.cs`
- [Background Worker](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs): `src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs`

### Application Core

- [Telemetry Ingestion](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs): `src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs`
- [Realtime Snapshot](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Realtime/ISnapshotStore.cs): `src/hexagon/BatteryEms.Application/Realtime/ISnapshotStore.cs`
- [Status Queries](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Api/DefaultBatteryStatusQuery.cs): `src/hexagon/BatteryEms.Application/Api/DefaultBatteryStatusQuery.cs`
- [Schedule Tracking](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Markets/IScheduleRepository.cs): `src/hexagon/BatteryEms.Application/Markets/IScheduleRepository.cs`
- [Control Cycle](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Control/ControlCycleUseCase.cs): `src/hexagon/BatteryEms.Application/Control/ControlCycleUseCase.cs`
- [Safety Limits](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Domain/ConstraintLimiter.cs): `src/hexagon/BatteryEms.Domain/ConstraintLimiter.cs`
- [Ramp Limits](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Domain/RampLimiter.cs): `src/hexagon/BatteryEms.Domain/RampLimiter.cs`
- [Schedule Optimization](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Optimization/ScheduleOptimizationRequest.cs): `src/hexagon/BatteryEms.Application/Optimization/ScheduleOptimizationRequest.cs`
- [Market Activation](https://github.com/pt9912/bess-ems/blob/main/src/hexagon/BatteryEms.Application/Markets/IRegelleistungActivationUseCase.cs): `src/hexagon/BatteryEms.Application/Markets/IRegelleistungActivationUseCase.cs`

### Field and Solver Adapters

- [Modbus Adapter](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Modbus/ModbusTelemetrySource.cs): `src/adapters/driven/BatteryEms.Adapters.Modbus/ModbusTelemetrySource.cs`
- [MQTT Adapter](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Mqtt/MqttAdapterOptions.cs): `src/adapters/driven/BatteryEms.Adapters.Mqtt/MqttAdapterOptions.cs`
- [OPC-UA Adapter](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.OpcUa/OpcUaAdapterOptions.cs): `src/adapters/driven/BatteryEms.Adapters.OpcUa/OpcUaAdapterOptions.cs`
- [Solver Adapters](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Optimization/OrTools/OrToolsScheduleOptimizer.cs): `src/adapters/driven/BatteryEms.Adapters.Optimization/OrTools/OrToolsScheduleOptimizer.cs`
- [Optimization Core Client](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.OptimizationCore/OptimizationCoreOptions.cs): `src/adapters/driven/BatteryEms.Adapters.OptimizationCore/OptimizationCoreOptions.cs`
- [Native Control Kernel](https://github.com/pt9912/bess-ems/blob/main/native/battery_control_core/include/battery_control_core.h): `native/battery_control_core/include/battery_control_core.h`

### State and Operations

- [Persistence](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driven/BatteryEms.Adapters.Persistence/DapperScheduleRepository.cs): `src/adapters/driven/BatteryEms.Adapters.Persistence/DapperScheduleRepository.cs`
- [API Token Registry](https://github.com/pt9912/bess-ems/blob/main/src/adapters/driving/BatteryEms.Api/Auth/ApiTokenRegistry.cs): `src/adapters/driving/BatteryEms.Api/Auth/ApiTokenRegistry.cs`

### Simulator

- [Simulator Command](https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/cmd/bess-field-sim/main.go): `simulators/bess-field-sim/cmd/bess-field-sim/main.go`
- [Scenario Runtime](https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/runtime/orchestrator.go): `simulators/bess-field-sim/internal/runtime/orchestrator.go`
- [Simulator Modbus](https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/modbus/server.go): `simulators/bess-field-sim/internal/modbus/server.go`
- [Simulator MQTT](https://github.com/pt9912/bess-ems/blob/main/simulators/bess-field-sim/internal/mqtt/paho_client.go): `simulators/bess-field-sim/internal/mqtt/paho_client.go`

### Other components

- **Operator**
- **Battery System**

## Connections

- Operator → API and Operator UI: requests status or control (evidence: `README.md`)
- API and Operator UI → API Token Registry: authenticates writes
- API and Operator UI → Status Queries: queries status (evidence: `README.md`)
- Host Composition → API and Operator UI: registers services (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Host Composition → Modbus Adapter: configures adapter (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Host Composition → MQTT Adapter: configures adapter (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Host Composition → OPC-UA Adapter: configures adapter (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Host Composition → Solver Adapters: configures solver (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Host Composition → Optimization Core Client: configures client (evidence: `src/host/BatteryEms.Host/BessHostBuilder.cs`)
- Background Worker → Telemetry Ingestion: runs ingestion (evidence: `src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs`)
- Telemetry Ingestion → Realtime Snapshot: stores measurements (evidence: `src/adapters/driving/BatteryEms.Worker/TelemetryIngestionHostedService.cs`)
- Modbus Adapter → Telemetry Ingestion: supplies telemetry
- MQTT Adapter → Telemetry Ingestion: supplies telemetry
- OPC-UA Adapter → Telemetry Ingestion: supplies telemetry
- Status Queries → Realtime Snapshot: reads latest snapshot (evidence: `src/hexagon/BatteryEms.Application/Api/DefaultBatteryStatusQuery.cs`)
- Background Worker → Control Cycle: runs control cycle (evidence: `README.md`)
- Control Cycle → Realtime Snapshot: reads measurements (evidence: `README.md`)
- Control Cycle → Schedule Tracking: resolves schedule (evidence: `README.md`)
- Control Cycle → Market Activation: prioritizes activation (evidence: `README.md`)
- Control Cycle → Safety Limits: applies constraints (evidence: `README.md`)
- Control Cycle → Ramp Limits: limits power ramp (evidence: `README.md`)
- Control Cycle → Modbus Adapter: dispatches command
- Control Cycle → MQTT Adapter: dispatches command
- Control Cycle → OPC-UA Adapter: dispatches command
- Schedule Optimization → Solver Adapters: requests schedule solve
- Schedule Optimization → Optimization Core Client: requests remote solve
- Schedule Tracking → Persistence: stores schedules
- Native Control Kernel → Control Cycle: optional kernel (evidence: `README.md`)
- Battery System → Modbus Adapter: provides field signals (evidence: `README.md`)
- Battery System → MQTT Adapter: provides field signals (evidence: `README.md`)
- Battery System → OPC-UA Adapter: provides field signals (evidence: `README.md`)
- Simulator Command → Scenario Runtime: runs scenario (evidence: `simulators/bess-field-sim/cmd/bess-field-sim/main.go`)
- Scenario Runtime → Simulator Modbus: publishes Modbus
- Scenario Runtime → Simulator MQTT: publishes MQTT
- Simulator Modbus → Battery System: simulates protocol (evidence: `README.md`)
- Simulator MQTT → Battery System: simulates protocol (evidence: `README.md`)
