forked from TKnpl/ioBroker.divera247
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
300 lines (275 loc) · 8.66 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
299
300
'use strict';
const utils = require('@iobroker/adapter-core');
const axios = require('axios');
const adapterName = require('./package.json').name.split('.').pop();
let lastAlarmId = null;
let lastAlarmStatus = false;
class Divera247 extends utils.Adapter {
constructor(options) {
super({
...options,
name: adapterName,
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
}
async onReady() {
const diveraAccessKey = this.config.diveraAccessKey;
const pollIntervallSeconds = this.config.pollIntervall;
const pollIntervallMilliseconds = pollIntervallSeconds * 1000;
const pollIntervallSecondsMinimum = 10;
this.setState('info.connection', false, true);
if (diveraAccessKey && pollIntervallSeconds) {
if (pollIntervallSeconds >= pollIntervallSecondsMinimum) {
if (await this.checkConnectionToApi(diveraAccessKey)) {
// Connected to API
this.setState('info.connection', true, true);
// Creating the Object 'alarm' -> response JSON key 'success'
this.setObjectNotExistsAsync('alarm', {
type: 'state',
common: {
name: 'Alarm',
type: 'boolean',
role: 'indicator',
read: true,
write: false
},
native: {},
});
// Creating the Object 'title' -> response JSON key 'data.title'
this.setObjectNotExistsAsync('title', {
type: 'state',
common: {
name: 'Einsatzstichwort',
type: 'string',
role: 'text',
read: true,
write: false
},
native: {},
});
// Creating the Object 'title' -> response JSON key 'data.title'
this.setObjectNotExistsAsync('text', {
type: 'state',
common: {
name: 'Meldungstext',
type: 'string',
role: 'text',
read: true,
write: false
},
native: {},
});
// Creating the Object 'address' -> response JSON key 'data.address'
this.setObjectNotExistsAsync('address', {
type: 'state',
common: {
name: 'Adresse',
type: 'string',
role: 'text',
read: true,
write: false
},
native: {},
});
// Creating the Object 'lat' -> response JSON key 'data.lat'
this.setObjectNotExistsAsync('lat', {
type: 'state',
common: {
name: 'Längengrad',
type: 'number',
role: 'text',
read: true,
write: false
},
native: {},
});
// Creating the Object 'lng' -> response JSON key 'data.lng'
this.setObjectNotExistsAsync('lng', {
type: 'state',
common: {
name: 'Breitengrad',
type: 'number',
role: 'text',
read: true,
write: false
},
native: {},
});
// Creating the Object 'date' -> response JSON key 'data.date'
this.setObjectNotExistsAsync('date', {
type: 'state',
common: {
name: 'Alarmierungszeit',
type: 'number',
role: 'date',
read: true,
write: false
},
native: {},
});
// Creating the Object 'priority' -> response JSON key 'data.priority'
this.setObjectNotExistsAsync('priority', {
type: 'state',
common: {
name: 'Priorität/Sonderrechte',
type: 'boolean',
role: 'indicator',
read: true,
write: false
},
native: {},
});
// Creating the Object 'lastUpdate' -> current timestamp
this.setObjectNotExistsAsync('lastUpdate', {
type: 'state',
common: {
name: 'Letzte Aktualisierung',
type: 'number',
role: 'value.time',
read: true,
write: false
},
native: {},
});
// Initialisation of the states
this.setState('alarm', { val: false, ack: true });
this.setState('title', { val: null, ack: true });
this.setState('text', { val: null, ack: true });
this.setState('address', { val: null, ack: true });
this.setState('lat', { val: null, ack: true });
this.setState('lng', { val: null, ack: true });
this.setState('date', { val: null, ack: true });
this.setState('priority', { val: null, ack: true });
this.setState('lastUpdate', { val: null, ack: true });
// Registration of an interval calling the main function for this adapter
let repeatingFunctionCall = setInterval(() => {
this.getDataFromApiAndSetObjects(diveraAccessKey);
}, pollIntervallMilliseconds);
}
} else {
this.log.error('The update interval must be at least ' + pollIntervallSecondsMinimum + ' seconds!');
}
}
}
/*
* Function to check the connection to the API
* returns true / false
*/
checkConnectionToApi(diveraAccessKey) {
// Calling the alerting-server api
return axios({
method: 'get',
baseURL: 'https://www.divera247.com/',
url: '/api/last-alarm?accesskey=' + diveraAccessKey,
responseType: 'json'
}).then(
function (response) {
if (response.status == 200) {
this.log.debug('Connection to API succeeded');
return true;
} else {
this.log.warn('Connection to API failed. Please check your API-Key and try again');
return false;
}
}.bind(this)
).catch(
function (error) {
if (error.response) {
// The request was made and the server responded with a error status code
if (error.response.status == 403) {
this.log.error('Access-Token invalid. Please use a valid token!');
return false;
} else {
this.log.warn('received error ' + error.response.status + ' response with content: ' + JSON.stringify(error.response.data));
return false;
}
} else if (error.request) {
// The request was made but no response was received
this.log.error(error.message);
return false;
} else {
// Something happened in setting up the request that triggered an Error
this.log.error(error.message);
return false;
}
}.bind(this)
)
}
/*
* Function that calls the API and set the Object States
*/
getDataFromApiAndSetObjects(diveraAccessKey) {
// Calling the alerting-server api
axios({
method: 'get',
baseURL: 'https://www.divera247.com/',
url: '/api/last-alarm?accesskey=' + diveraAccessKey,
responseType: 'json'
}).then(
function (response) {
const content = response.data;
this.log.debug('Received data from Divera-API (' + response.status + '): ' + JSON.stringify(content));
// Setting the update state
this.setState('lastUpdate', { val: Date.now(), ack: true });
// Setting the alarm specific states when a new alarm is active
if (content.success && lastAlarmId != content.data.id) {
lastAlarmId = content.data.id;
lastAlarmStatus = content.success;
this.setState('alarm', { val: content.success, ack: true });
this.setState('title', { val: content.data.title, ack: true });
this.setState('text', { val: content.data.text, ack: true });
this.setState('address', { val: content.data.address, ack: true });
this.setState('lat', { val: content.data.lat, ack: true });
this.setState('lng', { val: content.data.lng, ack: true });
this.setState('date', { val: content.data.date * 1000, ack: true });
this.setState('priority', { val: content.data.priority, ack: true });
} else if (content.success != lastAlarmStatus) {
lastAlarmStatus = content.success;
this.setState('alarm', { val: content.success, ack: true });
}
}.bind(this)
).catch(
function (error) {
if (error.response) {
// The request was made and the server responded with a error status code
if (error.response.status == 403) {
this.log.error('Access-Token has been invalid. Please use a valid token!');
this.setState('info.connection', false, true);
} else {
this.log.warn('received error ' + error.response.status + ' response with content: ' + JSON.stringify(error.response.data));
this.setState('info.connection', false, true);
}
} else if (error.request) {
// The request was made but no response was received
this.log.error(error.message);
this.setState('info.connection', false, true);
} else {
// Something happened in setting up the request that triggered an Error
this.log.error(error.message);
this.setState('info.connection', false, true);
}
}.bind(this)
)
}
// Is called when adapter shuts down
onUnload(callback) {
try {
clearInterval(repeatingFunctionCall);
callback();
} catch (e) {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Divera247(options);
} else {
// otherwise start the instance directly
new Divera247();
}