-
Notifications
You must be signed in to change notification settings - Fork 17
/
fakegato-timer.js
133 lines (117 loc) · 3.41 KB
/
fakegato-timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*jshint esversion: 6,node: true,-W041: false */
'use strict';
const DEBUG = true;
class FakeGatoTimer {
constructor(params) {
if (!params)
params = {};
this.subscribedServices = [];
this.minutes = params.minutes || 10;
this.intervalID = null;
this.running = false;
this.log = params.log || {};
if (!this.log.debug) {
this.log.debug = DEBUG ? console.log : function () { };
}
}
// Subscription management
subscribe(service, callback) {
this.log.debug("** Fakegato-timer Subscription :", service.accessoryName);
let newService = {
'service': service,
'callback': callback,
'backLog': [],
'previousBackLog': [],
'previousAvrg': {}
};
this.subscribedServices.push(newService);
}
getSubscriber(service) {
let findServ = function (element) {
return element.service === service;
};
return this.subscribedServices.find(findServ);
}
_getSubscriberIndex(service) {
let findServ = function (element) {
return element.service === service;
};
return this.subscribedServices.findIndex(findServ);
}
getSubscribers() {
return this.subscribedServices;
}
unsubscribe(service) {
let index = this._getSubscriberIndex(service);
this.subscribedServices.splice(index, 1);
if (this.subscribedServices.length === 0 && this.running)
this.stop();
}
// Timer management
start() {
this.log.debug("**Start Global Fakegato-Timer - " + this.minutes + "min**");
if (this.running)
this.stop();
this.running = true;
this.intervalID = setInterval(this.executeCallbacks.bind(this), this.minutes * 60 * 1000);
}
stop() {
this.log.debug("**Stop Global Fakegato-Timer****");
clearInterval(this.intervalID);
this.running = false;
this.intervalID = null;
}
// Data management
executeCallbacks() {
this.log.debug("**Fakegato-timer: executeCallbacks**");
if (this.subscribedServices.length !== 0) {
for (let s in this.subscribedServices) {
if (this.subscribedServices.hasOwnProperty(s)) {
let service = this.subscribedServices[s];
if (typeof (service.callback) == 'function') {
service.previousAvrg = service.callback({
'backLog': service.backLog,
'previousAvrg': service.previousAvrg,
'timer': this,
'immediate': false
});
}
}
}
}
}
executeImmediateCallback(service) {
this.log.debug("**Fakegato-timer: executeImmediateCallback**");
if (typeof (service.callback) == 'function' && service.backLog.length)
service.callback({
'backLog': service.backLog,
'timer': this,
'immediate': true
});
}
addData(params) {
let data = params.entry;
let service = params.service;
let immediateCallback = params.immediateCallback || false;
this.log.debug("**Fakegato-timer: addData ", service.accessoryName, data, " immediate: ", immediateCallback);
if (immediateCallback) // door or motion -> replace
this.getSubscriber(service).backLog[0] = data;
else
this.getSubscriber(service).backLog.push(data);
if (immediateCallback) {
//setTimeout(this.executeImmediateCallback.bind(this), 0,service);
this.executeImmediateCallback(this.getSubscriber(service));
}
if (this.running === false)
this.start();
}
emptyData(service) {
this.log.debug("**Fakegato-timer: emptyData **", service.accessoryName);
let source = this.getSubscriber(service);
if (source.backLog.length) source.previousBackLog = source.backLog;
source.backLog = [];
}
}
module.exports = {
FakeGatoTimer: FakeGatoTimer
};