resolved issue with not connect error msg
This commit is contained in:
parent
bb1b53d4ff
commit
a79f1ca62c
2 changed files with 18 additions and 12 deletions
12
main.js
12
main.js
|
|
@ -41,20 +41,20 @@ async function main() {
|
|||
});
|
||||
|
||||
// subscribe to changes from HA
|
||||
client.on('stateChanged', (deviceId, state) => {
|
||||
client.on('stateChanged', async (deviceId, state) => {
|
||||
if (state) {
|
||||
controller.turnOn(deviceId);
|
||||
await controller.turnOn(deviceId);
|
||||
}
|
||||
else {
|
||||
controller.turnOff(deviceId);
|
||||
await controller.turnOff(deviceId);
|
||||
}
|
||||
});
|
||||
client.on('brightnessChanged', (deviceId, brightness) => {
|
||||
client.on('brightnessChanged', async (deviceId, brightness) => {
|
||||
if (brightness > 0) {
|
||||
controller.turnOn(deviceId, brightness);
|
||||
await controller.turnOn(deviceId, brightness);
|
||||
}
|
||||
else {
|
||||
controller.turnOff(deviceId);
|
||||
await controller.turnOff(deviceId);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
18
plejd.js
18
plejd.js
|
|
@ -254,10 +254,10 @@ class Controller extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
turnOn(id, brightness) {
|
||||
async turnOn(id, brightness) {
|
||||
if (!this.isConnected) {
|
||||
console.log('error: not connected');
|
||||
return;
|
||||
console.log('warning: not connected, will connect. might take a few seconds.');
|
||||
await this.connect();
|
||||
}
|
||||
|
||||
logger('turning on ' + id + ' at brightness ' + brightness);
|
||||
|
|
@ -274,10 +274,10 @@ class Controller extends EventEmitter {
|
|||
this.write(payload);
|
||||
}
|
||||
|
||||
turnOff(id) {
|
||||
async turnOff(id) {
|
||||
if (!this.isConnected) {
|
||||
console.log('error: not connected');
|
||||
return;
|
||||
console.log('warning: not connected, will connect. might take a few seconds.');
|
||||
await this.connect();
|
||||
}
|
||||
|
||||
logger('turning off ' + id);
|
||||
|
|
@ -317,6 +317,12 @@ class Controller extends EventEmitter {
|
|||
var ping = crypto.randomBytes(1);
|
||||
|
||||
try {
|
||||
// make sure we're connected, otherwise, return false and reconnect
|
||||
if (this.peripheral.state !== 'connected') {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
this.pingCharacteristic.write(ping, false, (err) => {
|
||||
if (err) {
|
||||
console.log('error: unable to send ping: ' + err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue