This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
149 lines (132 loc) · 5.75 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
'use strict';
const utils = require('@iobroker/adapter-core');
const http = require('urllib');
class Windhager extends utils.Adapter {
constructor(options) {
super({
...options,
name: 'windhager',
});
this.on('ready', this.onReady.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
const self = this;
const windhagerIp = this.config.ip;
const windhagerLogin = this.config.login;
const windhagerPasswd = this.config.password;
const dataPath = 'datapoints';
this.log.info('Configured address: ' + windhagerIp);
this.log.debug('Configured login:' + windhagerLogin );
this.log.debug('Configured password:' + windhagerPasswd);
const connOptions = {
method: 'GET',
digestAuth: windhagerLogin + ':' + windhagerPasswd,
dataType: 'json'
};
http.request('http://' + windhagerIp + '/api/1.0/' + dataPath, connOptions, function (error, body, response) {
if (error || response.statusCode !== 200) {
self.log.error('Error' || {statusCode: response.statusCode});
} else {
self.log.debug('received data (' + response.statusCode + '): ' + body);
self.setObjectNotExistsAsync(dataPath + '.responseTime', {
type: 'state',
common: {
name: 'responseTime',
type: 'number',
role: 'value',
unit: 'ms',
read: true,
write: false
},
native: {}
});
self.setState(dataPath + '.responseTime', {val: parseInt(response.rt), ack: true});
self.setObjectNotExistsAsync(dataPath + '.JSON', {
type: 'state',
common: {
name: 'JSON',
type: 'string',
role: 'text',
read: 'true',
write: 'false'
},
native: {}
});
self.setState(dataPath + '.JSON', {val: JSON.stringify(body)});
const bodyObj = body;
var key, subkey, objectID;
for (let i = 0; i < bodyObj.length; i++) {
if (typeof bodyObj[i] !== 'undefined') {
self.log.debug(bodyObj[i].OID + ' ' + bodyObj[i].value + ' ' + bodyObj[i].unit);
for (key in bodyObj[i]) {
if (bodyObj[i].hasOwnProperty(key)) {
if (key == 'device') {
for (subkey in bodyObj[i][key]) {
if (objectID[i][key].hasOwnProperty(subkey)) {
self.setObjectNotExistsAsync(dataPath + bodyObj[i].OID.replace(/\//g, '.') + '.' + key + '.' + subkey, {
type: 'state',
common: {
name: subkey,
type: 'text',
role: 'text',
read: true,
write: false
},
native: {}
});
self.setState(dataPath + bodyObj[i].OID.replace(/\//g, '.') + '.' + key + '.' + subkey, {val: bodyObj[i][key][subkey], ack: true});
}
}
} else {
self.setObjectNotExistsAsync(dataPath + bodyObj[i].OID.replace(/\//g, '.') + '.' + key, {
type: 'state',
common: {
name: key,
type: 'string',
role: 'text',
read: true,
write: false
},
native: {}
});
self.setState(dataPath + bodyObj[i].OID.replace(/\//g, '.') + '.' + key, {val: bodyObj[i][key], ack: true});
}
}
}
}
}
}
});
setTimeout(this.stop.bind(this), 30000);
}
onUnload(callback) {
try {
this.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
onObjectChange(id, obj) {
if (obj) {
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
this.log.info(`object ${id} deleted`);
}
}
onStateChange(id, state) {
if (state) {
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
this.log.info(`state ${id} deleted`);
}
}
}
if (module.parent) {
module.exports = (options) => new Windhager(options);
} else {
new Windhager();
}