-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathweather-server-handler.js
120 lines (120 loc) · 7.37 KB
/
weather-server-handler.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WeatherServerHandler = void 0;
const sprintf_js_1 = require("sprintf-js");
const loxone_handler_base_1 = require("./loxone-handler-base");
class WeatherServerHandler extends loxone_handler_base_1.LoxoneHandlerBase {
constructor(adapter) {
super(adapter);
this.adapter = adapter;
this.deviceName = 'WeatherServer';
this.weatherTypeTexts = {};
this.forecastChannelsCount = 0;
}
async loadAsync(data, filter) {
if (data === undefined || !data.hasOwnProperty('states') || !data.states.hasOwnProperty('actual')) {
return;
}
this.format = data.format;
this.weatherTypeTexts = data.weatherTypeTexts;
const deviceName = 'WeatherServer';
await this.updateObjectAsync(deviceName, {
type: 'device',
common: {
name: deviceName,
role: 'weather',
},
native: { data: data },
});
await this.setWeatherObjectsAsync('Actual');
this.addStateEventHandler(data.states.actual, async (value) => {
await this.setWeatherStates(deviceName + '.Actual', value.entries[0]);
});
if (filter === 'current') {
return;
}
this.addStateEventHandler(data.states.forecast, async (value) => {
const hourCount = Math.min(value.entries.length, filter === '1day' ? 24 : Number.MAX_VALUE);
for (let i = 0; i < hourCount; i++) {
const channelName = 'Hour' + (0, sprintf_js_1.sprintf)('%02d', i + 1);
if (i >= this.forecastChannelsCount) {
await this.setWeatherObjectsAsync(channelName);
this.forecastChannelsCount++;
}
await this.setWeatherStates(deviceName + '.' + channelName, value.entries[i]);
}
});
}
async setWeatherStates(parent, values) {
if (values === undefined) {
return;
}
await this.setStateAck(parent + '.barometricPressure', values.barometricPressure);
await this.setFormattedStateAck(parent + '.barometricPressure-formatted', values.barometricPressure, this.format.barometricPressure);
await this.setStateAck(parent + '.dewPoint', values.dewPoint);
await this.setFormattedStateAck(parent + '.dewPoint-formatted', values.dewPoint, this.format.temperature);
await this.setStateAck(parent + '.perceivedTemperature', values.perceivedTemperature);
await this.setFormattedStateAck(parent + '.perceivedTemperature-formatted', values.perceivedTemperature, this.format.temperature);
await this.setStateAck(parent + '.precipitation', values.precipitation);
await this.setFormattedStateAck(parent + '.precipitation-formatted', values.precipitation, this.format.precipitation);
await this.setStateAck(parent + '.relativeHumidity', values.relativeHumidity);
await this.setFormattedStateAck(parent + '.relativeHumidity-formatted', values.relativeHumidity, this.format.relativeHumidity);
await this.setStateAck(parent + '.solarRadiation', values.solarRadiation);
await this.setStateAck(parent + '.temperature', values.temperature);
await this.setFormattedStateAck(parent + '.temperature-formatted', values.temperature, this.format.temperature);
await this.setTimeStateAck(parent + '.timestamp', values.timestamp);
await this.setStateAck(parent + '.weatherType', values.weatherType);
await this.setStateAck(parent + '.weatherType-text', this.weatherTypeTexts[values.weatherType]);
await this.setStateAck(parent + '.windDirection', values.windDirection);
await this.setStateAck(parent + '.windSpeed', values.windSpeed);
await this.setFormattedStateAck(parent + '.windSpeed-formatted', values.windSpeed, this.format.windSpeed);
}
async setTimeStateAck(id, miniserverTime) {
const value = miniserverTime * 1000 + new Date(2009, 0, 1).getTime();
await this.setStateAck(id, value);
}
async setWeatherObjectsAsync(channelName) {
await this.updateObjectAsync(this.deviceName + '.' + channelName, {
type: 'channel',
common: {
name: channelName,
role: 'weather.current',
},
native: {},
});
await this.setWeatherObjectAsync(channelName, 'barometricPressure', channelName + ': Barometric pressure', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'barometricPressure-formatted', channelName + ': Barometric pressure: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'dewPoint', channelName + ': Dew point', 'number', 'value.temperature');
await this.setWeatherObjectAsync(channelName, 'dewPoint-formatted', channelName + ': Dew point: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'perceivedTemperature', channelName + ': Perceived temperature', 'number', 'value.temperature');
await this.setWeatherObjectAsync(channelName, 'perceivedTemperature-formatted', channelName + ': Perceived temperature: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'precipitation', channelName + ': Precipitation', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'precipitation-formatted', channelName + ': Precipitation: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'relativeHumidity', channelName + ': Relative humidity', 'number', 'value.humidity');
await this.setWeatherObjectAsync(channelName, 'relativeHumidity-formatted', channelName + ': Relative humidity: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'solarRadiation', channelName + ': Solar radiation', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'temperature', channelName + ': Temperature', 'number', 'value.temperature');
await this.setWeatherObjectAsync(channelName, 'temperature-formatted', channelName + ': Temperature: formatted', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'timestamp', channelName + ': Timestamp', 'number', 'value.time');
await this.setWeatherObjectAsync(channelName, 'weatherType', channelName + ': Weather type', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'weatherType-text', channelName + ': Weather type: text', 'string', 'text');
await this.setWeatherObjectAsync(channelName, 'windDirection', channelName + ': Wind direction', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'windSpeed', channelName + ': Wind speed', 'number', 'value');
await this.setWeatherObjectAsync(channelName, 'windSpeed-formatted', channelName + ': Wind speed: formatted', 'string', 'text');
}
async setWeatherObjectAsync(channelName, id, name, type, role) {
await this.updateObjectAsync(this.deviceName + '.' + channelName + '.' + id, {
type: 'state',
common: {
name: name,
read: true,
write: false,
type: type,
role: role,
},
native: {},
});
}
}
exports.WeatherServerHandler = WeatherServerHandler;
//# sourceMappingURL=weather-server-handler.js.map