-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (124 loc) · 4.31 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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core');
const axios = require('axios');
const adapterName = require('./package.json').name.split('.').pop();
class Luftdaten extends utils.Adapter {
constructor(options) {
super({
...options,
name: adapterName,
});
this.killTimeout = null;
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
const unitList = {
Hum: '%',
Temp: '°C',
wifiquality: '%',
wifissid: '',
IP: '',
latency: 'ms',
hPa: 'hPa',
wifirssi: 'dB',
version: 'µs',
LUX: 'lx'
};
const roleList = {
Hum: 'value.procent',
Temp: 'value.temperature',
wifiquality: 'value.procent',
wifissid: 'value',
IP: 'value',
latency: 'value',
hPa: 'value.pressure',
wifirssi: 'value.decibel',
version: 'value',
LUX: 'value.luminousFlux'
};
const hostAddress = this.config.hostAddress;
const hostPort = this.config.hostPort;
this.log.info('hostAddress= ' + hostAddress);
this.log.info('hostPort= ' + hostPort);
await this.setObjectNotExistsAsync('getData', {
native: {}
});
await this.setObjectNotExistsAsync('matrixInfo', {
native: {}
});
if(hostAddress && hostPort){
this.log.info('starting request...');
axios({
method: 'post',
baseURL: 'http://' + hostAddress + ':' + hostPort,
url: '/api/v3/basics',
responseType: 'json',
data: {
get: 'matrixInfo'
}
}).then(
async (response) => {
const content = response.data;
this.log.info('resposne: ' + JSON.stringify(content));
for (var key in content){
var value = content[key];
//document.write("<br> - " + key + ": " + value);
this.log.info(key + ' - ' + roleList.key + ' - ' + unitList.key);
await this.setObjectNotExistsAsync('matrixInfo.' + key, {
type: 'state',
common: {
name: key,
type: 'number',
role: roleList[key],
unit: unitList[key],
read: true,
write: false
},
native: {}
});
this.setState('matrixInfo.' + key, {val: value, ack: true});
}
}
).catch(
(error) => {
if (error.response) {
this.log.warn('received error ' + error.response.status + ' response from local sensor ' + sensorIdentifier + ' with content: ' + JSON.stringify(error.response.data));
} else if (error.request) {
this.log.error(error.message);
} else {
this.log.error(error.message);
}
}
);
} else {
this.killTimeout = setTimeout(this.stop.bind(this), 10000);
}
}
onUnload(callback) {
try {
if (this.killTimeout) {
this.log.debug('clearing kill timeout');
clearTimeout(this.killTimeout);
}
this.log.debug('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Luftdaten(options);
} else {
// otherwise start the instance directly
new Luftdaten();
}