Set Plejd time only hourly when explicityly requesting

This commit is contained in:
Victor Hagelbäck 2021-02-18 20:21:57 +01:00
parent 12ec9a1b7c
commit 74716557c8

View file

@ -220,7 +220,7 @@ class PlejBLEHandler extends EventEmitter {
this.emit('connected'); this.emit('connected');
// Connected and authenticated, request current time and start ping // Connected and authenticated, request current time and start ping
this.requestCurrentPlejdTime(); this._requestCurrentPlejdTime();
this.startPing(); this.startPing();
this.startWriteQueue(); this.startWriteQueue();
@ -667,7 +667,7 @@ class PlejBLEHandler extends EventEmitter {
await this.onWriteSuccess(); await this.onWriteSuccess();
} }
async requestCurrentPlejdTime() { async _requestCurrentPlejdTime() {
logger.info('Requesting current Plejd time...'); logger.info('Requesting current Plejd time...');
// Eg: 0b0102001b: 0b: id, 0102: read, 001b: time // Eg: 0b0102001b: 0b: id, 0102: read, 001b: time
@ -683,7 +683,7 @@ class PlejBLEHandler extends EventEmitter {
shouldRetry: true, shouldRetry: true,
payload, payload,
}); });
setTimeout(() => this.requestCurrentPlejdTime(), 1000 * 3600); // Once per hour setTimeout(() => this._requestCurrentPlejdTime(), 1000 * 3600); // Once per hour
} }
startWriteQueue() { startWriteQueue() {
@ -946,9 +946,10 @@ class PlejBLEHandler extends EventEmitter {
logger.debug(`Plejd time update ${plejdTime.toString()}, diff ${diffSeconds} seconds`); logger.debug(`Plejd time update ${plejdTime.toString()}, diff ${diffSeconds} seconds`);
if (Math.abs(diffSeconds) > 60) { if (Math.abs(diffSeconds) > 60) {
logger.warn( logger.warn(
`Plejd time off by more than 1 minute. Reported time: ${plejdTime.toString()}, diff ${diffSeconds} seconds`, `Plejd time off by more than 1 minute. Reported time: ${plejdTime.toString()}, diff ${diffSeconds} seconds. Time will be set hourly.`,
); );
const newLocalTimestamp = now.getTime() / 1000 - offsetSecondsGuess; if (this.connectedDevice && deviceId === this.connectedDevice.id) {
const newLocalTimestamp = (now.getTime() - offsetSecondsGuess) / 1000;
logger.info(`Setting time to ${now.toString()}`); logger.info(`Setting time to ${now.toString()}`);
const payload = Buffer.alloc(10); const payload = Buffer.alloc(10);
// E.g: 00 0110 001b 38df2360 00 // E.g: 00 0110 001b 38df2360 00
@ -962,6 +963,7 @@ class PlejBLEHandler extends EventEmitter {
shouldRetry: true, shouldRetry: true,
payload, payload,
}); });
}
} else if (deviceId !== BLE_BROADCAST_DEVICE_ID) { } else if (deviceId !== BLE_BROADCAST_DEVICE_ID) {
logger.info('Got time response. Plejd time in sync with Home Assistant time'); logger.info('Got time response. Plejd time in sync with Home Assistant time');
} }