diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index cf1b3c8..173ab09 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -7,6 +7,8 @@ class DeviceRegistry { /** @private @type {Object.} */ devices = {}; + /** @private @type {Object.} */ + mainBleIdByDeviceId = {}; /** @private @type {Object.} */ outputDeviceUniqueIdsByRoomId = {}; /** @private @type {Object.} */ @@ -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} diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index 6e943d8..e6d85f6 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -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),