2 * Copyright (c) 2010-2023 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.MetricPrefix;
16 import javax.measure.Unit;
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;
27 import com.google.gson.annotations.SerializedName;
30 * private class: a generic data point
32 * @author Andrew Fiddian-Green - Initial contribution
36 public abstract class BasePoint {
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
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.";
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";
53 public static final int UNDEFINED_VALUE = -1;
55 @SerializedName("rep")
57 @SerializedName("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;
78 private String[] enumVals = {};
79 private boolean enumParsed = false;
80 protected boolean isEnum = false;
83 * initialize the enum value list
85 private boolean initEnum() {
87 String descr = this.descr;
88 if (descr != null && descr.contains("*")) {
89 String[] values = descr.split("\\*");
98 public int getPresentPriority() {
99 return presentPriority;
103 * abstract methods => MUST be overridden
105 public abstract int asInt();
107 public void refreshValueFrom(BasePoint from) {
108 presentPriority = from.presentPriority;
111 protected boolean isEnum() {
112 return (enumParsed ? isEnum : initEnum());
115 public State getEnum() {
118 if (index >= 0 && index < enumVals.length) {
119 return new StringType(enumVals[index]);
122 return UnDefType.NULL;
126 * property getter for openHAB State => MUST be overridden
128 public State getState() {
129 return UnDefType.NULL;
133 * property getter for openHAB returns the Units of Measure of the point value
135 public Unit<?> getUnit() {
137 * determine the Units of Measure if available
139 String descr = this.descr;
142 case DEGREES_CELSIUS: {
143 return SIUnits.CELSIUS;
145 case DEGREES_FAHRENHEIT: {
146 return ImperialUnits.FAHRENHEIT;
148 case DEGREES_KELVIN: {
151 case PERCENT_RELATIVE_HUMIDITY: {
152 return Units.PERCENT;
164 return MetricPrefix.MILLI(Units.SECOND);
166 case PARTS_PER_MILLION: {
167 return Units.PARTS_PER_MILLION;
175 * property getter for JSON => MAY be overridden
177 public String commandJson(String newVal) {
179 for (int index = 0; index < enumVals.length; index++) {
180 if (enumVals[index].equals(newVal)) {
181 return String.format("{\"value\":%d}", index);
185 return String.format("{\"value\":%s}", newVal);
188 public String getMemberName() {
189 String memberName = this.memberName;
190 return memberName != null ? memberName : "undefined";
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());
201 return fullHierarchyName;
204 public String getPointClass() {
205 String shortHierarchyName = hierarchyNameSuffix();
206 if (shortHierarchyName != null) {
207 return shortHierarchyName;
209 return "#".concat(getMemberName());