]> git.basschouten.com Git - openhab-addons.git/blob
49cf9a3f297cab9e4ab4660b18ad7ec0a8d9478f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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;
14
15 import java.util.Arrays;
16 import java.util.Map;
17 import java.util.stream.Collectors;
18
19 import javax.measure.Unit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.unit.ImperialUnits;
24 import org.openhab.core.library.unit.SIUnits;
25 import org.openhab.core.library.unit.SmartHomeUnits;
26
27 /**
28  * Enum of the units used in the miio protocol
29  * Used to find the right {@link javax.measure.Unit} given the string of the unit
30  *
31  * @author Marcel Verpaalen - Initial contribution
32  */
33 @NonNullByDefault
34 public enum MiIoQuantiyTypes {
35
36     CELCIUS(SIUnits.CELSIUS),
37     FAHRENHEIT(ImperialUnits.FAHRENHEIT),
38     SECOND(SmartHomeUnits.SECOND),
39     MINUTE(SmartHomeUnits.MINUTE),
40     HOUR(SmartHomeUnits.HOUR),
41     SECONDS(SmartHomeUnits.SECOND),
42     MINUTES(SmartHomeUnits.MINUTE),
43     HOURS(SmartHomeUnits.HOUR),
44     AMPERE(SmartHomeUnits.AMPERE),
45     WATT(SmartHomeUnits.WATT);
46
47     private final Unit<?> unit;
48
49     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
50             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
51
52     private MiIoQuantiyTypes(Unit<?> unit) {
53         this.unit = unit;
54     }
55
56     public Unit<?> getUnit() {
57         return unit;
58     }
59
60     public static @Nullable Unit<?> get(String unitName) {
61         return stringMap.get(unitName.toUpperCase());
62     }
63 }