Skip to content

Commit

Permalink
[airthings] Enhance logging (openhab#11043)
Browse files Browse the repository at this point in the history
Signed-off-by: Davy Wong <[email protected]>
Signed-off-by: Dave J Schoepel <[email protected]>
  • Loading branch information
dw-8 authored and dschoepel committed Nov 9, 2021
1 parent cbbc215 commit 293de78
Showing 1 changed file with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -49,6 +50,10 @@ abstract public class AbstractAirthingsHandler extends BeaconBluetoothHandler {
private @Nullable ScheduledFuture<?> scheduledTask;

private volatile int refreshInterval;
private volatile int errorConnectCounter;
private volatile int errorReadCounter;
private volatile int errorDisconnectCounter;
private volatile int errorResolvingCounter;

private volatile ServiceState serviceState = ServiceState.NOT_RESOLVED;
private volatile ReadState readState = ReadState.IDLE;
Expand Down Expand Up @@ -129,20 +134,42 @@ private synchronized void execute() {
private void connect() {
logger.debug("Connect to device {}...", address);
if (!device.connect()) {
logger.debug("Connecting to device {} failed", address);
errorConnectCounter++;
if (errorConnectCounter < 6) {
logger.debug("Connecting to device {} failed {} times", address, errorConnectCounter);
} else {
logger.debug("ERROR: Controller reset needed. Connecting to device {} failed {} times", address,
errorConnectCounter);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Connecting to device failed");
}
} else {
logger.debug("Connected to device {}", address);
errorConnectCounter = 0;
}
}

private void disconnect() {
logger.debug("Disconnect from device {}...", address);
if (!device.disconnect()) {
logger.debug("Disconnect from device {} failed", address);
errorDisconnectCounter++;
if (errorDisconnectCounter < 6) {
logger.debug("Disconnect from device {} failed {} times", address, errorDisconnectCounter);
} else {
logger.debug("ERROR: Controller reset needed. Disconnect from device {} failed {} times", address,
errorDisconnectCounter);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Disconnect from device failed");
}
} else {
logger.debug("Disconnected from device {}", address);
errorDisconnectCounter = 0;
}
}

private void read() {
switch (serviceState) {
case NOT_RESOLVED:
logger.debug("Discover services on device {}", address);
discoverServices();
break;
case RESOLVED:
Expand All @@ -152,6 +179,8 @@ private void read() {
BluetoothCharacteristic characteristic = device.getCharacteristic(getDataUUID());
if (characteristic != null) {
readState = ReadState.READING;
errorReadCounter = 0;
errorResolvingCounter = 0;
device.readCharacteristic(characteristic).whenComplete((data, ex) -> {
try {
logger.debug("Characteristic {} from device {}: {}", characteristic.getUuid(),
Expand All @@ -165,14 +194,34 @@ private void read() {
}
});
} else {
logger.debug("Read data from device {} failed", address);
errorReadCounter++;
if (errorReadCounter < 6) {
logger.debug("Read data from device {} failed {} times", address, errorReadCounter);
} else {
logger.debug(
"ERROR: Controller reset needed. Read data from device {} failed {} times",
address, errorReadCounter);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Read data from device failed");
}
disconnect();
}
break;
default:
logger.debug("Unhandled Resolved readState {} on device {}", readState, address);
break;
}
default:
break;
default: // serviceState RESOLVING
errorResolvingCounter++;
if (errorResolvingCounter < 6) {
logger.debug("Unhandled serviceState {} on device {}", serviceState, address);
} else {
logger.debug("ERROR: Controller reset needed. Unhandled serviceState {} on device {}",
serviceState, address);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Service discovery for device failed");
}
break;
}
}
Expand All @@ -197,6 +246,7 @@ private void printServices() {

@Override
public void onConnectionStateChange(BluetoothConnectionStatusNotification connectionNotification) {
logger.debug("Connection State Change Event is {}", connectionNotification.getConnectionState());
switch (connectionNotification.getConnectionState()) {
case DISCONNECTED:
if (serviceState == ServiceState.RESOLVING) {
Expand Down

0 comments on commit 293de78

Please sign in to comment.