]> git.basschouten.com Git - openhab-addons.git/blob
27046ec79efec5a76a1e6abd7ecef55aeb9f38c1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.siemensrds.points;
14
15 import javax.measure.Unit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.core.library.unit.ImperialUnits;
21 import org.openhab.core.library.unit.SIUnits;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 import com.google.gson.annotations.SerializedName;
27
28 /**
29  * private class: a generic data point
30  *
31  * @author Andrew Fiddian-Green - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public abstract class BasePoint {
36     /*
37      * note: temperature symbols with a degree sign: the MVN Spotless formatter
38      * trashes the "degree" (looks like *) symbol, so we must escape these symbols
39      * as octal \260 or unicode \u00B00
40      */
41     public static final String DEGREES_CELSIUS = "\260C";
42     public static final String DEGREES_FAHRENHEIT = "\260F";
43     public static final String DEGREES_KELVIN = "K";
44     public static final String PERCENT_RELATIVE_HUMIDITY = "%r.H.";
45
46     public static final int UNDEFINED_VALUE = -1;
47
48     @SerializedName("rep")
49     protected int rep;
50     @SerializedName("type")
51     protected int type;
52     @SerializedName("write")
53     protected boolean write;
54     @SerializedName("descr")
55     protected @Nullable String descr;
56     @SerializedName("limits")
57     protected float @Nullable [] limits;
58     @SerializedName("descriptionName")
59     protected @Nullable String descriptionName;
60     @SerializedName("objectName")
61     protected @Nullable String objectName;
62     @SerializedName("memberName")
63     private @Nullable String memberName;
64     @SerializedName("hierarchyName")
65     private @Nullable String hierarchyName;
66     @SerializedName("translated")
67     protected boolean translated;
68     @SerializedName("presentPriority")
69     protected int presentPriority;
70
71     private String @Nullable [] enumVals;
72     private boolean enumParsed = false;
73     protected boolean isEnum = false;
74
75     /*
76      * initialize the enum value list
77      */
78     private boolean initEnum() {
79         if (!enumParsed) {
80             String descr = this.descr;
81             if (descr != null && descr.contains("*")) {
82                 enumVals = descr.split("\\*");
83                 isEnum = true;
84             }
85         }
86         enumParsed = true;
87         return isEnum;
88     }
89
90     public int getPresentPriority() {
91         return presentPriority;
92     }
93
94     /*
95      * abstract methods => MUST be overridden
96      */
97     public abstract int asInt();
98
99     public void refreshValueFrom(BasePoint from) {
100         presentPriority = from.presentPriority;
101     }
102
103     protected boolean isEnum() {
104         return (enumParsed ? isEnum : initEnum());
105     }
106
107     public State getEnum() {
108         if (isEnum()) {
109             int index = asInt();
110             String[] enumVals = this.enumVals;
111             if (index >= 0 && enumVals != null && index < enumVals.length) {
112                 return new StringType(enumVals[index]);
113             }
114         }
115         return UnDefType.NULL;
116     }
117
118     /*
119      * property getter for openHAB State => MUST be overridden
120      */
121     public State getState() {
122         return UnDefType.NULL;
123     }
124
125     /*
126      * property getter for openHAB returns the Units of Measure of the point value
127      */
128     public Unit<?> getUnit() {
129         /*
130          * determine the Units of Measure if available; note that other possible units
131          * (Ampere, hours, milliseconds, minutes) are currently not implemented
132          */
133         String descr = this.descr;
134         if (descr != null) {
135             switch (descr) {
136                 case DEGREES_CELSIUS: {
137                     return SIUnits.CELSIUS;
138                 }
139                 case DEGREES_FAHRENHEIT: {
140                     return ImperialUnits.FAHRENHEIT;
141                 }
142                 case DEGREES_KELVIN: {
143                     return Units.KELVIN;
144                 }
145                 case PERCENT_RELATIVE_HUMIDITY: {
146                     return Units.PERCENT;
147                 }
148             }
149         }
150         return Units.ONE;
151     }
152
153     /*
154      * property getter for JSON => MAY be overridden
155      */
156     public String commandJson(String newVal) {
157         if (isEnum()) {
158             String[] enumVals = this.enumVals;
159             if (enumVals != null) {
160                 for (int index = 0; index < enumVals.length; index++) {
161                     if (enumVals[index].equals(newVal)) {
162                         return String.format("{\"value\":%d}", index);
163                     }
164                 }
165             }
166         }
167         return String.format("{\"value\":%s}", newVal);
168     }
169
170     public String getMemberName() {
171         String memberName = this.memberName;
172         return memberName != null ? memberName : "undefined";
173     }
174
175     private @Nullable String hierarchyNameSuffix() {
176         String fullHierarchyName = this.hierarchyName;
177         if (fullHierarchyName != null) {
178             int suffixPosition = fullHierarchyName.lastIndexOf("'");
179             if (suffixPosition >= 0) {
180                 return fullHierarchyName.substring(suffixPosition, fullHierarchyName.length());
181             }
182         }
183         return fullHierarchyName;
184     }
185
186     public String getPointClass() {
187         String shortHierarchyName = hierarchyNameSuffix();
188         if (shortHierarchyName != null) {
189             return shortHierarchyName;
190         }
191         return "#".concat(getMemberName());
192     }
193 }