Improve user-facing logging and BLE id handling in device registry and PlejdBLEHandler
- Resolves #265
This commit is contained in:
parent
ae8e41ec41
commit
75306827ae
2 changed files with 31 additions and 7 deletions
|
|
@ -7,6 +7,8 @@ class DeviceRegistry {
|
||||||
|
|
||||||
/** @private @type {Object.<string, import('types/ApiSite').Device>} */
|
/** @private @type {Object.<string, import('types/ApiSite').Device>} */
|
||||||
devices = {};
|
devices = {};
|
||||||
|
/** @private @type {Object.<string, number>} */
|
||||||
|
mainBleIdByDeviceId = {};
|
||||||
/** @private @type {Object.<string, string[]>} */
|
/** @private @type {Object.<string, string[]>} */
|
||||||
outputDeviceUniqueIdsByRoomId = {};
|
outputDeviceUniqueIdsByRoomId = {};
|
||||||
/** @private @type {Object.<number, string>} */
|
/** @private @type {Object.<number, string>} */
|
||||||
|
|
@ -45,6 +47,10 @@ class DeviceRegistry {
|
||||||
this.outputUniqueIdByBleOutputAddress[
|
this.outputUniqueIdByBleOutputAddress[
|
||||||
this.getUniqueBLEId(inputDevice.bleInputAddress, inputDevice.input)
|
this.getUniqueBLEId(inputDevice.bleInputAddress, inputDevice.input)
|
||||||
] = inputDevice.uniqueId;
|
] = inputDevice.uniqueId;
|
||||||
|
|
||||||
|
if (!this.mainBleIdByDeviceId[inputDevice.deviceId]) {
|
||||||
|
this.mainBleIdByDeviceId[inputDevice.deviceId] = inputDevice.bleInputAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
||||||
|
|
@ -69,6 +75,9 @@ class DeviceRegistry {
|
||||||
);
|
);
|
||||||
|
|
||||||
this.outputUniqueIdByBleOutputAddress[outputDevice.bleOutputAddress] = outputDevice.uniqueId;
|
this.outputUniqueIdByBleOutputAddress[outputDevice.bleOutputAddress] = outputDevice.uniqueId;
|
||||||
|
if (!this.mainBleIdByDeviceId[outputDevice.deviceId]) {
|
||||||
|
this.mainBleIdByDeviceId[outputDevice.deviceId] = outputDevice.bleOutputAddress;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId]) {
|
if (!this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId]) {
|
||||||
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = [];
|
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = [];
|
||||||
|
|
@ -179,6 +188,14 @@ class DeviceRegistry {
|
||||||
return (this.inputDevices[uniqueInputId] || {}).name;
|
return (this.inputDevices[uniqueInputId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} deviceId
|
||||||
|
*/
|
||||||
|
getMainBleIdByDeviceId(deviceId) {
|
||||||
|
return this.mainBleIdByDeviceId[deviceId];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string } deviceId The physical device serial number
|
* @param {string } deviceId The physical device serial number
|
||||||
* @return {import('./types/ApiSite').Device}
|
* @return {import('./types/ApiSite').Device}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,10 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
config;
|
config;
|
||||||
bleDevices = [];
|
bleDevices = [];
|
||||||
bus = null;
|
bus = null;
|
||||||
|
/** @type {import('types/ApiSite').Device} */
|
||||||
connectedDevice = null;
|
connectedDevice = null;
|
||||||
|
/** @type Number? */
|
||||||
|
connectedDeviceId = null;
|
||||||
consecutiveWriteFails;
|
consecutiveWriteFails;
|
||||||
consecutiveReconnectAttempts = 0;
|
consecutiveReconnectAttempts = 0;
|
||||||
/** @type {import('./DeviceRegistry')} */
|
/** @type {import('./DeviceRegistry')} */
|
||||||
|
|
@ -140,6 +143,7 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
|
|
||||||
this.bleDevices = [];
|
this.bleDevices = [];
|
||||||
this.connectedDevice = null;
|
this.connectedDevice = null;
|
||||||
|
this.connectedDeviceId = null;
|
||||||
|
|
||||||
this.characteristics = {
|
this.characteristics = {
|
||||||
data: null,
|
data: null,
|
||||||
|
|
@ -244,8 +248,8 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
await delay(this.config.connectionTimeout * 1000);
|
await delay(this.config.connectionTimeout * 1000);
|
||||||
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
const connectedPlejdDevice = await this._onDeviceConnected(plejd);
|
const deviceWasConnected = await this._onDeviceConnected(plejd);
|
||||||
if (connectedPlejdDevice) {
|
if (deviceWasConnected) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -284,7 +288,7 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
throw new Error('Could not connect to any Plejd device');
|
throw new Error('Could not connect to any Plejd device');
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`BLE Connected to ${this.connectedDevice.name}`);
|
logger.info(`BLE Connected to ${this.connectedDevice.title}`);
|
||||||
|
|
||||||
// Connected and authenticated, request current time and start ping
|
// Connected and authenticated, request current time and start ping
|
||||||
if (this.config.updatePlejdClock) {
|
if (this.config.updatePlejdClock) {
|
||||||
|
|
@ -770,9 +774,12 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('Connected device is a Plejd device with the right characteristics.');
|
|
||||||
|
|
||||||
this.connectedDevice = device.device;
|
this.connectedDevice = device.device;
|
||||||
|
this.connectedDeviceId = this.deviceRegistry.getMainBleIdByDeviceId(this.connectedDevice.deviceId);
|
||||||
|
|
||||||
|
logger.verbose('The connected Plejd device has the right charecteristics!');
|
||||||
|
logger.info(`Connected to Plejd device ${this.connectedDevice.title} (${this.connectedDevice.deviceId}, BLE id ${this.connectedDeviceId}).`);
|
||||||
|
|
||||||
await this._authenticate();
|
await this._authenticate();
|
||||||
|
|
||||||
return this.connectedDevice;
|
return this.connectedDevice;
|
||||||
|
|
@ -887,12 +894,12 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Plejd clock time off by more than 1 minute. Reported time: ${plejdTime.toString()}, diff ${diffSeconds} seconds. Time will be set hourly.`,
|
`Plejd clock time off by more than 1 minute. Reported time: ${plejdTime.toString()}, diff ${diffSeconds} seconds. Time will be set hourly.`,
|
||||||
);
|
);
|
||||||
if (this.connectedDevice && bleOutputAddress === this.connectedDevice.id) {
|
if (this.connectedDevice && bleOutputAddress === this.connectedDeviceId) {
|
||||||
// Requested time sync by us
|
// Requested time sync by us
|
||||||
const newLocalTimestamp = now.getTime() / 1000 - offsetSecondsGuess;
|
const newLocalTimestamp = now.getTime() / 1000 - offsetSecondsGuess;
|
||||||
logger.info(`Setting time to ${now.toString()}`);
|
logger.info(`Setting time to ${now.toString()}`);
|
||||||
const payload = this._createPayload(
|
const payload = this._createPayload(
|
||||||
this.connectedDevice.id,
|
this.connectedDeviceId,
|
||||||
BLE_CMD_TIME_UPDATE,
|
BLE_CMD_TIME_UPDATE,
|
||||||
10,
|
10,
|
||||||
(pl) => pl.writeInt32LE(Math.trunc(newLocalTimestamp), 5),
|
(pl) => pl.writeInt32LE(Math.trunc(newLocalTimestamp), 5),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue