@homebridge-plugins/homebridge-smarthq
Version:
The SmartHQ plugin allows you to interact with SmartHQ Devices in HomeKit and with Siri.
111 lines • 5.3 kB
JavaScript
import { timer, concat, of } from 'rxjs';
import { switchMap, skipWhile } from 'rxjs';
import { OpalDeviceBase } from '../OpalDeviceBase.js';
export class OpalMonitorManager extends OpalDeviceBase {
platform;
accessory;
device;
servicesSubscription = null;
schedulerSubscription = null;
statusSubscription = null;
opalIceMaker;
constructor(opalIceMaker, platform, accessory, device) {
super(platform, accessory, device);
this.platform = platform;
this.accessory = accessory;
this.device = device;
this.opalIceMaker = opalIceMaker;
}
getTimeUntilNextMinute() {
const now = new Date();
const remaining = (60 - now.getSeconds()) * 1000 - now.getMilliseconds();
return remaining > 0 ? remaining : 0;
}
// Create a timer that aligns with the minute boundary and executes immediately at alignment
createMinuteAlignedTimer(intervalMs) {
return timer(this.getTimeUntilNextMinute()).pipe(
// Start with an immediate emission, then continue with regular interval
switchMap(() => concat(of(0), // Emit 0 immediately when the timer reaches alignment
timer(intervalMs, intervalMs))));
}
// Start the monitoring
startServicesMonitoring() {
// Stop any existing subscription first
this.stopServicesMonitoring();
const refreshRate = (this.platform.config.options?.refreshRate || 30) * 1000;
this.servicesSubscription =
this.createMinuteAlignedTimer(refreshRate)
.pipe(skipWhile(() => !this.opalIceMaker.progressManager && !this.platform.config.options?.homekitControllerNotificationsSecret))
.subscribe(async () => {
try {
this.platform.debugLog('Running opal services monitoring tasks');
// These Notifications only alert in Homekit Controller
await this.opalIceMaker.filterMaintenanceManager.getFilterMaintenanceStatus();
await this.opalIceMaker.descaleManager.getDescaleStatus();
if (this.opalIceMaker.progressManager?.hasService()) {
const currentProductionValue = await this.opalIceMaker.progressManager.processProductionProgress();
this.opalIceMaker.powerManager.turnOffOnProductionLimitSurpassed(currentProductionValue);
}
}
catch (error) {
this.platform.errorLog(`Error in service monitoring: ${error}`);
}
});
this.platform.debugLog(`Opal services monitor starting in ${this.getTimeUntilNextMinute() / 1000} seconds, Interval: ${refreshRate / 1000} seconds`);
}
startStatusMonitoring() {
this.stopStatusMonitoring();
const refreshRate = (this.platform.config.options?.refreshRate || 30) * 1000;
this.statusSubscription =
this.createMinuteAlignedTimer(refreshRate)
.subscribe(async () => {
try {
this.platform.debugLog('Running opal status monitoring tasks');
await this.opalIceMaker.statusManager.getOpalCurrentStatus();
}
catch (error) {
this.platform.errorLog(`Error in status monitoring: ${error}`);
}
});
this.platform.debugLog(`Opal status monitor starting in ${this.getTimeUntilNextMinute() / 1000} seconds, Interval: ${refreshRate / 1000} seconds`);
}
startSchedulerMonitoring() {
this.stopSchedulerMonitoring();
const schedulingInterval = this.opalIceMaker.schedulingManager.schedulerInterval;
this.schedulerSubscription = this.createMinuteAlignedTimer(schedulingInterval)
.pipe(skipWhile(() => !this.platform.config.deviceOptions?.opal?.oplIceProductionSchedule))
.subscribe(() => {
try {
this.platform.debugLog('Running opal scheduler monitoring tasks');
this.opalIceMaker.schedulingManager.initializeIfIceMakerOnSchedule();
}
catch (error) {
this.platform.errorLog(`Error in scheduler monitoring: ${error}`);
}
});
this.platform.debugLog(`Opal scheduling monitor starting in ${this.getTimeUntilNextMinute() / 1000} seconds, Interval: ${schedulingInterval / 1000} seconds`);
}
stopSchedulerMonitoring() {
if (this.schedulerSubscription) {
this.schedulerSubscription.unsubscribe();
this.schedulerSubscription = null;
this.platform.debugLog('Stopped Schedule Monitoring');
}
}
stopStatusMonitoring() {
if (this.statusSubscription) {
this.statusSubscription.unsubscribe();
this.statusSubscription = null;
this.platform.debugLog('Stopped Status Monitoring');
}
}
// Stop the monitoring
stopServicesMonitoring() {
if (this.servicesSubscription) {
this.servicesSubscription.unsubscribe();
this.servicesSubscription = null;
this.platform.debugLog('Stopped ice maker monitoring');
}
}
}
//# sourceMappingURL=OpalMonitorManager.js.map