changed ble adapter
This commit is contained in:
parent
a27d7c1180
commit
82c4384097
1 changed files with 11 additions and 16 deletions
|
|
@ -2,6 +2,7 @@ const plejd = require('./plejd');
|
||||||
const api = require('./api');
|
const api = require('./api');
|
||||||
const mqtt = require('./mqtt');
|
const mqtt = require('./mqtt');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const PlejdService = require('./ble');
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const rawData = fs.readFileSync('/data/plejd.json');
|
const rawData = fs.readFileSync('/data/plejd.json');
|
||||||
|
|
@ -22,43 +23,37 @@ async function main() {
|
||||||
client.init();
|
client.init();
|
||||||
|
|
||||||
// init the BLE interface
|
// init the BLE interface
|
||||||
const controller = new plejd.Controller(cryptoKey, true);
|
const plejd = new PlejdService(cryptoKey, true);
|
||||||
controller.on('scanComplete', async (peripherals) => {
|
plejd.on('authenticated', () => {
|
||||||
await controller.connect();
|
|
||||||
});
|
|
||||||
|
|
||||||
controller.on('connected', () => {
|
|
||||||
console.log('plejd: connected via bluetooth.');
|
console.log('plejd: connected via bluetooth.');
|
||||||
});
|
});
|
||||||
|
|
||||||
// subscribe to changes from Plejd
|
// subscribe to changes from Plejd
|
||||||
controller.on('stateChanged', (deviceId, state) => {
|
plejd.on('stateChanged', (deviceId, state) => {
|
||||||
client.updateState(deviceId, state);
|
client.updateState(deviceId, state);
|
||||||
});
|
});
|
||||||
controller.on('dimChanged', (deviceId, state, dim) => {
|
plejd.on('dimChanged', (deviceId, state, dim) => {
|
||||||
client.updateState(deviceId, state);
|
client.updateState(deviceId, state);
|
||||||
client.updateBrightness(deviceId, dim);
|
client.updateBrightness(deviceId, dim);
|
||||||
});
|
});
|
||||||
|
|
||||||
// subscribe to changes from HA
|
// subscribe to changes from HA
|
||||||
client.on('stateChanged', async (deviceId, state) => {
|
client.on('stateChanged', (deviceId, state) => {
|
||||||
if (state) {
|
if (state) {
|
||||||
await controller.turnOn(deviceId);
|
plejd.turnOn(deviceId);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await controller.turnOff(deviceId);
|
plejd.turnOff(deviceId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
client.on('brightnessChanged', async (deviceId, brightness) => {
|
client.on('brightnessChanged', (deviceId, brightness) => {
|
||||||
if (brightness > 0) {
|
if (brightness > 0) {
|
||||||
await controller.turnOn(deviceId, brightness);
|
plejd.turnOn(deviceId, brightness);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await controller.turnOff(deviceId);
|
plejd.turnOff(deviceId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
controller.init();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue