HTTP Bridge
HTTP bridge for periodic communication with REST API endpoints.
Overview
The HTTP Bridge provides cycle-synchronized HTTP communication with remote devices and services using REST APIs. It enables OpenEMS components to periodically fetch data from HTTP endpoints and handle responses asynchronously. The bridge is widely used to integrate hardware devices like electric meters, relays, inverters, and other devices that expose RESTful APIs.
Key capabilities: * Cycle-synchronized periodic HTTP requests (every N cycles) * Configurable connect and read timeouts * Automatic error handling and retry logic * JSON parsing support for structured data * Custom headers and HTTP method support
Architecture
The HTTP Bridge consists of:
-
HttpBridgeCycleService: Main interface for cycle-based HTTP operations
-
CycleSubscriber: Manages cycle events and triggers scheduled requests
-
HttpBridgeCycleServiceImpl: Implementation managing request queue and execution
Core Interfaces
HttpBridgeCycleService
Purpose: Provides cycle-synchronized HTTP communication with remote endpoints.
Key Features:
* subscribeCycle(int cycle, String url, Consumer<HttpResponse<String>>) - Fetch every N cycles
* subscribeEveryCycle(String url, Consumer<HttpResponse<String>>) - Fetch every cycle
* subscribeCycle(CycleEndpoint endpoint) - Advanced configuration with custom callbacks
* subscribeJsonCycle(…) - Convenience methods with automatic JSON parsing
* removeCycleEndpoint(CycleEndpoint) - Cancel subscriptions
Usage Pattern:
httpBridgeCycleService.subscribeEveryCycle("http://device.local/status",
response -> {
// Process response data
String data = response.data();
},
error -> {
// Handle error
logger.warn("HTTP request failed: " + error);
}
);
Cycle Scheduling: * If cycle=1: Fetch every OpenEMS cycle * If cycle=5: Fetch every 5th cycle (reduced polling) * If request takes longer than cycle period: Wait for completion, then schedule next
CycleEndpoint
Configuration record for a single HTTP subscription:
Fields:
* cycle (int): How many cycles to wait between requests (minimum 1)
* endpoint (Supplier<Endpoint>): Supplies the HTTP endpoint to fetch
* onResult (Consumer<HttpResponse<String>>): Callback for successful responses
* onError (Consumer<HttpError>): Callback for errors
Usage:
var endpoint = new CycleEndpoint(
5, // Every 5 cycles
() -> new Endpoint("http://device.local/api/status", HttpMethod.GET, ...),
response -> processData(response.data()),
error -> logError(error)
);
httpBridgeCycleService.subscribeCycle(endpoint);