continued work

This commit is contained in:
icanos 2020-01-20 10:58:03 +00:00
parent 51c257d461
commit 667beaa11f

View file

@ -72,6 +72,8 @@ class PlejdService extends EventEmitter {
this.objectManager.removeAllListeners(); this.objectManager.removeAllListeners();
} }
clearInterval(this.pingRef);
const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/'); const bluez = await this.bus.getProxyObject(BLUEZ_SERVICE_NAME, '/');
this.objectManager = await bluez.getInterface(DBUS_OM_INTERFACE); this.objectManager = await bluez.getInterface(DBUS_OM_INTERFACE);
@ -293,13 +295,6 @@ class PlejdService extends EventEmitter {
this.write(payload); this.write(payload);
} }
disconnect() {
console.log('disconnect()');
clearInterval(this.pingRef);
}
async authenticate() { async authenticate() {
console.log('authenticate()'); console.log('authenticate()');
const self = this; const self = this;
@ -322,17 +317,20 @@ class PlejdService extends EventEmitter {
async write(data, retry = true) { async write(data, retry = true) {
try { try {
console.log('plejd-ble: sending ' + data.length + ' byte(s) of data to Plejd');
const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data); const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data);
await this.characteristics.data.WriteValue([...encryptedData], {}); await this.characteristics.data.WriteValue([...encryptedData], {});
} }
catch (err) { catch (err) {
console.log('plejd-ble: write failed ' + err); console.log('plejd-ble: write failed ' + err);
await this.init(); setTimeout(async () => {
await this.init();
if (retry) { if (retry) {
logger('reconnected and retrying to write'); logger('reconnected and retrying to write');
await this.write(data, false); await this.write(data, false);
} }
}, 2000);
} }
} }
@ -350,6 +348,8 @@ class PlejdService extends EventEmitter {
logger('ping'); logger('ping');
await this.ping(); await this.ping();
}, 3000); }, 3000);
await this.ping();
} }
onPingSuccess(nr) { onPingSuccess(nr) {
@ -407,6 +407,7 @@ class PlejdService extends EventEmitter {
Buffer.from( Buffer.from(
String(dirtyAddr[1]) String(dirtyAddr[1])
.replace(/\-/g, '') .replace(/\-/g, '')
.replace(/\_/g, '')
.replace(/\:/g, ''), 'hex' .replace(/\:/g, ''), 'hex'
) )
); );
@ -438,7 +439,7 @@ class PlejdService extends EventEmitter {
} }
return { return {
addr: [...addr] addr: addr
}; };
} }
@ -448,8 +449,6 @@ class PlejdService extends EventEmitter {
const objects = await this.objectManager.GetManagedObjects(); const objects = await this.objectManager.GetManagedObjects();
let characteristics = []; let characteristics = [];
console.log(objects);
for (const path of Object.keys(objects)) { for (const path of Object.keys(objects)) {
const interfaces = Object.keys(objects[path]); const interfaces = Object.keys(objects[path]);
if (interfaces.indexOf(GATT_CHRC_ID) > -1) { if (interfaces.indexOf(GATT_CHRC_ID) > -1) {
@ -459,7 +458,6 @@ class PlejdService extends EventEmitter {
for (const path of Object.keys(objects)) { for (const path of Object.keys(objects)) {
const interfaces = Object.keys(objects[path]); const interfaces = Object.keys(objects[path]);
console.log(interfaces);
if (interfaces.indexOf(GATT_SERVICE_ID) > -1) { if (interfaces.indexOf(GATT_SERVICE_ID) > -1) {
let chPaths = []; let chPaths = [];
for (const c of characteristics) { for (const c of characteristics) {
@ -492,14 +490,15 @@ class PlejdService extends EventEmitter {
// After we've authenticated, we need to hook up the event listener // After we've authenticated, we need to hook up the event listener
// for changes to lastData. // for changes to lastData.
this.characteristics.lastData.on('PropertiesChanged', this.onLastDataUpdated.bind(this)); this.characteristics.lastDataProperties.on('PropertiesChanged', this.onLastDataUpdated.bind(this));
this.characteristics.lastData.StartNotify();
} }
//onLastDataUpdated(data, isNotification) { async onLastDataUpdated(iface, properties, invalidated) {
onLastDataUpdated(iface, properties, invalidated) {
if (iface !== GATT_CHRC_ID) { if (iface !== GATT_CHRC_ID) {
return; return;
} }
const changedKeys = Object.keys(properties); const changedKeys = Object.keys(properties);
if (changedKeys.length === 0) { if (changedKeys.length === 0) {
return; return;
@ -507,9 +506,12 @@ class PlejdService extends EventEmitter {
console.log(properties); console.log(properties);
const value = await properties.Get('Value'); const value = await properties['Value'];
if (!value) {
return;
}
const data = value.value; const data = value.value;
return;
const decoded = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data); const decoded = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data);
let state = 0; let state = 0;