From 84d193fc444bbb044a7e4216964c61b4b423e32f Mon Sep 17 00:00:00 2001 From: Dwan Date: Thu, 15 Sep 2022 18:52:56 +0300 Subject: [PATCH] Fixing state reported as sleep when turning on device --- index.js | 107 ++++++++++++++++++++++++++++++++++++++------------- package.json | 6 +-- 2 files changed, 84 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 02b913b..98a0ff1 100644 --- a/index.js +++ b/index.js @@ -158,30 +158,85 @@ class ADBPlugin { this.handleRemoteControl(); // Power events - this.adb.on("awake", () => { - this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE); - this.displayInfo(this.green(`Awake`)); - }); - this.adb.on("sleep", () => { - this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.INACTIVE); - this.displayInfo(this.red(`Sleep`)); - }); - // App change event - this.parseInput(this.adb.currentAppID); - this.adb.on("appChange", () => { + + let count = 0; + this.adb.on(`update`, (type, message, debug) => { + switch (type) { + // Connection events + case `connecting`: + this.displayDebug("Connecting..."); + break; + case `timeout`: + this.displayDebug("Timeout..."); + break; + case `status`: + if (count++ == 0) this.displayDebug(`Alive ${Date()}`); + if (count >= 60) count = 0; + break; + case `connected`: + this.displayDebug("Connected"); + break; + case `disconnected`: + this.displayDebug("Not connected"); + break; + case `authorized`: + this.displayDebug("Authorized"); + break; + case `unauthorized`: + this.displayDebug("Unauthorized"); + break; + + // App events + case `appChange`: + this.parseInput(this.adb.currentAppID); + break; + case `playback`: + if (this.enablePlaybackSensor == YES) { + if (this.isPlaying == this.adb.isPlayback) return; + + this.isPlaying = this.playbackSensorExclude.includes(this.currentAppID) ? NO : this.adb.isPlayback ? YES : NO; + this.displayInfo(`Playback - ${this.isPlaying ? this.green(`On`) : this.red(`Off`)}`); + this.accessoryPlaybackSensorService.updateCharacteristic(Characteristic.MotionDetected, this.isPlaying); + if (debug) this.displayDebug("Playback debug:\n" + debug.trim()); + } + break; + + // Sleep/awake events + case `awake`: + this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE); + this.displayInfo(this.green(`Awake`)); + break; + case `sleep`: + this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.INACTIVE); + this.displayInfo(this.red(`Sleep`)); + break; + + // Power events + case `powerOn`: + this.displayDebug(`Turning power on`); + break; + case `powerOff`: + this.displayDebug(`Turning power off`); + break; + case `debugPowerOn`: + this.displayDebug(`Turning power on: ${message.awake}, ${debug}`); + break; + case `debugPowerOff`: + this.displayDebug(`Turning power off: ${message.awake}, ${debug}`); + break; + case `powerOnStatus`: + this.displayDebug(`Turning power on: ${message}`); + break; + case `powerOffStatus`: + this.displayDebug(`Turning power off: ${message}`); + break; + + default: + break; + } + // App change event this.parseInput(this.adb.currentAppID); }); - // Playback event - if (this.enablePlaybackSensor == YES) { - this.adb.on("playback", (appId, playback, message) => { - if (this.isPlaying == this.adb.isPlayback) return; - - this.isPlaying = this.playbackSensorExclude.includes(this.currentAppID) ? NO : this.adb.isPlayback ? YES : NO; - this.displayInfo(`Playback - ${this.isPlaying ? this.green(`On`) : this.red(`Off`)}`); - this.accessoryPlaybackSensorService.updateCharacteristic(Characteristic.MotionDetected, this.isPlaying); - if (message) this.displayDebug("Playback debug:\n" + message.trim()); - }); - } }); } @@ -373,7 +428,7 @@ class ADBPlugin { this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.INACTIVE); - if (error) this.displayDebug(`Power on error message:\n${error}`); + if (error) this.displayDebug(`Power on error message: ${error}`); }); } } else { @@ -393,7 +448,7 @@ class ADBPlugin { this.accessoryService.updateCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE); - if (error) this.displayDebug(`Power off error message:\n${error}`); + if (error) this.displayDebug(`Power off error message: ${error}`); }); } }).onGet(() => this.adb.isAwake ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE); @@ -580,7 +635,7 @@ class ADBPlugin { * @param {string} text text to display in Homebridge log */ displayDebug(text) { - if (this.debug) this.log.info(`\x1b[2m${this.name} - ${text}\x1b[0m`); + if (this.debug) this.log.info(`\x1b[2m${this.name} - 🐞 ${text}\x1b[0m`); } /** @@ -588,7 +643,7 @@ class ADBPlugin { * @param {string} text text to display in Homebridge log */ displayInfo(text) { - this.log.info(`${this.name} - ${text}`); + this.log.info(`${this.name} - 🤖 ${text}`); } } diff --git a/package.json b/package.json index b079979..98df02c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-adb", - "version": "2.0.1", + "version": "2.1.0", "description": "A HomeBridge plugin to control ADB enabled Android devices", "main": "index.js", "engines": { @@ -28,8 +28,8 @@ }, "homepage": "https://github.com/dwaan/homebridge-adb#readme", "dependencies": { - "nodejs-adb-wrapper": "^1.1.3", - "wake_on_lan": "^1.0.0" + "nodejs-adb-wrapper": "1.2.0", + "wake_on_lan": "1.0.0" }, "devDependencies": { "nodemon": "^2.0.15",