]> git.basschouten.com Git - openhab-addons.git/blob
a59308f9383ed3f9622db782ef06a09a06121a75
[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.miio.internal.miot;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Enum of the unitTypes used in the miio protocol
23  * Used to find the right {@link javax.measure.unitType} given the string of the unitType
24  *
25  * @author Marcel Verpaalen - Initial contribution
26  */
27 @NonNullByDefault
28 public enum MiIoQuantiyTypesConversion {
29
30     ANGLE("Angle", "arcdegrees", "radians"),
31     DENSITY("Density", "mg/m3"),
32     DIMENSIONLESS("Dimensionless", "percent", "percentage", "ppm"),
33     ELECTRIC_POTENTIAL("ElectricPotential", "volt"),
34     POWER("Power", "watt", "w"),
35     CURRENT("ElectricCurrent", "ampere", "mA"),
36     ILLUMINANCE("Illuminance", "lux"),
37     PRESSURE("Pressure", "pascal"),
38     TEMPERATURE("Temperature", "c", "celcius", "celsius", "f", "farenheith", "kelvin", "K"),
39     TIME("Time", "seconds", "minutes", "minute", "hour", "hours", "days", "Months"),
40     VOLUME("Volume", "litre", "liter", "m3");
41
42     /*
43      * available options according to miot spec:
44      * percentage
45      * Celsius degrees Celsius
46      * seconds
47      * minutes
48      * hours
49      * days
50      * kelvin temperature scale
51      * pascal Pascal (atmospheric pressure unit)
52      * arcdegrees radians (angle units)
53      * rgb RGB (color)
54      * watt (power)
55      * litre
56      * ppm ppm concentration
57      * lux Lux (illuminance)
58      * mg/m3 milligrams per cubic meter
59      */
60
61     private final String unitType;
62     private final String[] aliasses;
63
64     private static Map<String, String> aliasMap() {
65         Map<String, String> aliassesMap = new HashMap<>();
66         for (MiIoQuantiyTypesConversion miIoQuantiyType : values()) {
67             for (String alias : miIoQuantiyType.getAliasses()) {
68                 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getunitType());
69             }
70         }
71         return aliassesMap;
72     }
73
74     private MiIoQuantiyTypesConversion(String unitType, String... aliasses) {
75         this.unitType = unitType;
76         this.aliasses = aliasses;
77     }
78
79     public String getunitType() {
80         return unitType;
81     }
82
83     public String[] getAliasses() {
84         return aliasses;
85     }
86
87     public static @Nullable String getType(@Nullable String unitTypeName) {
88         if (unitTypeName != null) {
89             return aliasMap().get(unitTypeName.toLowerCase());
90         }
91         return null;
92     }
93 }