forked from lucacalcaterra/risco-mqtt-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanelPoller.js
More file actions
74 lines (65 loc) · 2.09 KB
/
panelPoller.js
File metadata and controls
74 lines (65 loc) · 2.09 KB
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
/* eslint linebreak-style: ["error", "windows"] */
/* eslint no-console: 0 */
// const Logger = require('logplease');
const EventEmitter = require('events');
const riscoLogger = require('./logger');
const RiscoConnection = require('./serverHandler.js');
const Config = require('./config/config');
module.exports = class RiscoPoller extends EventEmitter {
constructor(interval) {
super();
this.counter = -1;
this.interval = interval;
this.pollerStop = true;
this.timer = null;
this.riscoConn = new RiscoConnection({
username: Config.Conn.loginData.username,
password: Config.Conn.loginData.password,
pincode: Config.Conn.loginData.code,
},
riscoLogger);
}
async init() {
await this.riscoConn.login();
await this.riscoConn.sendSiteAndCode();
await this.riscoConn.getCameras();
await this.riscoConn.getEventHistory();
await this.riscoConn.getDetectors();
// check new panel status and emit
await this.riscoConn.getCPState();
this.emit('newpanelstatus');
riscoLogger.log('debug', 'Init function for getting data from Cloud completed: OK');
}
async poll() {
if (!this.pollerStop) {
this.timer = setTimeout(async () => {
riscoLogger.log('debug', 'polling ...');
this.counter += 1;
await this.riscoConn.getDetectors();
await this.riscoConn.getCPState();
this.emit('polled');
// check if is logged... if not init...
if (!this.riscoConn.isLogged) {
riscoLogger.log('warn', 'Disconnected from cloud...relogin and init...');
this.init();
}
if (this.riscoConn.riscoCPState !== null) {
this.emit('newpanelstatus');
riscoLogger.log('info', 'Status panel infos arrived');
}
if (!this.pollerStop) { await this.poll(); }
},
this.interval);
}
}
stop() {
this.pollerStop = true;
clearTimeout(this.timer);
riscoLogger.log('debug', 'polling stopped.');
}
async start() {
this.pollerStop = false;
riscoLogger.log('debug', 'polling started...');
await this.poll();
}
};