matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
63 lines • 2.43 kB
JavaScript
import { LOCAL_REFRESH_INTERVAL_MULTIPLIER } from '../constants/index.js';
/** Polls device status via local network or MQTT. */
export class PollingService {
refreshInterval;
logger;
messageRoutingService;
localIntervals = new Map();
constructor(refreshInterval, logger, messageRoutingService) {
this.refreshInterval = refreshInterval;
this.logger = logger;
this.messageRoutingService = messageRoutingService;
}
/** Start polling device status via local UDP. */
activateDeviceNotifyOverLocal(device) {
this.stopLocalPollingForDevice(device.duid);
this.logger.debug('Activating device status polling for:', device.duid);
const interval = setInterval(async () => {
try {
const messageDispatcher = this.messageRoutingService.getMessageDispatcher(device.duid);
if (!messageDispatcher) {
this.logger.error('Local Polling - No message dispatcher for device:', device.duid);
return;
}
await messageDispatcher.getDeviceStatus(device.duid);
}
catch (error) {
this.logger.error('Failed to get device status:', error);
}
}, this.refreshInterval * LOCAL_REFRESH_INTERVAL_MULTIPLIER);
this.localIntervals.set(device.duid, interval);
}
/** Trigger a one-shot local status request without affecting the recurring interval. */
async requestStatusOnce(duid) {
const messageDispatcher = this.messageRoutingService.getMessageDispatcher(duid);
if (!messageDispatcher)
return;
try {
await messageDispatcher.getDeviceStatus(duid);
}
catch (error) {
this.logger.error('Failed to get device status:', error);
}
}
/** Stop all polling intervals. */
stopPolling() {
for (const interval of this.localIntervals.values()) {
clearInterval(interval);
}
this.localIntervals.clear();
}
stopLocalPollingForDevice(duid) {
const interval = this.localIntervals.get(duid);
if (interval !== undefined) {
clearInterval(interval);
this.localIntervals.delete(duid);
}
}
/** Cleanup and shutdown. */
async shutdown() {
this.stopPolling();
}
}
//# sourceMappingURL=pollingService.js.map