Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hdpowerview] Fix excessive firmware version updates #13155

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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