Merge pull request #67 from icanos/feature/scenes
reworked write queue and added configurable wait time
This commit is contained in:
commit
a9e82dfa5b
4 changed files with 27 additions and 13 deletions
|
|
@ -40,7 +40,7 @@ const GATT_SERVICE_ID = 'org.bluez.GattService1';
|
||||||
const GATT_CHRC_ID = 'org.bluez.GattCharacteristic1';
|
const GATT_CHRC_ID = 'org.bluez.GattCharacteristic1';
|
||||||
|
|
||||||
class PlejdService extends EventEmitter {
|
class PlejdService extends EventEmitter {
|
||||||
constructor(cryptoKey, devices, sceneManager, connectionTimeout, keepAlive = false) {
|
constructor(cryptoKey, devices, sceneManager, connectionTimeout, writeQueueWaitTime, keepAlive = false) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex');
|
this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex');
|
||||||
|
|
@ -53,6 +53,7 @@ class PlejdService extends EventEmitter {
|
||||||
this.devices = devices;
|
this.devices = devices;
|
||||||
this.connectEventHooked = false;
|
this.connectEventHooked = false;
|
||||||
this.connectionTimeout = connectionTimeout;
|
this.connectionTimeout = connectionTimeout;
|
||||||
|
this.writeQueueWaitTime = writeQueueWaitTime;
|
||||||
this.writeQueue = [];
|
this.writeQueue = [];
|
||||||
this.writeQueueRef = null;
|
this.writeQueueRef = null;
|
||||||
|
|
||||||
|
|
@ -362,6 +363,11 @@ class PlejdService extends EventEmitter {
|
||||||
await this.characteristics.data.WriteValue([...encryptedData], {});
|
await this.characteristics.data.WriteValue([...encryptedData], {});
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
if (err.message === 'In Progress') {
|
||||||
|
setTimeout(() => this.write(data, retry), 1000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.log('plejd-ble: write failed ' + err);
|
console.log('plejd-ble: write failed ' + err);
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
await this.init();
|
await this.init();
|
||||||
|
|
@ -425,12 +431,16 @@ class PlejdService extends EventEmitter {
|
||||||
console.log('startWriteQueue()');
|
console.log('startWriteQueue()');
|
||||||
clearInterval(this.writeQueueRef);
|
clearInterval(this.writeQueueRef);
|
||||||
|
|
||||||
this.writeQueueRef = setInterval(async () => {
|
this.writeQueueRef = setTimeout(() => this.runWriteQueue(), this.writeQueueWaitTime);
|
||||||
while (this.writeQueue.length > 0) {
|
}
|
||||||
const data = this.writeQueue.pop();
|
|
||||||
await this.write(data);
|
async runWriteQueue() {
|
||||||
}
|
while (this.writeQueue.length > 0) {
|
||||||
}, 400);
|
const data = this.writeQueue.pop();
|
||||||
|
await this.write(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeQueueRef = setTimeout(() => this.runWriteQueue(), this.writeQueueWaitTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _processPlejdService(path, characteristics) {
|
async _processPlejdService(path, characteristics) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "Plejd",
|
"name": "Plejd",
|
||||||
"version": "0.4.1",
|
"version": "0.4.2",
|
||||||
"slug": "plejd",
|
"slug": "plejd",
|
||||||
"description": "Adds support for the Swedish home automation devices from Plejd.",
|
"description": "Adds support for the Swedish home automation devices from Plejd.",
|
||||||
"url": "https://github.com/icanos/hassio-plejd/",
|
"url": "https://github.com/icanos/hassio-plejd/",
|
||||||
|
|
@ -23,7 +23,8 @@
|
||||||
"mqttUsername": "",
|
"mqttUsername": "",
|
||||||
"mqttPassword": "",
|
"mqttPassword": "",
|
||||||
"includeRoomsAsLights": false,
|
"includeRoomsAsLights": false,
|
||||||
"connectionTimeout": 2
|
"connectionTimeout": 2,
|
||||||
|
"writeQueueWaitTime": 400
|
||||||
},
|
},
|
||||||
"schema": {
|
"schema": {
|
||||||
"site": "str",
|
"site": "str",
|
||||||
|
|
@ -33,6 +34,7 @@
|
||||||
"mqttUsername": "str",
|
"mqttUsername": "str",
|
||||||
"mqttPassword": "str",
|
"mqttPassword": "str",
|
||||||
"includeRoomsAsLights": "bool",
|
"includeRoomsAsLights": "bool",
|
||||||
"connectionTimeout": "int"
|
"connectionTimeout": "int",
|
||||||
|
"writeQueueWaitTime": "int"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,7 +4,7 @@ const fs = require('fs');
|
||||||
const PlejdService = require('./ble.bluez');
|
const PlejdService = require('./ble.bluez');
|
||||||
const SceneManager = require('./scene.manager');
|
const SceneManager = require('./scene.manager');
|
||||||
|
|
||||||
const version = "0.4.1";
|
const version = "0.4.2";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('starting Plejd add-on v. ' + version);
|
console.log('starting Plejd add-on v. ' + version);
|
||||||
|
|
@ -32,7 +32,7 @@ async function main() {
|
||||||
|
|
||||||
// init the BLE interface
|
// init the BLE interface
|
||||||
const sceneManager = new SceneManager(site, devices);
|
const sceneManager = new SceneManager(site, devices);
|
||||||
const plejd = new PlejdService(cryptoKey, devices, sceneManager, config.connectionTimeout, true);
|
const plejd = new PlejdService(cryptoKey, devices, sceneManager, config.connectionTimeout, config.writeQueueWaitTime, true);
|
||||||
plejd.on('connectFailed', () => {
|
plejd.on('connectFailed', () => {
|
||||||
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
|
console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ MQTTUSERNAME=$(jq --raw-output ".mqttUsername" $CONFIG_PATH)
|
||||||
MQTTPASSWORD=$(jq --raw-output ".mqttPassword" $CONFIG_PATH)
|
MQTTPASSWORD=$(jq --raw-output ".mqttPassword" $CONFIG_PATH)
|
||||||
INCLUDEROOMSASLIGHTS=$(jq --raw-output ".includeRoomsAsLights" $CONFIG_PATH)
|
INCLUDEROOMSASLIGHTS=$(jq --raw-output ".includeRoomsAsLights" $CONFIG_PATH)
|
||||||
CONNECTIONTIMEOUT=$(jq --raw-output ".connectionTimeout" $CONFIG_PATH)
|
CONNECTIONTIMEOUT=$(jq --raw-output ".connectionTimeout" $CONFIG_PATH)
|
||||||
|
WRITEQUEUEWAITTIME=$(jq --raw-output ".writeQueueWaitTime" $CONFIG_PATH)
|
||||||
|
|
||||||
PLEJD_PATH=/data/plejd.json
|
PLEJD_PATH=/data/plejd.json
|
||||||
PLEJD_CONFIG="{
|
PLEJD_CONFIG="{
|
||||||
|
|
@ -20,7 +21,8 @@ PLEJD_CONFIG="{
|
||||||
\"mqttUsername\": \"$MQTTUSERNAME\",
|
\"mqttUsername\": \"$MQTTUSERNAME\",
|
||||||
\"mqttPassword\": \"$MQTTPASSWORD\",
|
\"mqttPassword\": \"$MQTTPASSWORD\",
|
||||||
\"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\",
|
\"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\",
|
||||||
\"connectionTimeout\": \"$CONNECTIONTIMEOUT\"
|
\"connectionTimeout\": \"$CONNECTIONTIMEOUT\",
|
||||||
|
\"writeQueueWaitTime\": \"$WRITEQUEUEWAITTIME\"
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue