From 82c438409701e22f6d54d38f4b15a52995c9a456 Mon Sep 17 00:00:00 2001 From: icanos Date: Thu, 19 Dec 2019 09:58:53 +0000 Subject: [PATCH] changed ble adapter --- plejd/main.js | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/plejd/main.js b/plejd/main.js index a43c427..78057a5 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -2,6 +2,7 @@ const plejd = require('./plejd'); const api = require('./api'); const mqtt = require('./mqtt'); const fs = require('fs'); +const PlejdService = require('./ble'); async function main() { const rawData = fs.readFileSync('/data/plejd.json'); @@ -22,43 +23,37 @@ async function main() { client.init(); // init the BLE interface - const controller = new plejd.Controller(cryptoKey, true); - controller.on('scanComplete', async (peripherals) => { - await controller.connect(); - }); - - controller.on('connected', () => { + const plejd = new PlejdService(cryptoKey, true); + plejd.on('authenticated', () => { console.log('plejd: connected via bluetooth.'); }); // subscribe to changes from Plejd - controller.on('stateChanged', (deviceId, state) => { + plejd.on('stateChanged', (deviceId, state) => { client.updateState(deviceId, state); }); - controller.on('dimChanged', (deviceId, state, dim) => { + plejd.on('dimChanged', (deviceId, state, dim) => { client.updateState(deviceId, state); client.updateBrightness(deviceId, dim); }); // subscribe to changes from HA - client.on('stateChanged', async (deviceId, state) => { + client.on('stateChanged', (deviceId, state) => { if (state) { - await controller.turnOn(deviceId); + plejd.turnOn(deviceId); } else { - await controller.turnOff(deviceId); + plejd.turnOff(deviceId); } }); - client.on('brightnessChanged', async (deviceId, brightness) => { + client.on('brightnessChanged', (deviceId, brightness) => { if (brightness > 0) { - await controller.turnOn(deviceId, brightness); + plejd.turnOn(deviceId, brightness); } else { - await controller.turnOff(deviceId); + plejd.turnOff(deviceId); } }); - - controller.init(); }); });