Skip to content

Commit

Permalink
Protect against NPE's if connection not yet created, update handlers …
Browse files Browse the repository at this point in the history
…only if the bridge is online.

Signed-off-by: digitaldan <[email protected]>
  • Loading branch information
digitaldan committed Apr 12, 2019
1 parent 27768d3 commit 3af0fe0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import java.util.Optional;

import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,9 +37,7 @@ public AbstractOmnilinkStatusHandler(Thing thing) {

@Override
public void initialize() {
Optional<T> status = retrieveStatus();
handleStatus(status.orElse(null)); // handle status will process null.
updateStatus(ThingStatus.ONLINE);
updateHandlerStatus();
}

/**
Expand Down Expand Up @@ -75,4 +75,19 @@ public void channelLinked(ChannelUID channelUID) {
updateChannels(status.get());
}
}

@Override
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
super.bridgeStatusChanged(bridgeStatusInfo);
updateHandlerStatus();
}

private void updateHandlerStatus() {
Bridge bridge = getBridge();
if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
Optional<T> status = retrieveStatus();
handleStatus(status.orElse(null)); // handle status will process null.
updateStatus(ThingStatus.ONLINE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public OmnilinkBridgeHandler(Bridge bridge) {
public void sendOmnilinkCommand(final int message, final int param1, final int param2)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
omniConnection.controllerCommand(message, param1, param2);
getOmniConnection().controllerCommand(message, param1, param2);
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -99,7 +99,7 @@ public void sendOmnilinkCommand(final int message, final int param1, final int p
public SecurityCodeValidation reqSecurityCodeValidation(int area, int digit1, int digit2, int digit3, int digit4)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqSecurityCodeValidation(area, digit1, digit2, digit3, digit4);
return getOmniConnection().reqSecurityCodeValidation(area, digit1, digit2, digit3, digit4);
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -109,7 +109,7 @@ public SecurityCodeValidation reqSecurityCodeValidation(int area, int digit1, in
public void activateKeypadEmergency(int area, int emergencyType)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
omniConnection.activateKeypadEmergency(area, emergencyType);
getOmniConnection().activateKeypadEmergency(area, emergencyType);
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -119,7 +119,7 @@ public void activateKeypadEmergency(int area, int emergencyType)
public SystemInformation reqSystemInformation()
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqSystemInformation();
return getOmniConnection().reqSystemInformation();
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -129,7 +129,7 @@ public SystemInformation reqSystemInformation()
public SystemFormats reqSystemFormats()
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqSystemFormats();
return getOmniConnection().reqSystemFormats();
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -139,7 +139,7 @@ public SystemFormats reqSystemFormats()
private SystemFeatures reqSystemFeatures()
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqSystemFeatures();
return getOmniConnection().reqSystemFeatures();
} catch (IOException | OmniNotConnectedException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -156,8 +156,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
ZoneId.systemDefault());
boolean inDaylightSavings = zdt.getZone().getRules().isDaylightSavings(zdt.toInstant());
try {
omniConnection.setTimeCommand(zdt.getYear() - 2000, zdt.getMonthValue(), zdt.getDayOfMonth(),
zdt.getDayOfWeek().getValue(), zdt.getHour(), zdt.getMinute(), inDaylightSavings);
getOmniConnection().setTimeCommand(zdt.getYear() - 2000, zdt.getMonthValue(),
zdt.getDayOfMonth(), zdt.getDayOfWeek().getValue(), zdt.getHour(), zdt.getMinute(),
inDaylightSavings);
} catch (IOException | OmniNotConnectedException | OmniInvalidResponseException
| OmniUnknownMessageTypeException e) {
logger.debug("Unable to set system date", e);
Expand Down Expand Up @@ -197,7 +198,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

private void makeOmnilinkConnection() {
if (this.omniConnection != null && this.omniConnection.connected()) {
if (omniConnection != null && omniConnection.connected()) {
return;
}

Expand Down Expand Up @@ -385,26 +386,24 @@ public void notConnectedEvent(Exception e) {

private void getSystemStatus() throws IOException, OmniNotConnectedException, OmniInvalidResponseException,
OmniUnknownMessageTypeException {
if (omniConnection != null) {
SystemStatus status = omniConnection.reqSystemStatus();
logger.debug("received system status: {}", status);
// let's update system time
String dateString = new StringBuilder().append(2000 + status.getYear()).append("-")
.append(String.format("%02d", status.getMonth())).append("-")
.append(String.format("%02d", status.getDay())).append("T")
.append(String.format("%02d", status.getHour())).append(":")
.append(String.format("%02d", status.getMinute())).append(":")
.append(String.format("%02d", status.getSecond())).toString();
DateTimeType sysDateTime = new DateTimeType(dateString);
updateState(OmnilinkBindingConstants.CHANNEL_SYSTEMDATE, new DateTimeType(dateString));
logger.debug("System date is: {}", sysDateTime);
}
SystemStatus status = getOmniConnection().reqSystemStatus();
logger.debug("received system status: {}", status);
// let's update system time
String dateString = new StringBuilder().append(2000 + status.getYear()).append("-")
.append(String.format("%02d", status.getMonth())).append("-")
.append(String.format("%02d", status.getDay())).append("T")
.append(String.format("%02d", status.getHour())).append(":")
.append(String.format("%02d", status.getMinute())).append(":")
.append(String.format("%02d", status.getSecond())).toString();
DateTimeType sysDateTime = new DateTimeType(dateString);
updateState(OmnilinkBindingConstants.CHANNEL_SYSTEMDATE, new DateTimeType(dateString));
logger.debug("System date is: {}", sysDateTime);
}

public Message reqObjectProperties(int objectType, int objectNum, int direction, int filter1, int filter2,
int filter3) throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqObjectProperties(objectType, objectNum, direction, filter1, filter2, filter3);
return getOmniConnection().reqObjectProperties(objectType, objectNum, direction, filter1, filter2, filter3);
} catch (OmniNotConnectedException | IOException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -414,7 +413,7 @@ public Message reqObjectProperties(int objectType, int objectNum, int direction,
public Message requestAudioSourceStatus(final int source, final int position)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqAudioSourceStatus(source, position);
return getOmniConnection().reqAudioSourceStatus(source, position);
} catch (OmniNotConnectedException | IOException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand All @@ -425,7 +424,7 @@ public ObjectStatus requestObjectStatus(final int objType, final int startObject
boolean extended)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.reqObjectStatus(objType, startObject, endObject, extended);
return getOmniConnection().reqObjectStatus(objType, startObject, endObject, extended);
} catch (OmniNotConnectedException | IOException e) {
setOfflineAndReconnect(e.getMessage());
throw new BridgeOfflineException(e);
Expand Down Expand Up @@ -483,7 +482,7 @@ public Optional<AudioPlayer> getAudioPlayer() {
public Message reqEventLogData(int eventNumber, int direction)
throws OmniInvalidResponseException, OmniUnknownMessageTypeException, BridgeOfflineException {
try {
return omniConnection.uploadEventLogData(eventNumber, direction);
return getOmniConnection().uploadEventLogData(eventNumber, direction);
} catch (OmniNotConnectedException | IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.getMessage());
throw new BridgeOfflineException(e);
Expand Down Expand Up @@ -558,4 +557,12 @@ private void pollEvents() {
logger.debug("NPE. Omni connection probably not set up.", e);
}
}

private Connection getOmniConnection() throws OmniNotConnectedException {
if (omniConnection != null) {
return omniConnection;
} else {
throw new OmniNotConnectedException("Connection not yet established.");
}
}
}

0 comments on commit 3af0fe0

Please sign in to comment.