2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.siemensrds.points;
15 import javax.measure.Unit;
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;
26 import com.google.gson.annotations.SerializedName;
29 * private class: a generic data point
31 * @author Andrew Fiddian-Green - Initial contribution
35 public abstract class BasePoint {
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
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.";
46 public static final int UNDEFINED_VALUE = -1;
48 @SerializedName("rep")
50 @SerializedName("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;
71 private String @Nullable [] enumVals;
72 private boolean enumParsed = false;
73 protected boolean isEnum = false;
76 * initialize the enum value list
78 private boolean initEnum() {
80 String descr = this.descr;
81 if (descr != null && descr.contains("*")) {
82 enumVals = descr.split("\\*");
90 public int getPresentPriority() {
91 return presentPriority;
95 * abstract methods => MUST be overridden
97 public abstract int asInt();
99 public void refreshValueFrom(BasePoint from) {
100 presentPriority = from.presentPriority;
103 protected boolean isEnum() {
104 return (enumParsed ? isEnum : initEnum());
107 public State getEnum() {
110 String[] enumVals = this.enumVals;
111 if (index >= 0 && enumVals != null && index < enumVals.length) {
112 return new StringType(enumVals[index]);
115 return UnDefType.NULL;
119 * property getter for openHAB State => MUST be overridden
121 public State getState() {
122 return UnDefType.NULL;
126 * property getter for openHAB returns the Units of Measure of the point value
128 public Unit<?> getUnit() {
130 * determine the Units of Measure if available; note that other possible units
131 * (Ampere, hours, milliseconds, minutes) are currently not implemented
133 String descr = this.descr;
136 case DEGREES_CELSIUS: {
137 return SIUnits.CELSIUS;
139 case DEGREES_FAHRENHEIT: {
140 return ImperialUnits.FAHRENHEIT;
142 case DEGREES_KELVIN: {
145 case PERCENT_RELATIVE_HUMIDITY: {
146 return Units.PERCENT;
154 * property getter for JSON => MAY be overridden
156 public String commandJson(String newVal) {
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);
167 return String.format("{\"value\":%s}", newVal);
170 public String getMemberName() {
171 String memberName = this.memberName;
172 return memberName != null ? memberName : "undefined";
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());
183 return fullHierarchyName;
186 public String getPointClass() {
187 String shortHierarchyName = hierarchyNameSuffix();
188 if (shortHierarchyName != null) {
189 return shortHierarchyName;
191 return "#".concat(getMemberName());