|
| 1 | +# eebus-go Architecture |
| 2 | + |
| 3 | +This document provides a comprehensive overview of the architecture of the eebus-go library, which implements the EEBUS protocol stack for energy management systems in Go. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The eebus-go library is a Go implementation of the EEBUS standard, providing a foundation for implementing energy management use cases. It builds upon two core protocol implementations: |
| 8 | + |
| 9 | +- **SHIP** (Smart Home IP) - Communication protocol layer |
| 10 | +- **SPINE** (Smart Premises Interoperable Network-Neutral) - Application protocol layer |
| 11 | + |
| 12 | +## Architecture Layers |
| 13 | + |
| 14 | +The architecture follows a layered approach from low-level networking to high-level use cases: |
| 15 | + |
| 16 | +``` |
| 17 | +┌─────────────────────────────────────────┐ |
| 18 | +│ Use Cases Layer │ |
| 19 | +│ (CEM, CS, EG, MA actors with │ |
| 20 | +│ specific use case implementations) │ |
| 21 | +├─────────────────────────────────────────┤ |
| 22 | +│ Features Layer │ |
| 23 | +│ (Client/Server feature helpers for │ |
| 24 | +│ common SPINE feature operations) │ |
| 25 | +├─────────────────────────────────────────┤ |
| 26 | +│ Service Layer │ |
| 27 | +│ (Central orchestration, device │ |
| 28 | +│ management, event coordination) │ |
| 29 | +├─────────────────────────────────────────┤ |
| 30 | +│ SPINE Layer │ |
| 31 | +│ (Application protocol, entities, │ |
| 32 | +│ features, data models) │ |
| 33 | +├─────────────────────────────────────────┤ |
| 34 | +│ SHIP Layer │ |
| 35 | +│ (Transport protocol, websockets, │ |
| 36 | +│ mDNS, pairing, security) │ |
| 37 | +└─────────────────────────────────────────┘ |
| 38 | +``` |
| 39 | + |
| 40 | +## Core Components |
| 41 | + |
| 42 | +### 1. Service Layer (`service/`) |
| 43 | + |
| 44 | +The central orchestration component that manages all aspects of the EEBUS service. |
| 45 | + |
| 46 | +**Key Components:** |
| 47 | + |
| 48 | +- `Service`: Main service implementation that coordinates all subsystems |
| 49 | +- `ServiceInterface`: Defines the contract for service operations |
| 50 | +- `ServiceReaderInterface`: Callback interface for service events |
| 51 | + |
| 52 | +**Responsibilities:** |
| 53 | + |
| 54 | +- Initialize and manage SHIP and SPINE layers |
| 55 | +- Handle device connections and disconnections |
| 56 | +- Coordinate use case implementations |
| 57 | +- Manage mDNS service discovery |
| 58 | +- Handle websocket connections |
| 59 | +- Certificate and security management |
| 60 | + |
| 61 | +**Key Methods:** |
| 62 | + |
| 63 | +- `Setup()`: Initialize the service components |
| 64 | +- `Start()`: Begin service operations |
| 65 | +- `AddUseCase()`: Register use case implementations |
| 66 | +- `RegisterRemoteSKI()`: Register a remote device for connection |
| 67 | + |
| 68 | +### 2. Configuration (`api/configuration.go`) |
| 69 | + |
| 70 | +Defines the service configuration parameters required for EEBUS operation. |
| 71 | + |
| 72 | +**Key Parameters:** |
| 73 | + |
| 74 | +- Device identification (brand, model, serial number, vendor code) |
| 75 | +- Network configuration (port, interfaces, certificates) |
| 76 | +- SPINE device and entity types |
| 77 | +- mDNS service parameters |
| 78 | +- Feature set definitions |
| 79 | + |
| 80 | +### 3. Features Layer (`features/`) |
| 81 | + |
| 82 | +Provides high-level abstractions for SPINE features with client/server role implementations. |
| 83 | + |
| 84 | +#### Client Features (`features/client/`) |
| 85 | + |
| 86 | +Features where the local entity acts as a client (consumer) of remote server features: |
| 87 | + |
| 88 | +- `DeviceClassification`: Request device manufacturer information |
| 89 | +- `DeviceConfiguration`: Read/write device configuration parameters |
| 90 | +- `ElectricalConnection`: Access electrical connection parameters |
| 91 | +- `LoadControl`: Interact with load control functionality |
| 92 | +- `Measurement`: Request measurement data |
| 93 | +- `TimeSeries`: Access time series data |
| 94 | + |
| 95 | +#### Server Features (`features/server/`) |
| 96 | + |
| 97 | +Features where the local entity acts as a server (provider) of feature functionality: |
| 98 | + |
| 99 | +- `DeviceConfiguration`: Provide device configuration data |
| 100 | +- `DeviceDiagnosis`: Report device state and diagnostics |
| 101 | +- `LoadControl`: Accept and manage load control commands |
| 102 | +- `Measurement`: Provide measurement data |
| 103 | + |
| 104 | +#### Internal Features (`features/internal/`) |
| 105 | + |
| 106 | +Common functionality shared between client and server implementations. |
| 107 | + |
| 108 | +### 4. Use Cases Layer (`usecases/`) |
| 109 | + |
| 110 | +Actor-based implementations of specific EEBUS use cases following the EEBUS specification. |
| 111 | + |
| 112 | +#### Actors and Use Cases |
| 113 | + |
| 114 | +**Customer Energy Management (CEM) — `usecases/cem/`:** |
| 115 | + |
| 116 | +- `cevc`: Coordinated EV Charging |
| 117 | +- `evcc`: EV Commissioning and Configuration |
| 118 | +- `evcem`: EV Charging Electricity Measurement |
| 119 | +- `evsecc`: EVSE Commissioning and Configuration |
| 120 | +- `evsoc`: EV State Of Charge |
| 121 | +- `opev`: Overload Protection by EV Charging Current Curtailment |
| 122 | +- `oscev`: Optimization of Self-Consumption During EV Charging |
| 123 | +- `vabd`: Visualization of Aggregated Battery Data |
| 124 | +- `vapd`: Visualization of Aggregated Photovoltaic Data |
| 125 | + |
| 126 | +**Controllable System (CS) — `usecases/cs/`:** |
| 127 | + |
| 128 | +- `lpc`: Limitation of Power Consumption |
| 129 | +- `lpp`: Limitation of Power Production |
| 130 | + |
| 131 | +**Energy Guard (EG) — `usecases/eg/`:** |
| 132 | + |
| 133 | +- `lpc`: Limitation of Power Consumption |
| 134 | +- `lpp`: Limitation of Power Production |
| 135 | + |
| 136 | +**Monitoring Appliance (MA) — `usecases/ma/`:** |
| 137 | + |
| 138 | +- `mpc`: Monitoring of Power Consumption |
| 139 | +- `mgcp`: Monitoring of Grid Connection Point |
| 140 | + |
| 141 | +### 5. Use Case Base (`usecases/usecase/`) |
| 142 | + |
| 143 | +Provides common functionality for all use case implementations: |
| 144 | + |
| 145 | +**`UseCaseBase`:** |
| 146 | + |
| 147 | +- Entity and actor type validation |
| 148 | +- Scenario management |
| 149 | +- Feature registration |
| 150 | +- Event handling coordination |
| 151 | +- Remote device compatibility checking |
| 152 | + |
| 153 | +## Data Flow and Event Handling |
| 154 | + |
| 155 | +### Connection Flow |
| 156 | + |
| 157 | +1. **Service Initialization:** |
| 158 | + |
| 159 | + ``` |
| 160 | + Configuration → Service.Setup() → SPINE Device Creation → mDNS Setup → SHIP Hub Setup |
| 161 | + ``` |
| 162 | + |
| 163 | +2. **Remote Device Discovery:** |
| 164 | + |
| 165 | + ``` |
| 166 | + mDNS Discovery → Service Registry → Connection Attempt → SHIP Handshake → Device Registration |
| 167 | + ``` |
| 168 | + |
| 169 | +3. **Entity and Feature Discovery:** |
| 170 | + |
| 171 | + ``` |
| 172 | + SPINE Discovery → Entity Registration → Feature Registration → Use Case Matching |
| 173 | + ``` |
| 174 | + |
| 175 | +### Event Processing Flow |
| 176 | + |
| 177 | +1. **SHIP Layer Events:** |
| 178 | + - Connection/disconnection events |
| 179 | + - Pairing state updates |
| 180 | + - Service discovery updates |
| 181 | + |
| 182 | +2. **SPINE Layer Events:** |
| 183 | + - Device/entity changes |
| 184 | + - Feature data updates |
| 185 | + - Binding and subscription changes |
| 186 | + |
| 187 | +3. **Use Case Events:** |
| 188 | + - Use case specific data updates |
| 189 | + - State changes |
| 190 | + - Error conditions |
| 191 | + |
| 192 | +### Message Flow |
| 193 | + |
| 194 | +``` |
| 195 | +Application Layer (Use Cases) |
| 196 | + ↕ |
| 197 | +Feature Layer (Client/Server Helpers) |
| 198 | + ↕ |
| 199 | +Service Layer (Event Coordination) |
| 200 | + ↕ |
| 201 | +SPINE Layer (Application Protocol) |
| 202 | + ↕ |
| 203 | +SHIP Layer (Transport Protocol) |
| 204 | + ↕ |
| 205 | +Network Layer (WebSocket/TCP) |
| 206 | +``` |
| 207 | + |
| 208 | +## Component Interactions |
| 209 | + |
| 210 | +### Service Hub Interface |
| 211 | + |
| 212 | +The service implements the `HubReaderInterface` to handle SHIP-level events: |
| 213 | + |
| 214 | +- `RemoteSKIConnected()`: Handle successful remote connections |
| 215 | +- `RemoteSKIDisconnected()`: Handle connection terminations |
| 216 | +- `SetupRemoteDevice()`: Configure remote device communication |
| 217 | +- `VisibleRemoteServicesUpdated()`: Process service discovery updates |
| 218 | + |
| 219 | +### Use Case Integration |
| 220 | + |
| 221 | +Use cases integrate with the service through: |
| 222 | + |
| 223 | +1. **Feature Registration:** Use cases register required SPINE features |
| 224 | +2. **Event Callbacks:** Use cases receive relevant SPINE events |
| 225 | +3. **Data Access:** Use cases access remote device data through feature helpers |
| 226 | +4. **State Management:** Use cases maintain scenario-specific state |
| 227 | + |
| 228 | +### Feature Abstraction |
| 229 | + |
| 230 | +Features provide abstraction over SPINE functionality: |
| 231 | + |
| 232 | +- **Subscription Management:** Automatic subscription to remote feature updates |
| 233 | +- **Data Requests:** Simplified methods for requesting remote data |
| 234 | +- **Write Operations:** Safe write operations with proper validation |
| 235 | +- **Event Filtering:** Automatic filtering of relevant events |
| 236 | + |
| 237 | +## Security and Pairing |
| 238 | + |
| 239 | +### Certificate Management |
| 240 | + |
| 241 | +- X.509 certificate handling for device authentication |
| 242 | +- Certificate validation and trust establishment |
| 243 | +- Secure key exchange during pairing |
| 244 | + |
| 245 | +### Pairing Process |
| 246 | + |
| 247 | +1. **Discovery:** mDNS-based service discovery |
| 248 | +2. **Initial Contact:** SHIP handshake initiation |
| 249 | +3. **Authentication:** Certificate exchange and validation |
| 250 | +4. **Trust Establishment:** User approval/automatic acceptance |
| 251 | +5. **Secure Communication:** Encrypted message exchange |
| 252 | + |
| 253 | +### Access Control |
| 254 | + |
| 255 | +- SKI (Subject Key Identifier) based device identification |
| 256 | +- Configurable auto-accept policies |
| 257 | +- User interaction callbacks for pairing approval |
| 258 | + |
| 259 | +## Configuration and Deployment |
| 260 | + |
| 261 | +### Service Configuration |
| 262 | + |
| 263 | +```go |
| 264 | +configuration := api.NewConfiguration( |
| 265 | + vendorCode, brand, model, serial, |
| 266 | + deviceCategories, deviceType, entityTypes, |
| 267 | + port, certificate, heartbeatTimeout |
| 268 | +) |
| 269 | +``` |
| 270 | + |
| 271 | +### Use Case Registration |
| 272 | + |
| 273 | +```go |
| 274 | +service.AddUseCase(NewEVCC(service, localEntity, eventCallback)) |
| 275 | +``` |
| 276 | + |
| 277 | +### Event Handling |
| 278 | + |
| 279 | +```go |
| 280 | +func (h *Handler) HandleEvent(payload spineapi.EventPayload) { |
| 281 | + // Process use case specific events |
| 282 | +} |
| 283 | +``` |
| 284 | + |
| 285 | +## Extension Points |
| 286 | + |
| 287 | +### Custom Use Cases |
| 288 | + |
| 289 | +- Implement `UseCaseInterface` |
| 290 | +- Extend `UseCaseBase` for common functionality |
| 291 | +- Register with service using `AddUseCase()` |
| 292 | + |
| 293 | +### Custom Features |
| 294 | + |
| 295 | +- Implement feature client/server interfaces |
| 296 | +- Use internal feature helpers for common operations |
| 297 | +- Register features with local entities |
| 298 | + |
| 299 | +### Custom Event Handling |
| 300 | + |
| 301 | +- Implement `EntityEventCallback` for use case events |
| 302 | +- Implement `ServiceReaderInterface` for service events |
| 303 | +- Process events through the established event flow |
| 304 | + |
| 305 | +## Testing and Mocking |
| 306 | + |
| 307 | +The architecture supports comprehensive testing through: |
| 308 | + |
| 309 | +- Mock interfaces for all major components |
| 310 | +- Test helpers for setting up device scenarios |
| 311 | +- Integration test framework for end-to-end testing |
| 312 | +- Feature-specific test suites |
| 313 | + |
| 314 | +## Dependencies |
| 315 | + |
| 316 | +### External Libraries |
| 317 | + |
| 318 | +- `github.com/enbility/ship-go`: SHIP protocol implementation |
| 319 | +- `github.com/enbility/spine-go`: SPINE protocol implementation |
| 320 | + |
| 321 | +### Internal Structure |
| 322 | + |
| 323 | +- Clear separation of concerns between layers |
| 324 | +- Interface-based design for testability |
| 325 | +- Event-driven architecture for loose coupling |
| 326 | + |
| 327 | +This architecture provides a robust, extensible foundation for implementing EEBUS-based energy management solutions while maintaining clear separation between protocol layers and business logic. |
0 commit comments