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