homebridge-musiccast-bnetqc
Version:
Homebridge Yamaha MusicCast Multiroom Plugin with precise volume control
70 lines • 2.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cache = void 0;
class Cache {
constructor(log) {
this.hosts = new Set();
this.cache = {};
this.callbacks = {};
this.lastUserActivity = {};
this.lastPoweredOn = {};
this.lastStatusUpdate = {};
this.updateIntervalPoweredOff = 60 * 1000;
this.updateIntervalPoweredOn = 10 * 1000;
this.updateIntervalUserActivity = 1 * 1000;
this.log = log;
this.update();
}
async update() {
var promises = [];
for (var host of this.hosts) {
const now = new Date().getTime();
if ((this.lastStatusUpdate[host].getTime() <= (now - this.updateIntervalPoweredOff))
||
((this.lastStatusUpdate[host].getTime() <= (now - this.updateIntervalPoweredOn)) && (this.lastPoweredOn[host].getTime() >= (now - this.updateIntervalPoweredOff)))
||
((this.lastStatusUpdate[host].getTime() <= (now - this.updateIntervalUserActivity)) && (this.lastUserActivity[host].getTime() >= (now - this.updateIntervalPoweredOn)))) {
promises.push(this.updateHost(host));
}
}
setTimeout(async () => {
this.update();
}, 1000);
return Promise.all(promises);
}
async updateHost(host) {
this.lastStatusUpdate[host] = new Date();
if (host in this.callbacks) {
return this.callbacks[host].callback(...this.callbacks[host].parameters);
}
}
setCallback(host, callback, parameters) {
this.hosts.add(host);
this.lastUserActivity[host] = new Date();
this.lastPoweredOn[host] = new Date();
this.lastStatusUpdate[host] = new Date();
return this.callbacks[host] = { callback: callback, parameters: parameters };
}
ping(host, poweredOn, userActivity) {
if (poweredOn) {
this.lastPoweredOn[host] = new Date();
}
if (userActivity) {
this.lastUserActivity[host] = new Date();
}
}
set(host, key, value) {
if (!(host in this.cache)) {
this.cache[host] = {};
}
return this.cache[host][key] = value;
}
get(host, key) {
if (host in this.cache && key in this.cache[host]) {
return this.cache[host][key];
}
this.log.error(`cache not found ${host} ${key}`);
}
}
exports.Cache = Cache;
//# sourceMappingURL=Cache.js.map