Skip to content

Commit

Permalink
[energidataservice] Introduce subscription-based providers (#17456)
Browse files Browse the repository at this point in the history
* Introduce subscription-based providers

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored Oct 11, 2024
1 parent 4cbae6f commit 7666ee0
Show file tree
Hide file tree
Showing 32 changed files with 2,388 additions and 808 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -86,12 +87,12 @@ public class ApiController {
.create();
private final HttpClient httpClient;
private final TimeZoneProvider timeZoneProvider;
private final String userAgent;
private final Supplier<String> userAgentSupplier;

public ApiController(HttpClient httpClient, TimeZoneProvider timeZoneProvider) {
this.httpClient = httpClient;
this.timeZoneProvider = timeZoneProvider;
userAgent = "openHAB/" + FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
this.userAgentSupplier = this::getUserAgent;
}

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

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

private String getUserAgent() {
return "openHAB/" + FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
}

private String sendRequest(Request request, Map<String, String> properties)
throws TimeoutException, ExecutionException, InterruptedException, DataServiceException {
logger.trace("GET request for {}", request.getURI());
Expand Down Expand Up @@ -210,7 +215,7 @@ public Collection<DatahubPricelistRecord> getDatahubPriceLists(GlobalLocationNum
.timeout(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS) //
.param("filter", mapToFilter(filterMap)) //
.param("columns", columns) //
.agent(userAgent) //
.agent(userAgentSupplier.get()) //
.method(HttpMethod.GET);

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

try {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.time.ZoneId;
import java.util.Currency;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ChannelUID;
Expand Down Expand Up @@ -62,6 +64,12 @@ public class EnergiDataServiceBindingConstants {
CHANNEL_SYSTEM_TARIFF, CHANNEL_TRANSMISSION_GRID_TARIFF, CHANNEL_ELECTRICITY_TAX,
CHANNEL_REDUCED_ELECTRICITY_TAX);

public static final Set<String> CO2_EMISSION_CHANNELS = Set.of(CHANNEL_CO2_EMISSION_PROGNOSIS,
CHANNEL_CO2_EMISSION_REALTIME);

public static final Set<String> SUBSCRIPTION_CHANNELS = Stream
.concat(ELECTRICITY_CHANNELS.stream(), CO2_EMISSION_CHANNELS.stream()).collect(Collectors.toSet());

// List of all properties
public static final String PROPERTY_REMAINING_CALLS = "remainingCalls";
public static final String PROPERTY_TOTAL_CALLS = "totalCalls";
Expand Down
Loading

0 comments on commit 7666ee0

Please sign in to comment.