changed ble adapter

This commit is contained in:
icanos 2019-12-19 09:58:53 +00:00
parent a27d7c1180
commit 82c4384097

View file

@ -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();
});
});