-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
298 lines (266 loc) · 11.4 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
/**
* Tinker Board-Monitor Adapter
*
* License: MIT
*/
/* jshint -W097 */
/* jshint strict: false */
/*jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const exec = require('child_process').execSync;
const adapterName = require('./package.json').name.split('.').pop();
let adapter;
let timer;
let config;
let objects;
let tinker = {};
let table = {};
let errorsLogged = {};
function startAdapter(options) {
options = options || {};
Object.assign(options, { name: adapterName });
adapter = new utils.Adapter(options);
adapter.on('ready', () => {
config = adapter.config;
if (adapter.config.forceinit) {
adapter.getObjectList({ startkey: adapter.name + '.' + adapter.instance, endkey: adapter.name + '.' + adapter.instance + '\u9999' }, function (err, res) {
res = res.rows;
for (let i = 0; i < res.length; i++) {
const id = res[i].doc.common.name;
adapter.log.debug('Remove ' + id + ': ' + id);
adapter.delObject(id, function (res, err) {
if (res !== undefined && res !== 'Not exists') adapter.log.error('res from delObject: ' + res);
if (err !== undefined) adapter.log.error('err from delObject: ' + err);
});
adapter.deleteState(id, function (res, err) {
if (res !== undefined && res !== 'Not exists') adapter.log.error('res from deleteState: ' + res);
if (err !== undefined) adapter.log.error('err from deleteState: ' + err);
});
}
});
}
adapter.subscribeStates('*');
adapter.getObjectList({ include_docs: true }, function (err, res) {
res = res.rows;
objects = {};
for (let i = 0; i < res.length; i++) {
objects[res[i].doc._id] = res[i].doc;
}
adapter.log.debug('received all objects');
main(adapter);
});
});
adapter.on('stateChange', (id, state) => {
adapter.log.debug('stateChange for ' + id + ' found state = ' + JSON.stringify(state));
});
adapter.on('unload', (callback) => {
try {
adapter.log.info('cleaned everything up...');
clearInterval(timer);
callback();
} catch (e) {
callback();
}
});
return adapter;
}
function main(adapter) {
// TODO: Check which Objects we provide
timer = setInterval(parser, adapter.config.interval || 60000);
parser();
}
function parser() {
adapter.log.debug('start parsing');
// Workaround, WebStorm
if (config === undefined) {
config = adapter.config;
}
for (let c in config) {
if (!config.hasOwnProperty(c)) continue;
adapter.log.debug('PARSING: ' + c);
if (c.indexOf('c_') !== 0 && config['c_' + c] === true) {
table[c] = new Array(20);
const o = config[c];
for (let i in o) {
if (!o.hasOwnProperty(i)) continue;
adapter.log.debug(' PARSING: ' + i);
const object = o[i];
const command = object.command;
let regexp;
if (object.multiline !== undefined) {
regexp = new RegExp(object.regexp, 'm');
} else {
regexp = new RegExp(object.regexp);
}
const post = object.post;
adapter.log.debug('---> ' + command);
let stdout;
try {
stdout = exec(command).toString();
adapter.log.debug('------------- ' + stdout);
} catch (er) {
adapter.log.debug(er.stack);
if (er.pid) console.log('%s (pid: %d) exited with status %d',
er.file, er.pid, er.status);
// do not process if exec fails
continue;
}
const match = regexp.exec(stdout);
adapter.log.debug('---> REGEXP: ' + regexp);
if (match !== undefined && match !== null && match.length !== undefined) {
adapter.log.debug('GROUPS: ' + match.length);
}
// TODO: if Group Match is bigger then 2
// split groups and header into seperate objects
if (match !== undefined && match !== null && match.length > 2) {
const lname = i.split(',');
for (let m = 1; m < match.length; m++) {
const value = match[m];
const name = lname[m - 1];
adapter.log.debug('MATCHING: ' + value);
adapter.log.debug('NAME: ' + name + ', VALULE: ' + value);
tinker[name] = value;
table[c][i] = value;
}
} else {
adapter.log.debug('---> POST: ' + post);
let value;
if (match !== undefined && match !== null) {
value = match[1];
} else {
value = stdout;
}
tinker[i] = value;
table[c][i] = value;
}
}
}
}
// TODO: Parse twice to get post data and evaluate
for (let c in config) {
if (!config.hasOwnProperty(c)) continue;
adapter.log.debug('CURRENT = ' + c + ' ' + config['c_' + c]);
adapter.log.debug(c.indexOf('c_'));
if (c.indexOf('c_') !== 0 && config['c_' + c]) {
if (objects[c] === undefined) {
const stateObj = {
common: {
name: c, // You can add here some description
read: true,
write: true,
role: 'sensor'
},
type: 'device',
_id: c
};
adapter.extendObject(c, stateObj);
}
const o = config[c];
for (let i in o) {
if (!o.hasOwnProperty(i)) continue;
const object = o[i];
const command = object.command;
const post = object.post;
adapter.log.debug('---> POST: ' + post + ' for ' + i + ' in ' + o);
let value;
const lname = i.split(',');
if (lname !== undefined && lname.length > 1) {
for (let m = 0; m < lname.length; m++) {
const name = lname[m];
value = tinker[name];
// TODO: Check if value is number and format it 2 Digits
if (!isNaN(value)) {
value = parseFloat(value);
const re = new RegExp(/^\d+\.\d+$/);
if (re.exec(value)) {
value = parseFloat(value.toFixed(2));
}
}
adapter.log.debug('MATCHING: ' + value);
adapter.log.debug('NAME: ' + name + ' VALULE: ' + value);
const objectName = adapter.name + '.' + adapter.instance + '.' + c + '.' + name;
adapter.log.debug('SETSTATE FOR ' + objectName + ' VALUE = ' + value);
if (objects[objectName] === undefined) {
// TODO Create an Objecttree
const stateObj = {
common: {
name: objectName, // You can add here some description
read: true,
write: true,
state: 'state',
role: 'value',
type: 'number'
},
type: 'state',
_id: objectName
};
adapter.extendObject(objectName, stateObj);
}
adapter.setState(objectName, {
val: value,
ack: true
});
}
} else {
value = tinker[i];
if (value !== undefined && value !== '' && value !== null) {
if (post.indexOf('$1') !== -1) {
adapter.log.debug('VALUE: ' + value + ' POST: ' + post);
try {
value = eval(post.replace('$1', value));
} catch (e) {
adapter.log.error('Cannot evaluate: ' + post.replace('$1', value));
value = NaN;
}
}
// TODO: Check if value is number and format it 2 Digits
if (!isNaN(value)) {
value = parseFloat(value);
const r = new RegExp(/^\d+\.\d+$/);
if (r.exec(value)) {
value = parseFloat(value.toFixed(2));
}
}
const objectName = adapter.name + '.' + adapter.instance + '.' + c + '.' + i;
adapter.log.debug('SETSTATE FOR ' + objectName + ' VALUE = ' + value);
if (objects[objectName] === undefined) {
// TODO Create an Objecttree
const stateObj = {
common: {
name: objectName, // You can add here some description
read: true,
write: true,
state: 'state',
role: 'value',
type: 'mixed'
},
type: 'state',
_id: objectName
};
adapter.extendObject(objectName, stateObj);
}
adapter.setState(objectName, {
val: value,
ack: true
});
} else {
if (i === 'wifi_send' || i === 'wifi_received') {
adapter.log.debug('No Value found for ' + i);
} else if (! errorsLogged[i]) {
adapter.log.error('No Value found for ' + i);
errorsLogged[i] = true;
}
}
}
}
}
}
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}