]> git.basschouten.com Git - openhab-addons.git/blob
a1b2dc2b1e5b1fd5db684279844cbf2a4395d287
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.fineoffsetweatherstation.internal.domain;
14
15 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_MAX_WIND_SPEED;
16 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_MOISTURE;
17 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_UV_INDEX;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
26 import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
27 import org.openhab.core.thing.type.ChannelTypeUID;
28 import org.openhab.core.types.State;
29
30 /**
31  * The measurands of supported by the gateway.
32  *
33  * @author Andreas Berger - Initial contribution
34  */
35 @NonNullByDefault
36 public enum Measurand {
37
38     INTEMP("temperature-indoor", (byte) 0x01, "Indoor Temperature", MeasureType.TEMPERATURE,
39             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_INDOOR_TEMPERATURE),
40
41     OUTTEMP("temperature-outdoor", (byte) 0x02, "Outdoor Temperature", MeasureType.TEMPERATURE,
42             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_OUTDOOR_TEMPERATURE),
43
44     DEWPOINT("temperature-dew-point", (byte) 0x03, "Dew point", MeasureType.TEMPERATURE),
45
46     WINDCHILL("temperature-wind-chill", (byte) 0x04, "Wind chill", MeasureType.TEMPERATURE),
47
48     HEATINDEX("temperature-heat-index", (byte) 0x05, "Heat index", MeasureType.TEMPERATURE),
49
50     INHUMI("humidity-indoor", (byte) 0x06, "Indoor Humidity", MeasureType.PERCENTAGE),
51
52     OUTHUMI("humidity-outdoor", (byte) 0x07, "Outdoor Humidity", MeasureType.PERCENTAGE,
53             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_ATMOSPHERIC_HUMIDITY),
54
55     ABSBARO("pressure-absolute", (byte) 0x08, "Absolutely pressure", MeasureType.PRESSURE),
56
57     RELBARO("pressure-relative", (byte) 0x09, "Relative pressure", MeasureType.PRESSURE,
58             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_BAROMETRIC_PRESSURE),
59
60     WINDDIRECTION("direction-wind", (byte) 0x0A, "Wind Direction", MeasureType.DEGREE,
61             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_WIND_DIRECTION),
62
63     WINDSPEED("speed-wind", (byte) 0x0B, "Wind Speed", MeasureType.SPEED,
64             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_WIND_SPEED),
65
66     GUSTSPEED("speed-gust", (byte) 0x0C, "Gust Speed", MeasureType.SPEED,
67             DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_WIND_SPEED),
68
69     RAINEVENT("rain-event", (byte) 0x0D, "Rain Event", MeasureType.HEIGHT),
70
71     RAINRATE("rain-rate", (byte) 0x0E, "Rain Rate", MeasureType.HEIGHT_PER_HOUR),
72
73     RAINHOUR("rain-hour", (byte) 0x0F, "Rain hour", MeasureType.HEIGHT),
74
75     RAINDAY("rain-day", (byte) 0x10, "Rain Day", MeasureType.HEIGHT),
76
77     RAINWEEK("rain-week", (byte) 0x11, "Rain Week", MeasureType.HEIGHT),
78
79     RAINMONTH("rain-month", (byte) 0x12, "Rain Month", MeasureType.HEIGHT_BIG),
80
81     RAINYEAR("rain-year", (byte) 0x13, "Rain Year", MeasureType.HEIGHT_BIG),
82
83     RAINTOTALS("rain-total", (byte) 0x14, "Rain Totals", MeasureType.HEIGHT_BIG),
84
85     LIGHT("illumination", (byte) 0x15, "Light", MeasureType.LUX),
86
87     UV("irradiation-uv", (byte) 0x16, "UV", MeasureType.MICROWATT_PER_SQUARE_CENTIMETRE),
88
89     UVI("uv-index", (byte) 0x17, "UV index", MeasureType.BYTE, CHANNEL_TYPE_UV_INDEX),
90
91     TIME("time", (byte) 0x18, "Date and time", MeasureType.DATE_TIME2),
92
93     DAYLWINDMAX("wind-max-day", (byte) 0X19, "Day max wind", MeasureType.SPEED, CHANNEL_TYPE_MAX_WIND_SPEED),
94
95     TEMP1("temperature-channel-1", (byte) 0x1A, "Temperature 1", MeasureType.TEMPERATURE),
96
97     TEMP2("temperature-channel-2", (byte) 0x1B, "Temperature 2", MeasureType.TEMPERATURE),
98
99     TEMP3("temperature-channel-3", (byte) 0x1C, "Temperature 3", MeasureType.TEMPERATURE),
100
101     TEMP4("temperature-channel-4", (byte) 0x1D, "Temperature 4", MeasureType.TEMPERATURE),
102
103     TEMP5("temperature-channel-5", (byte) 0x1E, "Temperature 5", MeasureType.TEMPERATURE),
104
105     TEMP6("temperature-channel-6", (byte) 0x1F, "Temperature 6", MeasureType.TEMPERATURE),
106
107     TEMP7("temperature-channel-7", (byte) 0x20, "Temperature 7", MeasureType.TEMPERATURE),
108
109     TEMP8("temperature-channel-8", (byte) 0x21, "Temperature 8", MeasureType.TEMPERATURE),
110
111     HUMI1("humidity-channel-1", (byte) 0x22, "Humidity 1", MeasureType.PERCENTAGE),
112
113     HUMI2("humidity-channel-2", (byte) 0x23, "Humidity 2", MeasureType.PERCENTAGE),
114
115     HUMI3("humidity-channel-3", (byte) 0x24, "Humidity 3", MeasureType.PERCENTAGE),
116
117     HUMI4("humidity-channel-4", (byte) 0x25, "Humidity 4", MeasureType.PERCENTAGE),
118
119     HUMI5("humidity-channel-5", (byte) 0x26, "Humidity 5", MeasureType.PERCENTAGE),
120
121     HUMI6("humidity-channel-6", (byte) 0x27, "Humidity 6", MeasureType.PERCENTAGE),
122
123     HUMI7("humidity-channel-7", (byte) 0x28, "Humidity 7", MeasureType.PERCENTAGE),
124
125     HUMI8("humidity-channel-8", (byte) 0x29, "Humidity 8", MeasureType.PERCENTAGE),
126
127     SOILTEMP1("temperature-soil-channel-1", (byte) 0x2B, "Soil Temperature 1", MeasureType.TEMPERATURE),
128
129     SOILTEMP2("temperature-soil-channel-2", (byte) 0x2D, "Soil Temperature 2", MeasureType.TEMPERATURE),
130
131     SOILTEMP3("temperature-soil-channel-3", (byte) 0x2F, "Soil Temperature 3", MeasureType.TEMPERATURE),
132
133     SOILTEMP4("temperature-soil-channel-4", (byte) 0x31, "Soil Temperature 4", MeasureType.TEMPERATURE),
134
135     SOILTEMP5("temperature-soil-channel-5", (byte) 0x33, "Soil Temperature 5", MeasureType.TEMPERATURE),
136
137     SOILTEMP6("temperature-soil-channel-6", (byte) 0x35, "Soil Temperature 6", MeasureType.TEMPERATURE),
138
139     SOILTEMP7("temperature-soil-channel-7", (byte) 0x37, "Soil Temperature 7", MeasureType.TEMPERATURE),
140
141     SOILTEMP8("temperature-soil-channel-8", (byte) 0x39, "Soil Temperature 8", MeasureType.TEMPERATURE),
142
143     SOILTEMP9("temperature-soil-channel-9", (byte) 0x3B, "Soil Temperature 9", MeasureType.TEMPERATURE),
144
145     SOILTEMP10("temperature-soil-channel-10", (byte) 0x3D, "Soil Temperature 10", MeasureType.TEMPERATURE),
146
147     SOILTEMP11("temperature-soil-channel-11", (byte) 0x3F, "Soil Temperature 11", MeasureType.TEMPERATURE),
148
149     SOILTEMP12("temperature-soil-channel-12", (byte) 0x41, "Soil Temperature 12", MeasureType.TEMPERATURE),
150
151     SOILTEMP13("temperature-soil-channel-13", (byte) 0x43, "Soil Temperature 13", MeasureType.TEMPERATURE),
152
153     SOILTEMP14("temperature-soil-channel-14", (byte) 0x45, "Soil Temperature 14", MeasureType.TEMPERATURE),
154
155     SOILTEMP15("temperature-soil-channel-15", (byte) 0x47, "Soil Temperature 15", MeasureType.TEMPERATURE),
156
157     SOILTEMP16("temperature-soil-channel-16", (byte) 0x49, "Soil Temperature 16", MeasureType.TEMPERATURE),
158
159     SOILMOISTURE1("moisture-soil-channel-1", (byte) 0x2C, "Soil Moisture 1", MeasureType.PERCENTAGE,
160             CHANNEL_TYPE_MOISTURE),
161
162     SOILMOISTURE2("moisture-soil-channel-2", (byte) 0x2E, "Soil Moisture 2", MeasureType.PERCENTAGE,
163             CHANNEL_TYPE_MOISTURE),
164
165     SOILMOISTURE3("moisture-soil-channel-3", (byte) 0x30, "Soil Moisture 3", MeasureType.PERCENTAGE,
166             CHANNEL_TYPE_MOISTURE),
167
168     SOILMOISTURE4("moisture-soil-channel-4", (byte) 0x32, "Soil Moisture 4", MeasureType.PERCENTAGE,
169             CHANNEL_TYPE_MOISTURE),
170
171     SOILMOISTURE5("moisture-soil-channel-5", (byte) 0x34, "Soil Moisture 5", MeasureType.PERCENTAGE,
172             CHANNEL_TYPE_MOISTURE),
173
174     SOILMOISTURE6("moisture-soil-channel-6", (byte) 0x36, "Soil Moisture 6", MeasureType.PERCENTAGE,
175             CHANNEL_TYPE_MOISTURE),
176
177     SOILMOISTURE7("moisture-soil-channel-7", (byte) 0x38, "Soil Moisture 7", MeasureType.PERCENTAGE,
178             CHANNEL_TYPE_MOISTURE),
179
180     SOILMOISTURE8("moisture-soil-channel-8", (byte) 0x3A, "Soil Moisture 8", MeasureType.PERCENTAGE,
181             CHANNEL_TYPE_MOISTURE),
182
183     SOILMOISTURE9("moisture-soil-channel-9", (byte) 0x3C, "Soil Moisture 9", MeasureType.PERCENTAGE,
184             CHANNEL_TYPE_MOISTURE),
185
186     SOILMOISTURE10("moisture-soil-channel-10", (byte) 0x3E, "Soil Moisture 10", MeasureType.PERCENTAGE,
187             CHANNEL_TYPE_MOISTURE),
188
189     SOILMOISTURE11("moisture-soil-channel-11", (byte) 0x40, "Soil Moisture 11", MeasureType.PERCENTAGE,
190             CHANNEL_TYPE_MOISTURE),
191
192     SOILMOISTURE12("moisture-soil-channel-12", (byte) 0x42, "Soil Moisture 12", MeasureType.PERCENTAGE,
193             CHANNEL_TYPE_MOISTURE),
194
195     SOILMOISTURE13("moisture-soil-channel-13", (byte) 0x44, "Soil Moisture 13", MeasureType.PERCENTAGE,
196             CHANNEL_TYPE_MOISTURE),
197
198     SOILMOISTURE14("moisture-soil-channel-14", (byte) 0x46, "Soil Moisture 14", MeasureType.PERCENTAGE,
199             CHANNEL_TYPE_MOISTURE),
200
201     SOILMOISTURE15("moisture-soil-channel-15", (byte) 0x48, "Soil Moisture 15", MeasureType.PERCENTAGE,
202             CHANNEL_TYPE_MOISTURE),
203
204     SOILMOISTURE16("moisture-soil-channel-16", (byte) 0x4A, "Soil Moisture 16", MeasureType.PERCENTAGE,
205             CHANNEL_TYPE_MOISTURE),
206
207     // will no longer be used
208     // skip battery-level, since it is read via Command.CMD_READ_SENSOR_ID_NEW
209     LOWBATT((byte) 0x4C, new Skip(1)),
210
211     PM25_24HAVG1("air-quality-24-hour-average-channel-1", (byte) 0x4D, "PM2.5 Air Quality 24 hour average channel 1",
212             MeasureType.PM25),
213
214     PM25_24HAVG2("air-quality-24-hour-average-channel-2", (byte) 0x4E, "PM2.5 Air Quality 24 hour average channel 2",
215             MeasureType.PM25),
216
217     PM25_24HAVG3("air-quality-24-hour-average-channel-3", (byte) 0x4F, "PM2.5 Air Quality 24 hour average channel 3",
218             MeasureType.PM25),
219
220     PM25_24HAVG4("air-quality-24-hour-average-channel-4", (byte) 0x50, "PM2.5 Air Quality 24 hour average channel 4",
221             MeasureType.PM25),
222
223     PM25_CH1("air-quality-channel-1", (byte) 0x2A, "PM2.5 Air Quality channel 1", MeasureType.PM25),
224
225     PM25_CH2("air-quality-channel-2", (byte) 0x51, "PM2.5 Air Quality channel 2", MeasureType.PM25),
226
227     PM25_CH3("air-quality-channel-3", (byte) 0x52, "PM2.5 Air Quality channel 3", MeasureType.PM25),
228
229     PM25_CH4("air-quality-channel-4", (byte) 0x53, "PM2.5 Air Quality channel 4", MeasureType.PM25),
230
231     LEAK_CH1("water-leak-channel-1", (byte) 0x58, "Leak channel 1", MeasureType.WATER_LEAK_DETECTION),
232
233     LEAK_CH2("water-leak-channel-2", (byte) 0x59, "Leak channel 2", MeasureType.WATER_LEAK_DETECTION),
234
235     LEAK_CH3("water-leak-channel-3", (byte) 0x5A, "Leak channel 3", MeasureType.WATER_LEAK_DETECTION),
236
237     LEAK_CH4("water-leak-channel-4", (byte) 0x5B, "Leak channel 4", MeasureType.WATER_LEAK_DETECTION),
238
239     // `LIGHTNING` is the name in the spec, so we keep it here as it
240     LIGHTNING("lightning-distance", (byte) 0x60, "lightning distance 1~40KM", MeasureType.LIGHTNING_DISTANCE),
241
242     LIGHTNING_TIME("lightning-time", (byte) 0x61, "lightning happened time", MeasureType.LIGHTNING_TIME),
243
244     // `LIGHTNING_POWER` is the name in the spec, so we keep it here as it
245     LIGHTNING_POWER("lightning-counter", (byte) 0x62, "lightning counter for the day", MeasureType.LIGHTNING_COUNTER),
246
247     TF_USR1("temperature-external-channel-1", (byte) 0x63, "Soil or Water temperature channel 1",
248             MeasureType.TEMPERATURE),
249
250     TF_USR2("temperature-external-channel-2", (byte) 0x64, "Soil or Water temperature channel 2",
251             MeasureType.TEMPERATURE),
252
253     TF_USR3("temperature-external-channel-3", (byte) 0x65, "Soil or Water temperature channel 3",
254             MeasureType.TEMPERATURE),
255
256     TF_USR4("temperature-external-channel-4", (byte) 0x66, "Soil or Water temperature channel 4",
257             MeasureType.TEMPERATURE),
258
259     TF_USR5("temperature-external-channel-5", (byte) 0x67, "Soil or Water temperature channel 5",
260             MeasureType.TEMPERATURE),
261
262     TF_USR6("temperature-external-channel-6", (byte) 0x68, "Soil or Water temperature channel 6",
263             MeasureType.TEMPERATURE),
264
265     TF_USR7("temperature-external-channel-7", (byte) 0x69, "Soil or Water temperature channel 7",
266             MeasureType.TEMPERATURE),
267
268     TF_USR8("temperature-external-channel-8", (byte) 0x6A, "Soil or Water temperature channel 8",
269             MeasureType.TEMPERATURE),
270
271     ITEM_SENSOR_CO2((byte) 0x70,
272             new MeasurandParser("sensor-co2-temperature", "Temperature (CO₂-Sensor)", MeasureType.TEMPERATURE),
273             new MeasurandParser("sensor-co2-humidity", "Humidity (CO₂-Sensor)", MeasureType.PERCENTAGE),
274             new MeasurandParser("sensor-co2-pm10", "PM10 Air Quality (CO₂-Sensor)", MeasureType.PM10),
275             new MeasurandParser("sensor-co2-pm10-24-hour-average", "PM10 Air Quality 24 hour average (CO₂-Sensor)",
276                     MeasureType.PM10),
277             new MeasurandParser("sensor-co2-pm25", "PM2.5 Air Quality (CO₂-Sensor)", MeasureType.PM25),
278             new MeasurandParser("sensor-co2-pm25-24-hour-average", "PM2.5 Air Quality 24 hour average (CO₂-Sensor)",
279                     MeasureType.PM25),
280             new MeasurandParser("sensor-co2-co2", "CO₂", MeasureType.CO2),
281             new MeasurandParser("sensor-co2-co2-24-hour-average", "CO₂ 24 hour average", MeasureType.CO2),
282             // skip battery-level, since it is read via Command.CMD_READ_SENSOR_ID_NEW
283             new Skip(1)),
284
285     ITEM_LEAF_WETNESS_CH1("leaf-wetness-channel-1", (byte) 0x72, "Leaf Moisture channel 1", MeasureType.PERCENTAGE,
286             CHANNEL_TYPE_MOISTURE),
287
288     ITEM_LEAF_WETNESS_CH2("leaf-wetness-channel-2", (byte) 0x73, "Leaf Moisture channel 2", MeasureType.PERCENTAGE,
289             CHANNEL_TYPE_MOISTURE),
290
291     ITEM_LEAF_WETNESS_CH3("leaf-wetness-channel-3", (byte) 0x74, "Leaf Moisture channel 3", MeasureType.PERCENTAGE,
292             CHANNEL_TYPE_MOISTURE),
293
294     ITEM_LEAF_WETNESS_CH4("leaf-wetness-channel-4", (byte) 0x75, "Leaf Moisture channel 4", MeasureType.PERCENTAGE,
295             CHANNEL_TYPE_MOISTURE),
296
297     ITEM_LEAF_WETNESS_CH5("leaf-wetness-channel-5", (byte) 0x76, "Leaf Moisture channel 5", MeasureType.PERCENTAGE,
298             CHANNEL_TYPE_MOISTURE),
299
300     ITEM_LEAF_WETNESS_CH6("leaf-wetness-channel-6", (byte) 0x77, "Leaf Moisture channel 6", MeasureType.PERCENTAGE,
301             CHANNEL_TYPE_MOISTURE),
302
303     ITEM_LEAF_WETNESS_CH7("leaf-wetness-channel-7", (byte) 0x78, "Leaf Moisture channel 7", MeasureType.PERCENTAGE,
304             CHANNEL_TYPE_MOISTURE),
305
306     ITEM_LEAF_WETNESS_CH8("leaf-wetness-channel-8", (byte) 0x79, "Leaf Moisture channel 8", MeasureType.PERCENTAGE,
307             CHANNEL_TYPE_MOISTURE),;
308
309     private static final Map<Byte, Measurand> MEASURANDS = new HashMap<>();
310
311     static {
312         for (Measurand value : values()) {
313             MEASURANDS.put(value.code, value);
314         }
315     }
316
317     private final byte code;
318     private final Parser[] parsers;
319
320     Measurand(String channelId, byte code, String name, MeasureType measureType) {
321         this(channelId, code, name, measureType, null);
322     }
323
324     Measurand(String channelId, byte code, String name, MeasureType measureType,
325             @Nullable ChannelTypeUID channelTypeUID) {
326         this(code, new MeasurandParser(channelId, name, measureType, channelTypeUID));
327     }
328
329     Measurand(byte code, Parser... parsers) {
330         this.code = code;
331         this.parsers = parsers;
332     }
333
334     public static @Nullable Measurand getByCode(byte code) {
335         return MEASURANDS.get(code);
336     }
337
338     public int extractMeasuredValues(byte[] data, int offset, ConversionContext context, List<MeasuredValue> result) {
339         int subOffset = 0;
340         for (Parser parser : parsers) {
341             subOffset += parser.extractMeasuredValues(data, offset + subOffset, context, result);
342         }
343         return subOffset;
344     }
345
346     private interface Parser {
347         int extractMeasuredValues(byte[] data, int offset, ConversionContext context, List<MeasuredValue> result);
348     }
349
350     private static class Skip implements Parser {
351         private final int skip;
352
353         public Skip(int skip) {
354             this.skip = skip;
355         }
356
357         @Override
358         public int extractMeasuredValues(byte[] data, int offset, ConversionContext context,
359                 List<MeasuredValue> result) {
360             return skip;
361         }
362     }
363
364     private static class MeasurandParser implements Parser {
365         private final String name;
366         private final String channelId;
367         private final MeasureType measureType;
368         private final @Nullable ChannelTypeUID channelTypeUID;
369
370         MeasurandParser(String channelId, String name, MeasureType measureType) {
371             this(channelId, name, measureType, null);
372         }
373
374         MeasurandParser(String channelId, String name, MeasureType measureType,
375                 @Nullable ChannelTypeUID channelTypeUID) {
376             this.channelId = channelId;
377             this.name = name;
378             this.measureType = measureType;
379             this.channelTypeUID = channelTypeUID == null ? measureType.getChannelTypeId() : channelTypeUID;
380         }
381
382         public int extractMeasuredValues(byte[] data, int offset, ConversionContext context,
383                 List<MeasuredValue> result) {
384             State state = measureType.toState(data, offset, context);
385             if (state != null) {
386                 result.add(new MeasuredValue(measureType, channelId, channelTypeUID, state, name));
387             }
388             return measureType.getByteSize();
389         }
390     }
391 }