Fix inability to recover from network issues on initialization. (#11891)

Fixes #11304

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2021-12-30 11:58:22 +01:00 committed by GitHub
parent a407555362
commit 50560f7b5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,8 @@ import static org.openhab.binding.danfossairunit.internal.DanfossAirUnitBindingC
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -62,6 +64,7 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
private @Nullable ScheduledFuture<?> pollingJob; private @Nullable ScheduledFuture<?> pollingJob;
private @Nullable DanfossAirUnitCommunicationController communicationController; private @Nullable DanfossAirUnitCommunicationController communicationController;
private @Nullable DanfossAirUnit airUnit; private @Nullable DanfossAirUnit airUnit;
private boolean propertiesInitializedSuccessfully = false;
public DanfossAirUnitHandler(Thing thing) { public DanfossAirUnitHandler(Thing thing) {
super(thing); super(thing);
@ -103,18 +106,8 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
var localCommunicationController = new DanfossAirUnitCommunicationController( var localCommunicationController = new DanfossAirUnitCommunicationController(
InetAddress.getByName(config.host), TCP_PORT); InetAddress.getByName(config.host), TCP_PORT);
this.communicationController = localCommunicationController; this.communicationController = localCommunicationController;
var localAirUnit = new DanfossAirUnit(localCommunicationController); this.airUnit = new DanfossAirUnit(localCommunicationController);
this.airUnit = localAirUnit;
scheduler.execute(() -> {
try {
thing.setProperty(PROPERTY_UNIT_NAME, localAirUnit.getUnitName());
thing.setProperty(PROPERTY_SERIAL, localAirUnit.getUnitSerialNumber());
startPolling(); startPolling();
updateStatus(ThingStatus.ONLINE);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
}
});
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
"@text/offline.communication-error.unknown-host [\"" + config.host + "\"]"); "@text/offline.communication-error.unknown-host [\"" + config.host + "\"]");
@ -138,6 +131,10 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
} }
private void updateAllChannels() { private void updateAllChannels() {
if (!initializeProperties()) {
return;
}
DanfossAirUnit localAirUnit = this.airUnit; DanfossAirUnit localAirUnit = this.airUnit;
if (localAirUnit == null) { if (localAirUnit == null) {
return; return;
@ -153,7 +150,7 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
try { try {
updateState(channel.getGroup().getGroupName(), channel.getChannelName(), updateState(channel.getGroup().getGroupName(), channel.getChannelName(),
channel.getReadAccessor().access(localAirUnit)); channel.getReadAccessor().access(localAirUnit));
if (getThing().getStatus() == ThingStatus.OFFLINE) { if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
} catch (UnexpectedResponseValueException e) { } catch (UnexpectedResponseValueException e) {
@ -161,7 +158,7 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
logger.debug( logger.debug(
"Cannot update channel {}: an unexpected or invalid response has been received from the air unit: {}", "Cannot update channel {}: an unexpected or invalid response has been received from the air unit: {}",
channel.getChannelName(), e.getMessage()); channel.getChannelName(), e.getMessage());
if (getThing().getStatus() == ThingStatus.OFFLINE) { if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
} catch (IOException e) { } catch (IOException e) {
@ -173,6 +170,33 @@ public class DanfossAirUnitHandler extends BaseThingHandler {
} }
} }
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 @Override
public void dispose() { public void dispose() {
logger.debug("Disposing Danfoss HRV handler '{}'", getThing().getUID()); logger.debug("Disposing Danfoss HRV handler '{}'", getThing().getUID());