From f8d5ae081b715923e95ddffc6bab9b14e51363be Mon Sep 17 00:00:00 2001 From: GiviMAD Date: Mon, 8 Feb 2021 17:25:52 -0800 Subject: [PATCH] [androiddebugbridge] check device awake state and minor fixes (#10106) * check device awake state and minor fixes * avoid update awake channel when not linked Signed-off-by: Miguel --- .../AndroidDebugBridgeBindingConstants.java | 1 + .../internal/AndroidDebugBridgeDevice.java | 9 ++++++- .../internal/AndroidDebugBridgeHandler.java | 25 +++++++++++++++++++ .../resources/OH-INF/thing/thing-types.xml | 8 ++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeBindingConstants.java b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeBindingConstants.java index de2d31d64d923..3e738d6a114fc 100644 --- a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeBindingConstants.java +++ b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeBindingConstants.java @@ -42,6 +42,7 @@ public class AndroidDebugBridgeBindingConstants { public static final String STOP_PACKAGE_CHANNEL = "stop-package"; public static final String STOP_CURRENT_PACKAGE_CHANNEL = "stop-current-package"; public static final String CURRENT_PACKAGE_CHANNEL = "current-package"; + public static final String AWAKE_STATE_CHANNEL = "awake-state"; public static final String WAKE_LOCK_CHANNEL = "wake-lock"; public static final String SCREEN_STATE_CHANNEL = "screen-state"; // List of all Parameters diff --git a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java index 8778fcfbbec42..8bcdd58e76cc8 100644 --- a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java +++ b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeDevice.java @@ -120,12 +120,19 @@ public String getCurrentPackage() throws AndroidDebugBridgeDeviceException, Inte throw new AndroidDebugBridgeDeviceReadException("can read package name"); } + public boolean isAwake() + throws InterruptedException, AndroidDebugBridgeDeviceException, TimeoutException, ExecutionException { + String devicesResp = runAdbShell("dumpsys", "activity", "|", "grep", "mWakefulness"); + return devicesResp.contains("mWakefulness=Awake"); + } + public boolean isScreenOn() throws InterruptedException, AndroidDebugBridgeDeviceException, AndroidDebugBridgeDeviceReadException, TimeoutException, ExecutionException { String devicesResp = runAdbShell("dumpsys", "power", "|", "grep", "'Display Power'"); if (devicesResp.contains("=")) { try { - return devicesResp.split("=")[1].equals("ON"); + var state = devicesResp.split("=")[1].trim(); + return state.equals("ON"); } catch (NumberFormatException e) { logger.debug("Unable to parse device wake lock: {}", e.getMessage()); } diff --git a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java index 245e38b90ca37..410e4887e4e19 100644 --- a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java +++ b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/AndroidDebugBridgeHandler.java @@ -59,6 +59,7 @@ public class AndroidDebugBridgeHandler extends BaseThingHandler { private AndroidDebugBridgeConfiguration config = new AndroidDebugBridgeConfiguration(); private @Nullable ScheduledFuture connectionCheckerSchedule; private AndroidDebugBridgeMediaStatePackageConfig @Nullable [] packageConfigs = null; + private boolean deviceAwake = false; public AndroidDebugBridgeHandler(Thing thing) { super(thing); @@ -135,6 +136,12 @@ private void handleCommandInternal(ChannelUID channelUID, Command command) updateState(channelUID, new DecimalType(lock)); } break; + case AWAKE_STATE_CHANNEL: + if (command instanceof RefreshType) { + boolean awakeState = adbConnection.isAwake(); + updateState(channelUID, OnOffType.from(awakeState)); + } + break; case SCREEN_STATE_CHANNEL: if (command instanceof RefreshType) { boolean screenState = adbConnection.isScreenOn(); @@ -277,6 +284,7 @@ public void checkConnection() { } catch (AndroidDebugBridgeDeviceException e) { logger.debug("Error connecting to device; [{}]: {}", e.getClass().getCanonicalName(), e.getMessage()); + adbConnection.disconnect(); updateStatus(ThingStatus.OFFLINE); return; } @@ -294,6 +302,23 @@ public void checkConnection() { } private void refreshStatus() throws InterruptedException, AndroidDebugBridgeDeviceException, ExecutionException { + boolean awakeState; + boolean prevDeviceAwake = deviceAwake; + try { + awakeState = adbConnection.isAwake(); + deviceAwake = awakeState; + } catch (TimeoutException e) { + logger.warn("Unable to refresh awake state: Timeout"); + return; + } + var awakeStateChannelUID = new ChannelUID(this.thing.getUID(), AWAKE_STATE_CHANNEL); + if (isLinked(awakeStateChannelUID)) { + updateState(awakeStateChannelUID, OnOffType.from(awakeState)); + } + if (!awakeState && !prevDeviceAwake) { + logger.debug("device {} is sleeping", config.ip); + return; + } try { handleCommandInternal(new ChannelUID(this.thing.getUID(), MEDIA_VOLUME_CHANNEL), RefreshType.REFRESH); } catch (AndroidDebugBridgeDeviceReadException e) { diff --git a/bundles/org.openhab.binding.androiddebugbridge/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.androiddebugbridge/src/main/resources/OH-INF/thing/thing-types.xml index 98710f5459c74..77336ba900f98 100644 --- a/bundles/org.openhab.binding.androiddebugbridge/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.androiddebugbridge/src/main/resources/OH-INF/thing/thing-types.xml @@ -18,6 +18,7 @@ + serial @@ -386,6 +387,13 @@ + + Switch + + Awake State + + + Switch