Skip to content

Commit

Permalink
Fix inability to recover from network issues on initialization. (open…
Browse files Browse the repository at this point in the history
…hab#11891)

Fixes openhab#11304

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored and moesterheld committed Jan 18, 2022
1 parent ce326b5 commit 0297c33
Showing 1 changed file with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -62,6 +64,7 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
private @Nullable ScheduledFuture<?> pollingJob;
private @Nullable DanfossAirUnitCommunicationController communicationController;
private @Nullable DanfossAirUnit airUnit;
private boolean propertiesInitializedSuccessfully = false;

public DanfossAirUnitHandler(Thing thing) {
super(thing);
Expand Down Expand Up @@ -103,18 +106,8 @@ public void initialize() {
var localCommunicationController = new DanfossAirUnitCommunicationController(
InetAddress.getByName(config.host), TCP_PORT);
this.communicationController = localCommunicationController;
var localAirUnit = new DanfossAirUnit(localCommunicationController);
this.airUnit = localAirUnit;
scheduler.execute(() -> {
try {
thing.setProperty(PROPERTY_UNIT_NAME, localAirUnit.getUnitName());
thing.setProperty(PROPERTY_SERIAL, localAirUnit.getUnitSerialNumber());
startPolling();
updateStatus(ThingStatus.ONLINE);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
}
});
this.airUnit = new DanfossAirUnit(localCommunicationController);
startPolling();
} catch (UnknownHostException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
"@text/offline.communication-error.unknown-host [\"" + config.host + "\"]");
Expand All @@ -138,6 +131,10 @@ private void removeDeprecatedChannels() {
}

private void updateAllChannels() {
if (!initializeProperties()) {
return;
}

DanfossAirUnit localAirUnit = this.airUnit;
if (localAirUnit == null) {
return;
Expand All @@ -153,15 +150,15 @@ private void updateAllChannels() {
try {
updateState(channel.getGroup().getGroupName(), channel.getChannelName(),
channel.getReadAccessor().access(localAirUnit));
if (getThing().getStatus() == ThingStatus.OFFLINE) {
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
} catch (UnexpectedResponseValueException e) {
updateState(channel.getGroup().getGroupName(), channel.getChannelName(), UnDefType.UNDEF);
logger.debug(
"Cannot update channel {}: an unexpected or invalid response has been received from the air unit: {}",
channel.getChannelName(), e.getMessage());
if (getThing().getStatus() == ThingStatus.OFFLINE) {
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
} catch (IOException e) {
Expand All @@ -173,6 +170,33 @@ private void updateAllChannels() {
}
}

private synchronized boolean initializeProperties() {
if (propertiesInitializedSuccessfully) {
return true;
}

DanfossAirUnit localAirUnit = this.airUnit;
if (localAirUnit == null) {
return false;
}

logger.debug("Initializing DanfossHRV properties '{}'", getThing().getUID());

try {
Map<String, String> properties = new HashMap<>(2);
properties.put(PROPERTY_UNIT_NAME, localAirUnit.getUnitName());
properties.put(PROPERTY_SERIAL, localAirUnit.getUnitSerialNumber());
updateProperties(properties);
propertiesInitializedSuccessfully = true;
updateStatus(ThingStatus.ONLINE);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
logger.debug("Cannot initialize properties: an error occurred: {}", e.getMessage());
}

return propertiesInitializedSuccessfully;
}

@Override
public void dispose() {
logger.debug("Disposing Danfoss HRV handler '{}'", getThing().getUID());
Expand Down

0 comments on commit 0297c33

Please sign in to comment.