Skip to content

Commit

Permalink
Fixing state reported as sleep when turning on device
Browse files Browse the repository at this point in the history
  • Loading branch information
dwaan committed Sep 15, 2022
1 parent 54bd6a2 commit 84d193f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 29 deletions.
107 changes: 81 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}
});
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -580,15 +635,15 @@ 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`);
}

/**
* A helper to output log
* @param {string} text text to display in Homebridge log
*/
displayInfo(text) {
this.log.info(`${this.name} - ${text}`);
this.log.info(`${this.name} - 🤖 ${text}`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 84d193f

Please sign in to comment.