-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZWayHoleDevice.js
59 lines (57 loc) · 2.36 KB
/
ZWayHoleDevice.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
'use strict';
schedule('* * * * *', function () {
let dp_string = '';
let dp_array = new Array();
let j = 0;
let timestamp = getState('ZwayLetzteAbfrage');
let zway_url = getState('ZwayUrl');
let dp_namespace = getState('ZwayNamensraum');
let zway_user = getState('ZwayBenutzer');
let zway_pass = getState('ZwayPasswort');
request.get(zway_url.val + '/ZAutomation/api/v1/devices?since=' + timestamp.val,
function (error, response, body) {
if (!error && response.statusCode == 200) {
let result = JSON.parse(response.body);
if(getState('ZwayLog').val==1){
log('Anzahl: ' + result.data.devices.length);
}
if(getState('ZwayLog').val==1){
log('Strukturänderung: ' + result.data.structureChanged);
}
for(let i=0;i<result.data.devices.length;i++){
jsonRunner(result.data.devices[i], dp_namespace.val + '.' + result.data.devices[i].id);
}
for(let k=0;k<dp_array.length;k++){
let temp_array = dp_array[k].split(',');
if(!existsState(temp_array[0])){
createState(temp_array[0], temp_array[1].trim());
}
else{
setState(temp_array[0], temp_array[1].trim());
}
temp_array = undefined;
}
} else {
log('Fehler: ' + error + response.statusCode);
}
let newtimestamp = Date.now();
newtimestamp = (Math.floor(newtimestamp/1000) - 3600);
setState('ZwayLetzteAbfrage', newtimestamp);
if(getState('ZwayLog').val==1){
log('Zeitstempel: ' + newtimestamp);
}
}).auth(zway_user.val, zway_pass.val, false);
function jsonRunner(obj, path) {
for (let key in obj) {
if (obj.hasOwnProperty(key) && (typeof obj[key] === 'object')) {
dp_string=key + '.';
jsonRunner(obj[key],path);
dp_string='';
} else {
let dp_temp = path + '.' + dp_string + key + ', ' + obj[key];
dp_array[j] = dp_temp;
j++;
}
}
}
})