This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest-connector.js
120 lines (98 loc) · 3.03 KB
/
rest-connector.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
'use strict'
let http = require('http');
let async = require('async');
class RESTConnector {
constructor(baseURL, port) {
this.baseURL = baseURL;
this.port = port;
this.lastSentAlarmTime = 0;
this.sentAlarms = [];
}
sendPing(ping) {
let promise = new Promise((resolve, reject) => {
this.sendPOST(JSON.stringify(ping), '/ping').then(() => {
resolve();
});
});
return promise;
}
sendAlarm(alarm) {
// We should only send a new alarm in case the last alarm was more than
// 10 minutes ago (= 10*60*1000 ms = 600'000 ms)
// Further, if the priority is higher than 1 (2 and above), we need to send
// the alarm any time!
// We need to exclude the temperature alarm in this case, since we want to send
// an alarm every time the difference is too big.
let currentTime = Date.now();
if (alarm.type !== 'difference' && alarm.type !== 'high' && alarm.priority < 2 && currentTime - this.lastSentAlarmTime < 600000) {
console.log('NOT SENDING ALARM SINCE IT HASNT BEEN 10 MINUTES');
return new Promise((resolve, reject) => { resolve(); });
}
let promise = new Promise((resolve, reject) => {
this.sendPOST(JSON.stringify(alarm), '/alarm').then(() => {
this.lastSentAlarmTime = Date.now();
this.sentAlarms.push(alarm);
console.log('Alarm ' + alarm.id + ' sent!');
console.log(this.sentAlarms);
resolve();
});
});
return promise;
}
resendAlarms() {
console.log('Starting to resend alarms!');
let unsentAlarms = this.sentAlarms.filter((alarm) => {
return !alarm.confirmed;
});
let promise = new Promise((resolve, reject) => {
async.each(unsentAlarms, (alarm, callback) => {
this.sendPOST(JSON.stringify(alarm), '/alarm').then(() => {
callback();
});
}, (err) => {
if (err) {
console.log('There was an error resending the alarms.');
}
console.log('All alarms resent!');
resolve();
});
});
return promise;
}
sendPOST(paket, endpoint) {
console.log('Starting to send ping');
let promise = new Promise((resolve, reject) => {
let postData = {
data: paket
};
postData = JSON.stringify(postData);
let options = {
hostname: this.baseURL,
port: this.port,
path: endpoint,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
let requestToSend = http.request(options, (res) => {
res.on('data', (chunk) => {
console.log('Received: ' + chunk);
});
res.on('end', () => {
console.log('Ping saved!');
resolve();
});
});
requestToSend.on('error', (err) => {
console.log('problem with request:' + err.message);
reject();
});
requestToSend.write(postData);
requestToSend.end();
});
return promise;
}
}
module.exports = RESTConnector;