homebridge-roborock-control
Version:
A Homebridge plugin to control Roborock vacuum cleaners.
53 lines • 2.2 kB
JavaScript
import { Log } from '../util/log.js';
/**
* Base class to implement common polling functionality for accessories.
*/
export class PollingAccessory {
platform;
accessory;
refreshInterval;
static kDefaultRefreshInterval = 10000;
_currentState = {};
constructor(platform, accessory, refreshInterval = PollingAccessory.kDefaultRefreshInterval) {
this.platform = platform;
this.accessory = accessory;
this.refreshInterval = refreshInterval;
// Begin monitoring the device for state changes.
setInterval(() => this.refreshDeviceState(), this.refreshInterval);
}
// Get the device state and push to Homekit if it has changed. This is called
// periodically to keep the state fresh, and can also be invoked by subclasses
// when (for instance) Homekit sets a new value for a device's properties.
async refreshDeviceState() {
// Retrieve the most recent device data.
const lastState = structuredClone(this._currentState);
this._currentState = await this.getDeviceState(lastState);
// Check whether the derived class' services have been initialized.
if (!this.servicesReady()) {
Log.debug('Services not yet initialized:', this.accessory.displayName);
return;
}
// Check whether any attributes have changed.
if (JSON.stringify(lastState) !== JSON.stringify(this._currentState)) {
Log.debug(`Updating ${this.accessory.displayName}:`, this._currentState);
this.updateHomekitState(this.currentState());
}
}
// Returns a clone of the current state object.
currentState() {
return structuredClone(this._currentState);
}
// Implemented by subclasses. Confirms that all accessory services are ready.
servicesReady() {
return false;
}
// Implemented by subclasses. Returns an object with the current device state.
async getDeviceState(lastState) {
return {};
}
// Implemented by subclasses. Updates Homekit with the current state.
async updateHomekitState(currentState) {
// TBI
}
}
//# sourceMappingURL=pollingAccessory.js.map