diff --git a/plejd/ble.bluez.js b/plejd/ble.bluez.js index 0bca4fc..2c7f365 100644 --- a/plejd/ble.bluez.js +++ b/plejd/ble.bluez.js @@ -40,7 +40,7 @@ const GATT_SERVICE_ID = 'org.bluez.GattService1'; const GATT_CHRC_ID = 'org.bluez.GattCharacteristic1'; class PlejdService extends EventEmitter { - constructor(cryptoKey, devices, sceneManager, connectionTimeout, keepAlive = false) { + constructor(cryptoKey, devices, sceneManager, connectionTimeout, writeQueueWaitTime, keepAlive = false) { super(); this.cryptoKey = Buffer.from(cryptoKey.replace(/-/g, ''), 'hex'); @@ -53,6 +53,7 @@ class PlejdService extends EventEmitter { this.devices = devices; this.connectEventHooked = false; this.connectionTimeout = connectionTimeout; + this.writeQueueWaitTime = writeQueueWaitTime; this.writeQueue = []; this.writeQueueRef = null; @@ -362,6 +363,11 @@ class PlejdService extends EventEmitter { await this.characteristics.data.WriteValue([...encryptedData], {}); } catch (err) { + if (err.message === 'In Progress') { + setTimeout(() => this.write(data, retry), 1000); + return; + } + console.log('plejd-ble: write failed ' + err); setTimeout(async () => { await this.init(); @@ -425,12 +431,16 @@ class PlejdService extends EventEmitter { console.log('startWriteQueue()'); clearInterval(this.writeQueueRef); - this.writeQueueRef = setInterval(async () => { - while (this.writeQueue.length > 0) { - const data = this.writeQueue.pop(); - await this.write(data); - } - }, 400); + this.writeQueueRef = setTimeout(() => this.runWriteQueue(), this.writeQueueWaitTime); + } + + async runWriteQueue() { + while (this.writeQueue.length > 0) { + const data = this.writeQueue.pop(); + await this.write(data, true); + } + + this.writeQueueRef = setTimeout(() => this.runWriteQueue(), this.writeQueueWaitTime); } async _processPlejdService(path, characteristics) { diff --git a/plejd/config.json b/plejd/config.json index e543442..edff1ed 100644 --- a/plejd/config.json +++ b/plejd/config.json @@ -1,6 +1,6 @@ { "name": "Plejd", - "version": "0.4.1", + "version": "0.4.2", "slug": "plejd", "description": "Adds support for the Swedish home automation devices from Plejd.", "url": "https://github.com/icanos/hassio-plejd/", @@ -23,7 +23,8 @@ "mqttUsername": "", "mqttPassword": "", "includeRoomsAsLights": false, - "connectionTimeout": 2 + "connectionTimeout": 2, + "writeQueueWaitTime": 400 }, "schema": { "site": "str", @@ -33,6 +34,7 @@ "mqttUsername": "str", "mqttPassword": "str", "includeRoomsAsLights": "bool", - "connectionTimeout": "int" + "connectionTimeout": "int", + "writeQueueWaitTime": "int" } } \ No newline at end of file diff --git a/plejd/main.js b/plejd/main.js index 73e5f5e..4f1f538 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -4,7 +4,7 @@ const fs = require('fs'); const PlejdService = require('./ble.bluez'); const SceneManager = require('./scene.manager'); -const version = "0.4.1"; +const version = "0.4.2"; async function main() { console.log('starting Plejd add-on v. ' + version); @@ -32,7 +32,7 @@ async function main() { // init the BLE interface 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', () => { console.log('plejd-ble: were unable to connect, will retry connection in 10 seconds.'); setTimeout(() => { diff --git a/plejd/rootfs/usr/bin/plejd.sh b/plejd/rootfs/usr/bin/plejd.sh index 236e27f..d269053 100644 --- a/plejd/rootfs/usr/bin/plejd.sh +++ b/plejd/rootfs/usr/bin/plejd.sh @@ -10,6 +10,7 @@ MQTTUSERNAME=$(jq --raw-output ".mqttUsername" $CONFIG_PATH) MQTTPASSWORD=$(jq --raw-output ".mqttPassword" $CONFIG_PATH) INCLUDEROOMSASLIGHTS=$(jq --raw-output ".includeRoomsAsLights" $CONFIG_PATH) CONNECTIONTIMEOUT=$(jq --raw-output ".connectionTimeout" $CONFIG_PATH) +WRITEQUEUEWAITTIME=$(jq --raw-output ".writeQueueWaitTime" $CONFIG_PATH) PLEJD_PATH=/data/plejd.json PLEJD_CONFIG="{ @@ -20,7 +21,8 @@ PLEJD_CONFIG="{ \"mqttUsername\": \"$MQTTUSERNAME\", \"mqttPassword\": \"$MQTTPASSWORD\", \"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\", - \"connectionTimeout\": \"$CONNECTIONTIMEOUT\" + \"connectionTimeout\": \"$CONNECTIONTIMEOUT\", + \"writeQueueWaitTime\": \"$WRITEQUEUEWAITTIME\" } "