2 * Copyright (c) 2010-2020 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.types.State;
23 import org.openhab.core.types.UnDefType;
25 import com.google.gson.annotations.SerializedName;
27 import tec.uom.se.AbstractUnit;
28 import tec.uom.se.unit.Units;
31 * private class: a generic data point
33 * @author Andrew Fiddian-Green - Initial contribution
37 public abstract class BasePoint {
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
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.";
48 public static final int UNDEFINED_VALUE = -1;
50 @SerializedName("rep")
52 @SerializedName("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;
73 private String @Nullable [] enumVals;
74 private boolean enumParsed = false;
75 protected boolean isEnum = false;
78 * initialize the enum value list
80 private boolean initEnum() {
82 String descr = this.descr;
83 if (descr != null && descr.contains("*")) {
84 enumVals = descr.split("\\*");
92 public int getPresentPriority() {
93 return presentPriority;
97 * abstract methods => MUST be overridden
99 public abstract int asInt();
101 public void refreshValueFrom(BasePoint from) {
102 presentPriority = from.presentPriority;
105 protected boolean isEnum() {
106 return (enumParsed ? isEnum : initEnum());
109 public State getEnum() {
112 String[] enumVals = this.enumVals;
113 if (index >= 0 && enumVals != null && index < enumVals.length) {
114 return new StringType(enumVals[index]);
117 return UnDefType.NULL;
121 * property getter for openHAB State => MUST be overridden
123 public State getState() {
124 return UnDefType.NULL;
128 * property getter for openHAB returns the Units of Measure of the point value
130 public Unit<?> getUnit() {
132 * determine the Units of Measure if available; note that other possible units
133 * (Ampere, hours, milliseconds, minutes) are currently not implemented
135 String descr = this.descr;
138 case DEGREES_CELSIUS: {
139 return SIUnits.CELSIUS;
141 case DEGREES_FAHRENHEIT: {
142 return ImperialUnits.FAHRENHEIT;
144 case DEGREES_KELVIN: {
147 case PERCENT_RELATIVE_HUMIDITY: {
148 return Units.PERCENT;
152 return AbstractUnit.ONE;
156 * property getter for JSON => MAY be overridden
158 public String commandJson(String newVal) {
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);
169 return String.format("{\"value\":%s}", newVal);
172 public String getMemberName() {
173 String memberName = this.memberName;
174 return memberName != null ? memberName : "undefined";
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());
185 return fullHierarchyName;
188 public String getPointClass() {
189 String shortHierarchyName = hierarchyNameSuffix();
190 if (shortHierarchyName != null) {
191 return shortHierarchyName;
193 return "#".concat(getMemberName());