]> git.basschouten.com Git - openhab-addons.git/blob
075c34c9e01294e47d0bb88793d405d9ab107903
[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 javax.measure.MetricPrefix.HECTO;
16 import static javax.measure.MetricPrefix.KILO;
17 import static javax.measure.MetricPrefix.MILLI;
18 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_CO2;
19 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_HUMIDITY;
20 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_ILLUMINATION;
21 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_LIGHTNING_COUNTER;
22 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_LIGHTNING_DISTANCE;
23 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_LIGHTNING_TIME;
24 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_PM10;
25 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_PM25;
26 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_PRESSURE;
27 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_RAIN;
28 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_RAIN_RATE;
29 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_TEMPERATURE;
30 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_UV_RADIATION;
31 import static org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants.CHANNEL_TYPE_WATER_LEAK_DETECTION;
32 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toInt16;
33 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toUInt16;
34 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toUInt32;
35 import static org.openhab.binding.fineoffsetweatherstation.internal.Utils.toUInt8;
36 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
37 import static org.openhab.core.library.unit.SIUnits.METRE;
38 import static org.openhab.core.library.unit.SIUnits.PASCAL;
39 import static org.openhab.core.library.unit.Units.DEGREE_ANGLE;
40 import static org.openhab.core.library.unit.Units.METRE_PER_SECOND;
41 import static org.openhab.core.library.unit.Units.MICROGRAM_PER_CUBICMETRE;
42 import static org.openhab.core.library.unit.Units.MILLIMETRE_PER_HOUR;
43 import static org.openhab.core.library.unit.Units.PARTS_PER_MILLION;
44 import static org.openhab.core.library.unit.Units.PERCENT;
45
46 import java.time.Instant;
47 import java.time.ZonedDateTime;
48 import java.util.function.BiFunction;
49
50 import javax.measure.Unit;
51
52 import org.eclipse.jdt.annotation.NonNullByDefault;
53 import org.eclipse.jdt.annotation.Nullable;
54 import org.openhab.binding.fineoffsetweatherstation.internal.Utils;
55 import org.openhab.core.library.types.DateTimeType;
56 import org.openhab.core.library.types.DecimalType;
57 import org.openhab.core.library.types.OnOffType;
58 import org.openhab.core.library.types.QuantityType;
59 import org.openhab.core.library.unit.Units;
60 import org.openhab.core.thing.type.ChannelTypeUID;
61 import org.openhab.core.types.State;
62
63 /**
64  * Represents the measured type with conversion from the sensors' bytes to the openHAB state.
65  *
66  * @author Andreas Berger - Initial contribution
67  */
68 @NonNullByDefault
69 public enum MeasureType {
70
71     TEMPERATURE(CELSIUS, 2, CHANNEL_TYPE_TEMPERATURE, (data, offset) -> toInt16(data, offset) / 10.),
72
73     PERCENTAGE(PERCENT, 1, CHANNEL_TYPE_HUMIDITY, (data, offset) -> toUInt8(data[offset])),
74
75     PRESSURE(HECTO(PASCAL), 2, CHANNEL_TYPE_PRESSURE, Utils::toUInt16),
76
77     DEGREE(DEGREE_ANGLE, 2, null, Utils::toUInt16),
78
79     SPEED(METRE_PER_SECOND, 2, null, (data, offset) -> toUInt16(data, offset) / 10.),
80
81     HEIGHT(MILLI(METRE), 2, CHANNEL_TYPE_RAIN, (data, offset) -> toUInt16(data, offset) / 10.),
82
83     HEIGHT_BIG(MILLI(METRE), 4, CHANNEL_TYPE_RAIN, (data, offset) -> toUInt32(data, offset) / 10.),
84
85     HEIGHT_PER_HOUR(MILLIMETRE_PER_HOUR, 2, CHANNEL_TYPE_RAIN_RATE, (data, offset) -> toUInt16(data, offset) / 10.),
86
87     LUX(Units.LUX, 4, CHANNEL_TYPE_ILLUMINATION, (data, offset) -> toUInt32(data, offset) / 10.),
88
89     PM25(MICROGRAM_PER_CUBICMETRE, 2, CHANNEL_TYPE_PM25, (data, offset) -> toUInt16(data, offset) / 10.),
90
91     PM10(MICROGRAM_PER_CUBICMETRE, 2, CHANNEL_TYPE_PM10, (data, offset) -> toUInt16(data, offset) / 10.),
92
93     CO2(PARTS_PER_MILLION, 2, CHANNEL_TYPE_CO2, Utils::toUInt16),
94
95     WATER_LEAK_DETECTION(1, CHANNEL_TYPE_WATER_LEAK_DETECTION,
96             (data, offset, context) -> toUInt8(data[offset]) != 0 ? OnOffType.ON : OnOffType.OFF),
97
98     LIGHTNING_DISTANCE(KILO(METRE), 1, CHANNEL_TYPE_LIGHTNING_DISTANCE, (data, offset) -> toUInt8(data[offset])),
99
100     LIGHTNING_COUNTER(4, CHANNEL_TYPE_LIGHTNING_COUNTER,
101             (data, offset, context) -> new DecimalType(toUInt32(data, offset))),
102
103     LIGHTNING_TIME(4, CHANNEL_TYPE_LIGHTNING_TIME,
104             (data, offset, context) -> new DateTimeType(
105                     ZonedDateTime.ofInstant(Instant.ofEpochSecond(toUInt32(data, offset)), context.getZoneId()))),
106
107     MICROWATT_PER_SQUARE_CENTIMETRE(Units.MICROWATT_PER_SQUARE_CENTIMETRE, 2, CHANNEL_TYPE_UV_RADIATION,
108             Utils::toUInt16),
109
110     BYTE(1, null, (data, offset, context) -> new DecimalType(toUInt8(data[offset]))),
111
112     DATE_TIME2(6, null, (data, offset, context) -> new DateTimeType(
113             ZonedDateTime.ofInstant(Instant.ofEpochSecond(toUInt32(data, offset)), context.getZoneId())));
114
115     private final int byteSize;
116     private final @Nullable ChannelTypeUID channelTypeUID;
117     private final StateConverter stateConverter;
118
119     /**
120      * @param unit the unit
121      * @param byteSize the size in the sensors' payload
122      * @param channelTypeUID the channel type
123      * @param valueExtractor a function to extract the sensor data into a number of the dimension defined by the unit
124      */
125     MeasureType(Unit<?> unit, int byteSize, @Nullable ChannelTypeUID channelTypeUID,
126             BiFunction<byte[], Integer, @Nullable Number> valueExtractor) {
127         this(byteSize, channelTypeUID, (bytes, offset, context) -> {
128             Number value = valueExtractor.apply(bytes, offset);
129             return value == null ? null : new QuantityType<>(value, unit);
130         });
131     }
132
133     /**
134      * @param byteSize the size in the sensors' payload
135      * @param channelTypeUID the channel type
136      * @param stateConverter a function to extract the sensor data into the openHAB's state
137      */
138     MeasureType(int byteSize, @Nullable ChannelTypeUID channelTypeUID, StateConverter stateConverter) {
139         this.byteSize = byteSize;
140         this.channelTypeUID = channelTypeUID;
141         this.stateConverter = stateConverter;
142     }
143
144     public int getByteSize() {
145         return byteSize;
146     }
147
148     public @Nullable ChannelTypeUID getChannelTypeId() {
149         return channelTypeUID;
150     }
151
152     public @Nullable State toState(byte[] data, int offset, ConversionContext context) {
153         return stateConverter.toState(data, offset, context);
154     }
155
156     private interface StateConverter {
157         @Nullable
158         State toState(byte[] data, int offset, ConversionContext context);
159     }
160 }