-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
290 lines (236 loc) · 9.82 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
/* jshint node: true */
// @ts-nocheck
'use strict';
/*
* Created with @iobroker/create-adapter v1.24.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
//const util = require('util');
const Parser = require('expr-eval').Parser;
// Load your modules here, e.g.:
const Listener = require('./lib/listener');
const Scheduler = require('./lib/scheduler');
const { PROT_WU, PROT_EW, DATAFIELDS } = require('./lib/constants');
//const getMethods = (obj) => Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function');
class Sainlogic extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
// eslint-disable-next-line no-unused-vars
constructor(options) {
super({
name: 'sainlogic',
});
this.on('ready', this.onReady.bind(this));
// this.on('objectChange', this.onObjectChange.bind(this));
//this.on('stateChange', this.onStateChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
checkUnit(attrdef, obj) {
const c_id = obj._id;
const target_unit = this.config[attrdef.unit_config];
if (target_unit != obj.common.unit) {
// change and convert unit
this.log.info(`Unit changed for ${c_id} from ${obj.common.unit} to ${target_unit}, updating data point`);
this.extendObject(c_id, {
type: obj.type,
common: {
name: attrdef.display_name,
type: attrdef.type,
role: attrdef.role,
unit: target_unit,
},
native: {},
});
const my_source_unit = attrdef.units.filter(function (unit) {
return unit.display_name == obj.common.unit;
});
const conversion_rule_back = my_source_unit[0].main_unit_conversion;
const that = this;
this.getState(c_id, function (err, st) {
const parser = new Parser();
let new_value = st.val;
// convert back if required
if (conversion_rule_back != null) {
const exp = parser.parse(conversion_rule_back);
new_value = exp.evaluate({ x: new_value });
}
new_value = that.toDisplayUnit(attrdef, new_value);
that.setState(c_id, { val: new_value, ack: true });
}.bind(that));
}
}
/** Converts the given value to the target unit for given attribute definition */
toDisplayUnit(attrdef, value) {
const parser = new Parser();
const target_unit = this.config[attrdef.unit_config];
const my_target_unit = attrdef.units.filter(function (unit) {
return unit.display_name == target_unit;
});
const conversion_rule_forward = my_target_unit[0].display_conversion;
this.log.debug('Target for ' + attrdef.id + ' unit is set: ' + target_unit + ', using conversion: ' + conversion_rule_forward);
let new_value = value;
// convert forward if required
if (conversion_rule_forward != null) {
const exp = parser.parse(conversion_rule_forward);
new_value = exp.evaluate({ x: value });
}
return new_value;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
if (this.config.scheduler_active == true) {
this.log.info('Starting Scheduler');
this.scheduler = new Scheduler(this.config, this);
this.scheduler.start();
}
if (this.config.listener_active == true) {
this.listener = new Listener(this.config.bind, this.config.port, this.config.path, this.config.listener_protocol, this.config.listener_forward_url, this);
this.listener.start();
}
}
verify_datapoint(obj_id, that, attrdef, attrname, value) {
// check target type and type-cast if needed
let default_value = '';
if (attrdef.type == 'number'){
if (value != null) {
value = parseFloat(value);
} else {
value = 0;
default_value = 0;
}
}
this.getObject(obj_id, function (err, obj) {
if (err || obj == null) {
that.log.info('Creating new data point: ' + obj_id);
that.setObjectNotExists(obj_id, {
type: 'state',
common: {
name: attrname,
type: attrdef.type,
unit: attrdef.unit,
role: attrdef.role,
min: attrdef.min,
max: attrdef.max,
def: default_value,
read: true,
write: false,
mobile: {
admin: {
visible: true
}
},
},
native: {},
// eslint-disable-next-line no-unused-vars
}, function (err, obj) {
// now update the value
that.setStateAsync(obj_id, { val: value, ack: true });
});
}
else {
if (attrdef.unit_config != null) {
that.checkUnit(attrdef, obj);
}
// now update the value
that.setStateAsync(obj_id, { val: value, ack: true });
}
}.bind(that));
}
/**
* @param {Date} date
* @param {{ softwaretype: any; indoortempf: any; tempf: any; dewptf: any; windchillf: any; indoorhumidity: any; humidity: any; windspeedmph: any; windgustmph: any; winddir: any; baromin: any; absbaromin: any; ... 6 more ...; UV: any; }} json_response
*/
setStates(date, obj_values) {
this.setStateAsync('info.last_update', { val: date.toString(), ack: true });
for (const attr in obj_values) {
// extract attribute id w/o channel
const c_id = attr.split('.').pop();
// check if this has a mapping to the current protocol
this.log.debug(`Setting value from data for ${attr} to ${obj_values[attr]}`);
const my_attr_def = DATAFIELDS.filter(function (def) {
return def.id == c_id;
});
let display_val = obj_values[attr];
if (my_attr_def[0].unit_config) {
display_val = this.toDisplayUnit(my_attr_def[0], display_val);
}
this.verify_datapoint(attr, this, my_attr_def[0], my_attr_def[0].channels[0].name, display_val ); // allways channel 0 as primary attribute name
if (c_id == 'winddir') {
const winddir_attrdef = DATAFIELDS.filter(function (def) {
return def.id == 'windheading';
});
this.verify_datapoint('weather.current.windheading', this, winddir_attrdef[0], winddir_attrdef[0].channels[0].name, this.getHeading(display_val, 16) );
}
}
}
// taken from https://www.programmieraufgaben.ch/aufgabe/windrichtung-bestimmen/ibbn2e7d
getHeading(degrees, precision) {
this.log.debug('Determining wind heading for ' + degrees + ' and precision ' + precision);
precision = precision || 16;
let directions = [],
direction = 0;
const step = 360 / precision;
let i = step / 2;
switch (precision) {
case 4: directions = 'N O S W'.split(' '); break;
case 8: directions = 'N NO O SO S SW W NW'.split(' '); break;
case 16: directions = ('N NNO NO ONO O OSO SO ' +
'SSO S SSW SW WSW W WNW NW NNW').split(' ');
break;
case 32: directions = ('N NzO NNO NOzN NO NOzO ONO OzN O OzS OSO ' +
'SOzO SO SOzS SSO SzO S SzW SSW SWzS SW SWzW WSW WzS W WzN ' +
'WNW NWzW NW NWzN NNW NzW').split(' ');
break;
default:
this.log.error('Wind heading could not be determined: invalid precision');
return '';
}
if (degrees < 0 || degrees > 360) {
this.log.error('Wind heading could not be determined: wind direction outside boundary');
return '';
}
if (degrees <= i || degrees >= 360 - i) return 'N';
while (i <= degrees) {
direction++;
i += step;
}
this.log.debug('GetHeading returning: ' + directions[direction]);
return directions[direction];
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (this.listener)
this.listener.stop();
if (this.scheduler)
this.scheduler.stop();
this.log.info('Sainlogic Adapter gracefully shut down...');
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 Sainlogic(options);
} else {
// otherwise start the instance directly
new Sainlogic();
}