UNPKG

matterbridge-roborock-vacuum-plugin

Version:
61 lines 2.29 kB
import { RvcOperationalState } from 'matterbridge/matter/clusters'; import { BURST_POLLING_INTERVAL_MS } from '../constants/index.js'; /** * Manages burst polling for robot vacuum devices. * Polls at a high frequency after a vacuum command is triggered, * stopping automatically when the device returns to an idle/docked state. */ export class BurstPollingManager { platform; timers = new Map(); constructor(platform) { this.platform = platform; } startBurstPolling(duid) { if (this.timers.has(duid)) return; const timer = setInterval(async () => { try { this.platform.log.notice(`Burst polling for a specific device: ${duid}`); await this.requestLocalDeviceStatus(duid); if (this.isDeviceIdle(duid)) { this.stopBurstPolling(duid); } } catch (error) { this.platform.log.error(`Burst polling failed: ${error instanceof Error ? error.message : String(error)}`); } }, BURST_POLLING_INTERVAL_MS); this.timers.set(duid, timer); } stopBurstPolling(duid) { const timer = this.timers.get(duid); if (timer !== undefined) { this.platform.log.notice(`Stop burst polling for a specific device: ${duid}`); clearInterval(timer); this.timers.delete(duid); } } stopAllBurstPolling() { for (const duid of this.timers.keys()) { this.stopBurstPolling(duid); } } has(duid) { return this.timers.has(duid); } async requestLocalDeviceStatus(duid) { const { roborockService } = this.platform; if (roborockService === undefined) return; await roborockService.requestDeviceStatusOnce(duid); } isDeviceIdle(duid) { const robot = this.platform.registry.robotsMap.get(duid); if (!robot) return true; const state = robot.getAttribute(RvcOperationalState.Cluster.id, 'operationalState', this.platform.log); return (state === RvcOperationalState.OperationalState.Docked || state === RvcOperationalState.OperationalState.Charging); } } //# sourceMappingURL=burstPollingManager.js.map