]> git.basschouten.com Git - openhab-addons.git/blob
821004936f65bd336c872ce5d93aa52af6613005
[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.omnilink.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 import com.digitaldan.jomnilinkII.MessageUtils;
18
19 /**
20  * The {@link TemperatureFormat} defines some methods that are used to
21  * convert OmniLink temperature values into Fahrenheit or Celsius.
22  *
23  * @author Craig Hamilton - Initial contribution
24  * @author Ethan Dye - openHAB3 rewrite
25  */
26 @NonNullByDefault
27 public enum TemperatureFormat {
28     // Don't convert zero - it appears that is what omni returns when there is no value.
29     CELSIUS(2) {
30         @Override
31         public float omniToFormat(int omniNumber) {
32             return MessageUtils.omniToC(omniNumber);
33         }
34
35         @Override
36         public int formatToOmni(float celsius) {
37             return MessageUtils.CToOmni(celsius);
38         }
39     },
40     FAHRENHEIT(1) {
41         @Override
42         public float omniToFormat(int omniNumber) {
43             return MessageUtils.omniToF(omniNumber);
44         }
45
46         @Override
47         public int formatToOmni(float fahrenheit) {
48             return MessageUtils.FtoOmni(fahrenheit);
49         }
50     };
51
52     private final int formatNumber;
53
54     private TemperatureFormat(int formatNumber) {
55         this.formatNumber = formatNumber;
56     }
57
58     /**
59      * Convert a number represented by the omni to the format.
60      *
61      * @param omniNumber Number to convert
62      * @return Number converted to appropriate format.
63      */
64     public abstract float omniToFormat(int omniNumber);
65
66     /**
67      * Convert a number from this format into an omni number.
68      *
69      * @param format Number in the current format.
70      * @return Omni formatted number.
71      */
72     public abstract int formatToOmni(float format);
73
74     /**
75      * Get the number which identifies this format as defined by the OmniLink protocol.
76      *
77      * @return Number which identifies this temperature format.
78      */
79     public int getFormatNumber() {
80         return formatNumber;
81     }
82
83     public static TemperatureFormat valueOf(int tempFormat) {
84         if (tempFormat == CELSIUS.formatNumber) {
85             return CELSIUS;
86         } else if (tempFormat == FAHRENHEIT.formatNumber) {
87             return FAHRENHEIT;
88         } else {
89             throw new IllegalArgumentException("Invalid temperature format!");
90         }
91     }
92 }