Skip to content

Commit 7666ee0

Browse files
authored
[energidataservice] Introduce subscription-based providers (openhab#17456)
* Introduce subscription-based providers Signed-off-by: Jacob Laursen <[email protected]>
1 parent 4cbae6f commit 7666ee0

32 files changed

+2388
-808
lines changed

bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/ApiController.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.ExecutionException;
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.TimeoutException;
30+
import java.util.function.Supplier;
3031
import java.util.stream.Collectors;
3132

3233
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -86,12 +87,12 @@ public class ApiController {
8687
.create();
8788
private final HttpClient httpClient;
8889
private final TimeZoneProvider timeZoneProvider;
89-
private final String userAgent;
90+
private final Supplier<String> userAgentSupplier;
9091

9192
public ApiController(HttpClient httpClient, TimeZoneProvider timeZoneProvider) {
9293
this.httpClient = httpClient;
9394
this.timeZoneProvider = timeZoneProvider;
94-
userAgent = "openHAB/" + FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
95+
this.userAgentSupplier = this::getUserAgent;
9596
}
9697

9798
/**
@@ -116,7 +117,7 @@ public ElspotpriceRecord[] getSpotPrices(String priceArea, Currency currency, Da
116117
.param("start", start.toString()) //
117118
.param("filter", "{\"" + FILTER_KEY_PRICE_AREA + "\":\"" + priceArea + "\"}") //
118119
.param("columns", "HourUTC,SpotPrice" + currency) //
119-
.agent(userAgent) //
120+
.agent(userAgentSupplier.get()) //
120121
.method(HttpMethod.GET);
121122

122123
if (!end.isEmpty()) {
@@ -138,6 +139,10 @@ public ElspotpriceRecord[] getSpotPrices(String priceArea, Currency currency, Da
138139
}
139140
}
140141

142+
private String getUserAgent() {
143+
return "openHAB/" + FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
144+
}
145+
141146
private String sendRequest(Request request, Map<String, String> properties)
142147
throws TimeoutException, ExecutionException, InterruptedException, DataServiceException {
143148
logger.trace("GET request for {}", request.getURI());
@@ -210,7 +215,7 @@ public Collection<DatahubPricelistRecord> getDatahubPriceLists(GlobalLocationNum
210215
.timeout(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) //
211216
.param("filter", mapToFilter(filterMap)) //
212217
.param("columns", columns) //
213-
.agent(userAgent) //
218+
.agent(userAgentSupplier.get()) //
214219
.method(HttpMethod.GET);
215220

216221
DateQueryParameter start = tariffFilter.getStart();
@@ -277,7 +282,7 @@ public CO2EmissionRecord[] getCo2Emissions(Dataset dataset, String priceArea, Da
277282
.param("filter", "{\"" + FILTER_KEY_PRICE_AREA + "\":\"" + priceArea + "\"}") //
278283
.param("columns", "Minutes5UTC,CO2Emission") //
279284
.param("sort", "Minutes5UTC DESC") //
280-
.agent(userAgent) //
285+
.agent(userAgentSupplier.get()) //
281286
.method(HttpMethod.GET);
282287

283288
try {

bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/CacheManager.java

-297
This file was deleted.

bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/EnergiDataServiceBindingConstants.java

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.time.ZoneId;
1818
import java.util.Currency;
1919
import java.util.Set;
20+
import java.util.stream.Collectors;
21+
import java.util.stream.Stream;
2022

2123
import org.eclipse.jdt.annotation.NonNullByDefault;
2224
import org.openhab.core.thing.ChannelUID;
@@ -62,6 +64,12 @@ public class EnergiDataServiceBindingConstants {
6264
CHANNEL_SYSTEM_TARIFF, CHANNEL_TRANSMISSION_GRID_TARIFF, CHANNEL_ELECTRICITY_TAX,
6365
CHANNEL_REDUCED_ELECTRICITY_TAX);
6466

67+
public static final Set<String> CO2_EMISSION_CHANNELS = Set.of(CHANNEL_CO2_EMISSION_PROGNOSIS,
68+
CHANNEL_CO2_EMISSION_REALTIME);
69+
70+
public static final Set<String> SUBSCRIPTION_CHANNELS = Stream
71+
.concat(ELECTRICITY_CHANNELS.stream(), CO2_EMISSION_CHANNELS.stream()).collect(Collectors.toSet());
72+
6573
// List of all properties
6674
public static final String PROPERTY_REMAINING_CALLS = "remainingCalls";
6775
public static final String PROPERTY_TOTAL_CALLS = "totalCalls";

0 commit comments

Comments
 (0)