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>} */
|
||||
devices = {};
|
||||
/** @private @type {Object.<string, number>} */
|
||||
mainBleIdByDeviceId = {};
|
||||
/** @private @type {Object.<string, string[]>} */
|
||||
outputDeviceUniqueIdsByRoomId = {};
|
||||
/** @private @type {Object.<number, string>} */
|
||||
|
|
@ -45,6 +47,10 @@ class DeviceRegistry {
|
|||
this.outputUniqueIdByBleOutputAddress[
|
||||
this.getUniqueBLEId(inputDevice.bleInputAddress, inputDevice.input)
|
||||
] = inputDevice.uniqueId;
|
||||
|
||||
if (!this.mainBleIdByDeviceId[inputDevice.deviceId]) {
|
||||
this.mainBleIdByDeviceId[inputDevice.deviceId] = inputDevice.bleInputAddress;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */
|
||||
|
|
@ -69,6 +75,9 @@ class DeviceRegistry {
|
|||
);
|
||||
|
||||
this.outputUniqueIdByBleOutputAddress[outputDevice.bleOutputAddress] = outputDevice.uniqueId;
|
||||
if (!this.mainBleIdByDeviceId[outputDevice.deviceId]) {
|
||||
this.mainBleIdByDeviceId[outputDevice.deviceId] = outputDevice.bleOutputAddress;
|
||||
}
|
||||
|
||||
if (!this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId]) {
|
||||
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = [];
|
||||
|
|
@ -179,6 +188,14 @@ class DeviceRegistry {
|
|||
return (this.inputDevices[uniqueInputId] || {}).name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} deviceId
|
||||
*/
|
||||
getMainBleIdByDeviceId(deviceId) {
|
||||
return this.mainBleIdByDeviceId[deviceId];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {string } deviceId The physical device serial number
|
||||
* @return {import('./types/ApiSite').Device}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,10 @@ class PlejBLEHandler extends EventEmitter {
|
|||
config;
|
||||
bleDevices = [];
|
||||
bus = null;
|
||||
/** @type {import('types/ApiSite').Device} */
|
||||
connectedDevice = null;
|
||||
/** @type Number? */
|
||||
connectedDeviceId = null;
|
||||
consecutiveWriteFails;
|
||||
consecutiveReconnectAttempts = 0;
|
||||
/** @type {import('./DeviceRegistry')} */
|
||||
|
|
@ -140,6 +143,7 @@ class PlejBLEHandler extends EventEmitter {
|
|||
|
||||
this.bleDevices = [];
|
||||
this.connectedDevice = null;
|
||||
this.connectedDeviceId = null;
|
||||
|
||||
this.characteristics = {
|
||||
data: null,
|
||||
|
|
@ -244,8 +248,8 @@ class PlejBLEHandler extends EventEmitter {
|
|||
await delay(this.config.connectionTimeout * 1000);
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const connectedPlejdDevice = await this._onDeviceConnected(plejd);
|
||||
if (connectedPlejdDevice) {
|
||||
const deviceWasConnected = await this._onDeviceConnected(plejd);
|
||||
if (deviceWasConnected) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -284,7 +288,7 @@ class PlejBLEHandler extends EventEmitter {
|
|||
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
|
||||
if (this.config.updatePlejdClock) {
|
||||
|
|
@ -770,9 +774,12 @@ class PlejBLEHandler extends EventEmitter {
|
|||
return null;
|
||||
}
|
||||
|
||||
logger.info('Connected device is a Plejd device with the right characteristics.');
|
||||
|
||||
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();
|
||||
|
||||
return this.connectedDevice;
|
||||
|
|
@ -887,12 +894,12 @@ class PlejBLEHandler extends EventEmitter {
|
|||
logger.warn(
|
||||
`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
|
||||
const newLocalTimestamp = now.getTime() / 1000 - offsetSecondsGuess;
|
||||
logger.info(`Setting time to ${now.toString()}`);
|
||||
const payload = this._createPayload(
|
||||
this.connectedDevice.id,
|
||||
this.connectedDeviceId,
|
||||
BLE_CMD_TIME_UPDATE,
|
||||
10,
|
||||
(pl) => pl.writeInt32LE(Math.trunc(newLocalTimestamp), 5),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue