Skip to content

Commit

Permalink
Fix excessive firmware version updates (#13155)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored Jul 21, 2022
1 parent 572d1ec commit 0577ee8
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.hdpowerview.internal.handler;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -82,6 +84,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private final HttpClient httpClient;
private final HDPowerViewTranslationProvider translationProvider;
private final ConcurrentHashMap<ThingUID, ShadeData> pendingShadeInitializations = new ConcurrentHashMap<>();
private final Duration firmwareVersionValidityPeriod = Duration.ofDays(1);

private long refreshInterval;
private long hardRefreshPositionInterval;
Expand All @@ -95,7 +98,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<Scene> sceneCache = new CopyOnWriteArrayList<>();
private List<SceneCollection> sceneCollectionCache = new CopyOnWriteArrayList<>();
private List<ScheduledEvent> scheduledEventCache = new CopyOnWriteArrayList<>();
private @Nullable FirmwareVersions firmwareVersions;
private Instant firmwareVersionsUpdated = Instant.MIN;
private Boolean deprecatedChannelsCreated = false;

private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
Expand Down Expand Up @@ -141,6 +144,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
} catch (HubMaintenanceException e) {
// exceptions are logged in HDPowerViewWebTargets
firmwareVersionsUpdated = Instant.MIN;
} catch (NumberFormatException | HubException e) {
logger.debug("Unexpected error {}", e.getMessage());
}
Expand All @@ -164,7 +168,7 @@ public void initialize() {
hardRefreshPositionInterval = config.hardRefresh;
hardRefreshBatteryLevelInterval = config.hardRefreshBatteryLevel;
initializeChannels();
firmwareVersions = null;
firmwareVersionsUpdated = Instant.MIN;

updateStatus(ThingStatus.UNKNOWN);
schedulePoll();
Expand Down Expand Up @@ -304,15 +308,17 @@ private synchronized void poll() {
}
} catch (HubMaintenanceException e) {
// exceptions are logged in HDPowerViewWebTargets
firmwareVersionsUpdated = Instant.MIN;
} catch (HubException e) {
logger.warn("Error connecting to bridge: {}", e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
firmwareVersionsUpdated = Instant.MIN;
}
}

private void updateFirmwareProperties()
throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
if (firmwareVersions != null) {
if (firmwareVersionsUpdated.isAfter(Instant.now().minus(firmwareVersionValidityPeriod))) {
return;
}
FirmwareVersions firmwareVersions = webTargets.getFirmwareVersions();
Expand All @@ -334,6 +340,7 @@ private void updateFirmwareProperties()
properties.put(HDPowerViewBindingConstants.PROPERTY_RADIO_FIRMWARE_VERSION, radio.toString());
}
updateProperties(properties);
firmwareVersionsUpdated = Instant.now();
}

private void pollShades() throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
Expand Down

0 comments on commit 0577ee8

Please sign in to comment.