Skip to content

Commit

Permalink
Added some random delays to avoid cloudfront rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
seime committed Nov 26, 2024
1 parent 687aa9c commit 7396a39
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ApiBridge(Storage<String> storage) {
this.storage = storage;
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> logger.debug(message));
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("UTC"));
dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("GMT"));

try {
digest = MessageDigest.getInstance("SHA-256");
Expand Down Expand Up @@ -202,11 +202,11 @@ private Request buildRequest(Token token, final AbstractRequest req) {

Instant timestamp = Instant.now();

String formattedDate = dateTimeFormatter.format(timestamp);
request.addHeader("Accept-Encoding", "gzip, deflate").addHeader("Accept", "*/*")
.addHeader("User-Agent", "G-RAC").addHeader("Content-Type", "application/json;charset=utf-8")
.addHeader("x-app-name", "Comfort Cloud")
.addHeader("x-app-timestamp", dateTimeFormatter.format(timestamp)).addHeader("x-app-type", "1")
.addHeader("x-app-version", appVersion)
.addHeader("x-app-name", "Comfort Cloud").addHeader("x-app-timestamp", formattedDate)
.addHeader("x-app-type", "1").addHeader("x-app-version", appVersion)
.addHeader("x-cfc-api-key", generateAPIKey(timestamp, token.getAccessToken()))
.addHeader("x-user-authorization-v2", "Bearer " + token.getAccessToken())
.addHeader("x-client-id", token.getClientId()).build();
Expand Down Expand Up @@ -401,10 +401,11 @@ private Token doV2AuthorizationFlow() throws IOException, NoSuchAlgorithmExcepti
RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),
gson.toJson(new GetAccClientIdDTO()));

String formattedDate = dateTimeFormatter.format(now);
Request getAccClientIdRequest = new Request.Builder().post(body).url(BASE_PATH_ACC + "/auth/v2/login")
.addHeader("Accept-Encoding", "gzip, deflate").addHeader("Accept", "*/*")
.addHeader("User-Agent", "G-RAC").addHeader("Content-Type", "application/json;charset=utf-8")
.addHeader("x-app-name", "Comfort Cloud").addHeader("x-app-timestamp", dateTimeFormatter.format(now))
.addHeader("x-app-name", "Comfort Cloud").addHeader("x-app-timestamp", formattedDate)
.addHeader("x-app-type", "1").addHeader("x-app-version", appVersion)
.addHeader("x-cfc-api-key", generateAPIKey(now, accessToken))
.addHeader("x-user-authorization-v2", "Bearer " + accessToken).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package no.seime.openhab.binding.panasoniccomfortcloud.internal.handler;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -63,6 +65,8 @@ public class PanasonicComfortCloudAccountHandler extends BaseBridgeHandler {

private static final String STORAGE_KEY = "PanasonicComfortCloud-Storage";

private Random random = new Random();

public PanasonicComfortCloudAccountHandler(final Bridge bridge, StorageService storageService) {
super(bridge);
apiBridge = new ApiBridge(storageService.getStorage(STORAGE_KEY));
Expand Down Expand Up @@ -146,13 +150,18 @@ public synchronized void doPoll(boolean triggerDeviceUpdate) {
}
updateStatus(ThingStatus.ONLINE);
if (triggerDeviceUpdate) {

AtomicLong delayIncrementer = new AtomicLong(1000);

try {
getThing().getThings().parallelStream()
getThing().getThings().stream()
.filter(e -> e.isEnabled()
&& (e.getStatus() == ThingStatus.ONLINE || e.getStatus() == ThingStatus.OFFLINE))
.forEach(e -> {
long delay = delayIncrementer.addAndGet(random.nextLong(3000));
try {
((PanasonicComfortCloudBaseThingHandler) e.getHandler()).loadFromServer();
scheduler.schedule(() -> ((PanasonicComfortCloudBaseThingHandler) e.getHandler())
.loadFromServer(), delay, TimeUnit.MILLISECONDS);
} catch (Exception ex) {
logger.warn("Error updating thing {}", e.getUID(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static no.seime.openhab.binding.panasoniccomfortcloud.internal.BindingConstants.*;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.measure.quantity.Temperature;
Expand Down Expand Up @@ -55,6 +56,8 @@ public PanasonicComfortCloudAirconditionHandler(Thing thing) {
super(thing);
}

private Random random = new Random();

private AirConditionerConfiguration config;

@Override
Expand All @@ -65,7 +68,7 @@ public void initialize() {
super.initialize(config.deviceId);
// This handler is responsible for loading the first time from the server, then the Account handler will take
// over
scheduler.schedule(this::loadIfDevicePresent, 3, TimeUnit.SECONDS);
scheduler.schedule(this::loadIfDevicePresent, 3 + random.nextInt(10), TimeUnit.SECONDS);
}

private void loadIfDevicePresent() {
Expand Down

0 comments on commit 7396a39

Please sign in to comment.