]> git.basschouten.com Git - openhab-addons.git/blob
92909a4bfdf237563a73a014d105ec0ba4660152
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.foobot.internal.json;
14
15 import java.util.Map;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import javax.measure.Unit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.unit.ImperialUnits;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
27
28 /**
29  * Enum for all specific sensor data returned by the Foobot device.
30  *
31  * @author Hilbrand Bouwkamp - Initial contribution
32  */
33 @NonNullByDefault
34 public enum FoobotSensor {
35     TIME("time", "time", null),
36     PM("pm", "pm", Units.MICROGRAM_PER_CUBICMETRE),
37     TEMPERATURE("temperature", "tmp", "C", SIUnits.CELSIUS, ImperialUnits.FAHRENHEIT),
38     HUMIDITY("humidity", "hum", null),
39     CO2("co2", "co2", Units.PARTS_PER_MILLION),
40     VOC("voc", "voc", null),
41     GPI("gpi", "allpollu", null);
42
43     private final String channelId;
44     private final String dataKey;
45     private final @Nullable String matchUnit;
46     private final @Nullable Unit<?> unit;
47     private final @Nullable Unit<?> alternativeUnit;
48
49     private static final Map<String, FoobotSensor> CHANNEL_ID_MAP = Stream.of(values())
50             .collect(Collectors.toMap(FoobotSensor::getChannelId, Function.identity()));
51
52     /**
53      * Constructor.
54      *
55      * @param channelId Id of the thing channel
56      * @param dataKey key of the sensor data in the foobot sensor json data
57      * @param unit Unit of the sensor data or null if no unit specified
58      */
59     private FoobotSensor(String channelId, String dataKey, @Nullable Unit<?> unit) {
60         this(channelId, dataKey, null, unit, null);
61     }
62
63     /**
64      * Constructor.
65      *
66      * @param channelId Id of the thing channel
67      * @param dataKey key of the sensor data in the foobot sensor json data
68      * @param matchUnit unit string to be matched with the foobot returned unit
69      * @param unit Unit of the sensor data or null if no unit specified
70      * @param alternativeUnit if foobot api unit doesn't match this unit is returned
71      */
72     private FoobotSensor(String channelId, String dataKey, @Nullable String matchUnit, @Nullable Unit<?> unit,
73             @Nullable Unit<?> alternativeUnit) {
74         this.channelId = channelId;
75         this.dataKey = dataKey;
76         this.matchUnit = matchUnit;
77         this.unit = unit;
78         this.alternativeUnit = alternativeUnit;
79     }
80
81     public static @Nullable FoobotSensor findSensorByChannelId(String channelId) {
82         return CHANNEL_ID_MAP.get(channelId);
83     }
84
85     public String getChannelId() {
86         return channelId;
87     }
88
89     /**
90      * @return Returns the key of the sensor type as returned by the foobot api
91      */
92     public String getDataKey() {
93         return dataKey;
94     }
95
96     /**
97      * Returns the Unit of this sensor data type or null if no unit specified.
98      *
99      * @param unitToMath match the returned unit by the foobot api with the Unit to be returned
100      * @return Unit or null if no unit available for the sensor
101      */
102     public @Nullable Unit<?> getUnit(String unitToMath) {
103         return matchUnit == null ? unit : (matchUnit.equals(unitToMath) ? unit : alternativeUnit);
104     }
105 }