This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
397 lines (335 loc) · 15 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const adapter = new utils.Adapter('primelab');
const querystring = require('querystring');
const http = require('https');
const zlib = require('zlib');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
main();
});
let cookie;
function main() {
login((data) => {
adapter.log.info('Erfolgreich angemeldet');
cookie = data;
getAccountID(data, (err, accountLink)=>{
getData(cookie, accountLink);
});
});
}
function login(callback){
let post_data = querystring.stringify({'usrname': adapter.config.username, 'passwd': adapter.config.password});
//adapter.log.info('Query String: ' + post_data);
let options = {host: 'primelab.cloud', port: 443, path: '/index.php?id=3', method: 'POST', headers: {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Langugage': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(post_data), 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}};
let post_req = http.request(options, (res) => {
adapter.log.debug('Login cookie: ' + res.headers['set-cookie']);
adapter.log.debug('Status Code Login: ' + res.headers.location);
res.setEncoding('utf8');
let cookie = res.headers['set-cookie'];
callback(cookie);
res.on('data', (chunk) => {
adapter.log.debug('Response: ' + chunk);
});
res.on('end', (chunk) => {
adapter.log.debug('On End: ' + chunk);
// print to console when response ends
});
});
post_req.write(post_data);
post_req.end();
}
function getAccountID(cookie, callback){
cookie = cookie.toString().split(";");
adapter.log.debug('Get Account Cookie: ' + cookie[0]);
let options = {host: 'primelab.cloud', port: 443, path: '/index.php?id=2', method: 'GET', headers: {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Langugage': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7', 'Connection': 'keep-alive', 'Cookie': cookie[0], 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}};
const gunzip = zlib.createGunzip();
let compressed = (options, callback)=> {
let get = http.get(options);
let buffer = [];
get.on('response', (res) => {
if (res.statusCode !== 200) throw new Error('Status not 200');
res.pipe(gunzip);
gunzip.on('data', (data) => {
buffer.push(data.toString());
}).on("end", () => {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", (e) => {
callback(e);
})
}).on('error', (e) => {
callback(e)
});
get.on('error', (err) => {
throw err;
})
};
compressed(options, (err, data)=>{
adapter.log.debug('Data for getAccountID: ' + data);
const dom = new JSDOM(data);
let accountLink = dom.window.document.querySelector("#accountNavigation a").href;
callback(null, accountLink);
})
}
function getData(cookie, accountLink){
adapter.log.debug('Account Link: ' + accountLink);
cookie = cookie.toString().split(";");
adapter.log.debug('Get Data Cookie: ' + cookie[0]);
let options = {host: 'primelab.cloud', port: 443, path: `/${accountLink}`, method: 'GET', headers: {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Langugage': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7', 'Connection': 'keep-alive', 'Cookie': cookie[0], 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}};
const gunzip = zlib.createGunzip();
let compressed = (options, callback)=> {
let get = http.get(options);
let buffer = [];
get.on('response', (res) => {
if (res.statusCode !== 200) throw new Error('Status not 200');
res.pipe(gunzip);
gunzip.on('data', (data) => {
buffer.push(data.toString());
}).on("end", () => {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", (e) => {
callback(e);
})
}).on('error', (e) => {
callback(e)
});
get.on('error', (err) => {
throw err;
})
};
compressed(options, (err, data)=>{
adapter.log.debug('Data for getData: ' + data);
const dom = new JSDOM(data);
//get number of all dataset's
let nrOfData = dom.window.document.querySelector("tbody").childNodes.length /2;
let cyanuric = false;
let chlorineT = false;
let chlorineF = false;
let ph = false;
let alkalinity = false;
for(let i = 0; i < nrOfData; i++) {
let z = i +1;
//get Data
let set = dom.window.document.querySelector("tbody tr:nth-child("+z+")").textContent;
set = set.split('\n');
//Data to variable
let userdate = set[2];
let serial = set[4];
let scenario = set[6];
let parameter = set[7];
let measurement = set[8];
let unit = set[9];
let sn = serial.replace(/-/g, '_');
let id = scenario.replace(/-/g, '_');
adapter.setObjectNotExists(sn, {
type: 'device',
common: {
name: serial,
role: 'sensor',
read: true,
write: false
}
});
//generate Timestamp
let t = new Date(userdate * 1000);
let ts = t.toLocaleString();
switch (scenario) {
case '431-Cyanuric-Acid':
if(!cyanuric){
adapter.setObjectNotExists(`${sn}.${id}`, {
type: 'channel',
common: {
name: parameter,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.measurement`, {
type: 'state',
common: {
name: 'Measurement of Cyanuric-Acid',
role: 'value',
type: 'number',
unit: unit,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.timestamp`, {
type: 'state',
common: {
name: 'Timestamp for Cyanuric-Acid',
role: 'indicator.timestamp',
type: 'number',
read: true,
write: false
}
});
adapter.setState(`${sn}.${id}.measurement`, measurement, true);
adapter.setState(`${sn}.${id}.timestamp`, ts, true);
cyanuric = true;
}
break;
case '421-Chlorine-Total':
if(!chlorineT){
adapter.setObjectNotExists(`${sn}.${id}`, {
type: 'channel',
common: {
name: parameter,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.measurement`, {
type: 'state',
common: {
name: 'Measurement of Chlorine-Total',
role: 'value',
type: 'number',
unit: unit,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.timestamp`, {
type: 'state',
common: {
name: 'Timestamp for Chlorine-Total',
role: 'indicator.timestamp',
type: 'number',
read: true,
write: false
}
});
adapter.setState(`${sn}.${id}.measurement`, measurement, true);
adapter.setState(`${sn}.${id}.timestamp`, ts, true);
chlorineT = true;
}
break;
case '428-Chlorine-Free':
if(!chlorineF){
adapter.setObjectNotExists(`${sn}.${id}`, {
type: 'channel',
common: {
name: parameter,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.measurement`, {
type: 'state',
common: {
name: 'Measurement of Chlorine-Free',
role: 'value',
type: 'number',
unit: unit,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.timestamp`, {
type: 'state',
common: {
name: 'Timestamp for Chlorine-Free',
role: 'indicator.timestamp',
type: 'number',
read: true,
write: false
}
});
adapter.setState(`${sn}.${id}.measurement`, measurement, true);
adapter.setState(`${sn}.${id}.timestamp`, ts, true);
chlorineF = true;
}
break;
case '429-pH-PoolLab':
if(!ph){
adapter.setObjectNotExists(`${sn}.${id}`, {
type: 'channel',
common: {
name: parameter,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.measurement`, {
type: 'state',
common: {
name: 'Measurement of pH-PoolLab',
role: 'value',
type: 'number',
unit: unit,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.timestamp`, {
type: 'state',
common: {
name: 'Timestamp for pH-PoolLab',
role: 'indicator.timestamp',
type: 'number',
read: true,
write: false
}
});
adapter.setState(`${sn}.${id}.measurement`, measurement, true);
adapter.setState(`${sn}.${id}.timestamp`, ts, true);
ph = true;
}
break;
case '430-Alkalinity-Total':
if(!alkalinity){
adapter.setObjectNotExists(`${sn}.${id}`, {
type: 'channel',
common: {
name: parameter,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.measurement`, {
type: 'state',
common: {
name: 'Measurement of Alkalinity-Total',
role: 'value',
type: 'number',
unit: unit,
read: true,
write: false
}
});
adapter.setObjectNotExists(`${sn}.${id}.timestamp`, {
type: 'state',
common: {
name: 'Timestamp for Alkalinity-Total',
role: 'indicator.timestamp',
type: 'number',
read: true,
write: false
}
});
adapter.setState(`${sn}.${id}.measurement`, measurement, true);
adapter.setState(`${sn}.${id}.timestamp`, ts, true);
alkalinity = true;
}
break;
}
}
adapter.stop();
})
}