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