-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
176 lines (151 loc) · 5.19 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
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// Load your modules here, e.g.:
const axios = require('axios');
// variables
const isValidUrl = /https:\/\/meteo.netitservices.com\/api\/v[0,1]\/devices\/[a-zA-Z0-9]{12}\/poll\?hwtypeId=[0-9]{1,4}/;
class Hagelschutz extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'hagelschutz',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
this.log.info('starting adapter "hagelschutz"...');
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
// The adapters config (in the instance object everything under the attribute "native") is accessible via
this.log.debug(`config.url: ${this.config.url}`);
// check url
if (!isValidUrl.test(this.config.url)) {
this.log.error(
'"Url" is not valid (allowed format: https://meteo.netitservices.com/api/v0/devices/xxxxxxxxxxxx/poll?hwtypeId=yyy) (ERR_#001)',
);
return;
}
this.log.debug(`The configuration has been checked successfully. Trying to connect "${this.config.url}"...`);
try {
// create objects
await this.createObjects();
// get data
await this.getHailData();
await this.getHailDataByIntervall();
} catch (error) {
this.log.error(error);
}
}
// https://github.com/ioBroker/ioBroker.docs/blob/master/docs/en/dev/objectsschema.md
// https://github.com/ioBroker/ioBroker/blob/master/doc/STATE_ROLES.md#state-roles
async createObjects() {
this.log.debug('[createObjects]: start objects creation...');
await this.setObjectNotExistsAsync('hailState', {
type: 'state',
common: {
name: 'Hail State',
desc: 'Hail State',
type: 'number',
role: 'value.hail',
states: { 0: 'no hail', 1: 'hail', 2: 'hail state triggered by test-alarm' },
min: 0,
max: 2,
read: true,
write: false,
},
native: {},
});
await this.setObjectNotExistsAsync('hail', {
type: 'state',
common: {
name: 'Hail',
desc: 'Hail',
type: 'boolean',
role: 'value.hail',
read: true,
write: false,
},
native: {},
});
this.log.debug('[createObjects]: Objects created...');
}
async getHailData() {
await axios({
method: 'GET',
url: this.config.url,
})
.then((response) => {
this.log.debug(`[getHailData]: HTTP status response: ${response.status} ${response.statusText}; config: ${JSON.stringify(response.config)}; headers: ${JSON.stringify(response.headers,)}; data: ${JSON.stringify(response.data)}`);
this.setState('hailState', { val: response.data.currentState, ack: true });
if (response.data.currentState === 1 || response.data.currentState === 2) {
this.setState('hail', { val: true, ack: true });
} else {
this.setState('hail', { val: false, ack: true });
}
this.setState('info.connection', true, true);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code that falls out of the range of 2xx
this.log.debug(`[getHailData]: HTTP status response: ${error.response.status}; headers: ${JSON.stringify(error.response.headers)}; data: ${JSON.stringify(error.response.data)}`);
} else if (error.request) {
// The request was made but no response was received `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
this.log.debug(`[getHailData]: error request: ${error}`);
} else {
// Something happened in setting up the request that triggered an Error
this.log.debug(`[getHailData]: error message: ${error.message}`);
}
this.log.debug(`[getHailData]: error.config: ${JSON.stringify(error.config)}`);
this.setState('info.connection', false, true);
});
}
async getHailDataByIntervall() {
this.log.info('[getHailDataByIntervall]: Starting polltimer with a 120 seconds interval.');
try {
this.intervall = setInterval(async () => {
await this.getHailData();
}, 120 * 1000); // default-intervall: 120s
} catch (error) {
this.log.error(`${error}: (ERR_#002)`);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
this.intervall && clearInterval(this.intervall);
this.setState('info.connection', false, true);
callback();
this.log.info('cleaned everything up... (#1)');
} catch {
callback();
this.log.info('cleaned everything up... (#2)');
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Hagelschutz(options);
} else {
// otherwise start the instance directly
new Hagelschutz();
}