-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
executable file
·311 lines (248 loc) · 7.29 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* jshint -W097 */// jshint strict:false
/* jslint node: true */
"use strict";
var xml2js = require('xml2js');
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var request = require('request');
// Set the headers
var headers = {
'User-Agent': 'User Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
}
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file
// name excluding extension
// adapter will be restarted automatically every time as the configuration
// changed, e.g system.adapter.mobile-alerts.0
var adapter = utils.adapter('mobile-alerts');
// is called when adapter shuts down - callback has to be called under any
// circumstances!
adapter.on('unload', function(callback) {
try {
adapter.log.debug('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed object changes
adapter.on('objectChange', function(id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.debug('objectChange ' + id + ' ' + JSON.stringify(obj));
});
// is called if a subscribed state changes
adapter.on('stateChange', function(id, state) {
// Warning, state can be null if it was deleted
adapter.log.debug('stateChange ' + id + ' ' + JSON.stringify(state));
// you can use the ack flag to detect if it is status (true) or command
// (false)
if (state && !state.ack) {
adapter.log.debug('ack is not set!');
}
});
// Some message was sent to adapter instance over message box. Used by email,
// pushover, text2speech, ...
adapter.on('message', function(obj) {
if (typeof obj == 'object' && obj.message) {
if (obj.command == 'send') {
// e.g. send email or pushover or whatever
console.log('send command');
// Send response in callback if required
if (obj.callback)
adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function() {
main();
});
function main() {
var methodName = "main";
adapter.log.debug("in: " + methodName + " v0.5.1");
// All states changes inside the adapter's namespace are subscribed
adapter.subscribeStates('*');
var ma_hostname = adapter.config.hostname;
var ma_phoneId = adapter.config.phoneId;
var ma_path = adapter.config.path;
var ma_extractNumbers = adapter.config.extractNumbers;
// Configure the request
var options = {
url: 'https://' + ma_hostname + ma_path,
method: 'POST',
headers: headers,
form: {'phoneid': ma_phoneId}
}
request(options, function (error, response, body) {
if (error || response.statusCode != 200) {
if (error !== null) { adapter.log.error('error: ' + error); }
adapter.log.error('response.statusCode: ' + response.statusCode);
adapter.log.debug('body: ' + body);
adapter.log.error('Exit: No valid response from Mobile Alerts server');
setTimeout(function() {
process.exit(-2);
}, 2000);
} else {
adapter.log.debug('Data received from Mobile Alerts server');
var data = body.toString().match(/(<h[45]>.*<\/h[45]>|<\/?body>|<a .*deviceid=.*<\/a>)/gim);
if (!data || !data.toString()) {
setTimeout(function() {
adapter.log.error('Exit: Data not matched');
adapter.log.debug('body: ' + body);
process.exit(-3);
}, 2000);
} else {
parseData(data.toString());
}
}
});
// Force terminate
setTimeout(function() {
adapter.log.error('Exit: Timeout. Termination forced!');
process.exit(-1);
}, 4 * 60000);
adapter.log.debug("out: " + methodName);
}
function parseData(xml) {
var methodName = "parseData";
adapter.log.debug("in: " + methodName);
adapter.log.debug("parameter: xml: " + xml);
var options = {
explicitArray : false,
mergeAttrs : true
};
var parser = new xml2js.Parser(options);
parser.parseString(xml, function(err, result) {
if (err || result == undefined || (result.a == undefined || result.h4 == undefined || result.h5 == undefined)) {
var msg;
if (err) {
msg = err;
} else {
adapter.log.debug("result: " + JSON.stringify(result));
msg = "Matching HTML tags not found";
}
adapter.log.error("parseString error: " + msg);
} else {
adapter.log.debug("result: " + JSON.stringify(result));
setDevices(result);
setObjects(result);
setStates(result);
}
});
setTimeout(function() {
process.exit(0);
}, 10000);
adapter.log.debug("out: " + methodName);
}
function setDevices(result) {
var methodName = "setDevices";
adapter.log.debug("in: " + methodName);
var i = 0;
var deviceId = "";
while (result.a[i] != undefined) {
var deviceName = result.a[i]["#"].trim();
var href = result.a[i]["href"];
var deviceId = getHrefParamValue(href, "deviceid");
adapter.log.debug(deviceName + ": " + deviceId);
adapter.setObjectNotExists(deviceId, {
type : 'device',
common : {
name : deviceName,
type : 'string',
role : 'sensor',
ack : true
},
native : {}
});
i++;
}
adapter.log.debug("out: " + methodName);
}
function setObjects(result) {
var methodName = "setObjects";
adapter.log.debug("in: " + methodName);
var i = 0;
var deviceId = "";
while ((result.a[i] != undefined || result.h4[i] != undefined || result.h5[i] != undefined)) {
if (result.h4[i] != undefined && result.h4[i] != undefined) {
var key = result.h5[i];
var value = result.h4[i];
if (key == "ID") {
deviceId = value;
} else {
var normalizedKey = normalize(key);
adapter.setObjectNotExists(deviceId + "." + normalizedKey, {
type : 'state',
common : {
name : key,
type : 'string',
role : 'data',
ack : true
},
native : {}
});
}
}
i++;
}
adapter.log.debug("out: " + methodName);
}
function setStates(result) {
var methodName = "setStates";
adapter.log.debug("in: " + methodName);
var i = 0;
var deviceId = "";
while ((result.a[i] != undefined || result.h4[i] != undefined || result.h4[i] != undefined)) {
if (result.h4[i] != undefined && result.h4[i] != undefined) {
var key = result.h5[i];
var value = result.h4[i];
if (key == "ID") {
deviceId = value;
} else {
adapter.setState(deviceId + "." + normalize(key), {
val : formatNum(value),
ack : true
});
}
}
i++;
}
adapter.log.debug("out: " + methodName);
}
// Utils
function formatNum(s) {
if (extractNumbers == undefined) {
var extractNumbers = adapter.config.extractNumbers;
if (extractNumbers && patternNumber == undefined) {
var patternNumber = /^(-|\+)?\d+(,|\.)?\d* *(C|F|%|mm|km\/h|hPa|m\/s)$/;
}
}
if (extractNumbers && patternNumber.test(s)) {
s = s.replace(",", ".");
s = parseFloat(s).toString();
}
return s;
}
function normalize(s) {
s = s.toLowerCase();
s = s.replace(" ", "_");
s = s.replace("ä", "ue");
s = s.replace("ö", "oe");
s = s.replace("ü", "ue");
s = s.replace("ß", "ss");
return s;
}
function getHrefParamValue(href, paramName) {
var paramValue;
var params = href.split("?")[1];
params = params.split("&");
for (var i = 0; i < params.length; i++) {
var param = params[i].split("="); // ['this','true'], ...
if (param[0] == paramName) {
paramValue = param[1];
break;
}
}
return paramValue;
}