forked from klein0r/ioBroker.luftdaten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
104 lines (88 loc) · 3.44 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
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var request = require('request');
var adapter = new utils.Adapter('luftdaten');
adapter.on('ready', function () {
main();
});
function main() {
var sensorType = adapter.config.sensorType;
var sensorIdentifier = adapter.config.sensorIdentifier;
let sensorName = (adapter.config.sensorName === "") ? sensorIdentifier : adapter.config.sensorName;
let path = (sensorType == "local") ? sensorIdentifier.replace(/\./g,"_") + "." : sensorIdentifier + ".";
adapter.log.debug('sensor type: ' + sensorType);
adapter.log.debug('sensor identifier: ' + sensorIdentifier);
adapter.log.debug('sensor name: ' + sensorName);
adapter.setObjectNotExists(path + 'Name', {
type: 'state',
common: {
name: 'Name',
type: 'string',
role: 'text'
},
native: {}
});
adapter.setState(path + 'Name', {val: sensorName, ack: true});
if (sensorType == "local") {
adapter.log.debug('local request');
request(
{
url: "http://" + sensorIdentifier + "/data.json",
json: true
},
function (error, response, content) {
adapter.log.debug('Request done');
if (!error && response.statusCode == 200) {
for (var key in content.sensordatavalues) {
var obj = content.sensordatavalues[key];
adapter.setObjectNotExists(path + obj.value_type, {
type: 'state',
common: {
name: obj.value_type,
type: 'number',
role: 'value'
},
native: {}
});
adapter.setState(path + obj.value_type, {val: obj.value, ack: true});
}
} else {
adapter.log.error(error);
}
}
);
} else if (sensorType == "remote") {
adapter.log.debug('remote request');
request(
{
url: "http://api.luftdaten.info/v1/sensor/" + sensorIdentifier + "/",
json: true
},
function (error, response, content) {
adapter.log.debug('Request done');
if (!error && response.statusCode == 200) {
for (var key in content[0].sensordatavalues) {
var obj = content[0].sensordatavalues[key];
adapter.setObjectNotExists(path + 'SDS_' + obj.value_type, {
type: 'state',
common: {
name: 'SDS_' + obj.value_type,
type: 'number',
role: 'value'
},
native: {}
});
adapter.setState(path + 'SDS_' + obj.value_type, {val: obj.value, ack: true});
}
} else {
adapter.log.error(error);
}
}
);
}
setTimeout(function () {
adapter.stop();
}, 10000);
}