2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.foobot.internal.json;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import javax.measure.Unit;
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;
29 * Enum for all specific sensor data returned by the Foobot device.
31 * @author Hilbrand Bouwkamp - Initial contribution
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);
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;
49 private static final Map<String, FoobotSensor> CHANNEL_ID_MAP = Stream.of(values())
50 .collect(Collectors.toMap(FoobotSensor::getChannelId, Function.identity()));
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
59 private FoobotSensor(String channelId, String dataKey, @Nullable Unit<?> unit) {
60 this(channelId, dataKey, null, unit, null);
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
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;
78 this.alternativeUnit = alternativeUnit;
81 public static @Nullable FoobotSensor findSensorByChannelId(String channelId) {
82 return CHANNEL_ID_MAP.get(channelId);
85 public String getChannelId() {
90 * @return Returns the key of the sensor type as returned by the foobot api
92 public String getDataKey() {
97 * Returns the Unit of this sensor data type or null if no unit specified.
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
102 public @Nullable Unit<?> getUnit(String unitToMath) {
103 return matchUnit == null ? unit : (matchUnit.equals(unitToMath) ? unit : alternativeUnit);