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.homematic.internal.model;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.openhab.binding.homematic.internal.misc.MiscUtils;
19 * Object that holds the metadata and values for a datapoint.
21 * @author Gerhard Riegler - Initial contribution
23 public class HmDatapoint implements Cloneable {
25 private HmChannel channel;
27 private String description;
29 private Object previousValue;
30 private Object defaultValue;
31 private HmValueType type;
32 private HmParamsetType paramsetType;
33 private Number minValue;
34 private Number maxValue;
35 private String[] options;
36 private boolean readOnly;
37 private boolean readable;
40 private boolean virtual;
41 private boolean trigger;
43 public HmDatapoint() {
46 public HmDatapoint(String name, String description, HmValueType type, Object value, boolean readOnly,
47 HmParamsetType paramsetType) {
48 this.description = description;
50 this.readOnly = readOnly;
51 this.paramsetType = paramsetType;
59 public String getName() {
66 public void setName(String name) {
67 this.name = MiscUtils.validateCharacters(name, "Datapoint name", "_");
71 * Returns the description.
73 public String getDescription() {
78 * Sets the description.
80 public void setDescription(String description) {
81 this.description = description;
85 * Returns the channel of the datapoint.
87 public HmChannel getChannel() {
92 * Sets the channel of the datapoint.
94 public void setChannel(HmChannel channel) {
95 this.channel = channel;
101 public Object getValue() {
106 * Returns the previous value.
108 public Object getPreviousValue() {
109 return previousValue;
115 public void setValue(Object value) {
116 previousValue = this.value;
121 * Returns the option list.
123 public String[] getOptions() {
128 * Sets the option list.
130 public void setOptions(String[] options) {
131 this.options = options;
135 * Returns the index of the value in an option list.
137 public int getOptionIndex(String option) {
138 if (options != null && option != null) {
139 for (int i = 0; i < options.length; i++) {
140 String value = options[i];
141 if (option.equalsIgnoreCase(value)) {
150 * Returns the value of an option list.
152 public @Nullable String getOptionValue() {
153 Integer idx = getIntegerValue();
154 if (options != null && idx != null && idx < options.length) {
160 public @Nullable Integer getIntegerValue() {
161 if (value instanceof Integer) {
163 } else if (value != null) {
164 return Integer.parseInt(value.toString());
170 public @Nullable Double getDoubleValue() {
171 if (value instanceof Double) {
172 return (double) value;
173 } else if (value != null) {
174 return Double.parseDouble(value.toString());
181 * Returns the max value.
183 public Number getMaxValue() {
188 * Sets the max value.
190 public void setMaxValue(Number maxValue) {
191 this.maxValue = maxValue;
195 * Returns the min value.
197 public Number getMinValue() {
202 * Sets the min value.
204 public void setMinValue(Number minValue) {
205 this.minValue = minValue;
209 * Returns true, if the datapoint is readOnly.
211 public boolean isReadOnly() {
216 * Sets the readOnly flag.
218 public void setReadOnly(boolean readOnly) {
219 this.readOnly = readOnly;
223 * Returns true, if the datapoint is readable.
225 public boolean isReadable() {
230 * Sets the readable flag.
232 public void setReadable(boolean readable) {
233 this.readable = readable;
237 * Returns extra infos for this datapoint.
239 public String getInfo() {
244 * Sets extra infos for this datapoint.
246 public void setInfo(String info) {
253 public String getUnit() {
260 public void setUnit(String unit) {
267 public HmValueType getType() {
274 public void setType(HmValueType type) {
279 * Returns the paramset type.
281 public HmParamsetType getParamsetType() {
286 * Sets the paramset type.
288 public void setParamsetType(HmParamsetType paramsetType) {
289 this.paramsetType = paramsetType;
293 * Returns the default value.
295 public Object getDefaultValue() {
300 * Sets the default value.
302 public void setDefaultValue(Object defaultValue) {
303 this.defaultValue = defaultValue;
307 * Returns true, if the datapoint is a virtual datapoint.
309 public boolean isVirtual() {
314 * Marks the datapoint as a virtual datapoint.
316 public void setVirtual(boolean virtual) {
317 this.virtual = virtual;
321 * Returns true, if the datapoint is an action.
323 public boolean isActionType() {
324 return type == HmValueType.ACTION;
328 * Returns true, if the datapoint is a boolean.
330 public boolean isBooleanType() {
331 return type == HmValueType.BOOL || type == HmValueType.ACTION;
335 * Returns true, if the datapoint is a float.
337 public boolean isFloatType() {
338 return type == HmValueType.FLOAT;
342 * Returns true, if the datapoint is an integer.
344 public boolean isIntegerType() {
345 return type == HmValueType.INTEGER;
349 * Returns true, if the datapoint is a number.
351 public boolean isNumberType() {
352 return isIntegerType() || isFloatType();
356 * Returns true, if the datapoint is a string.
358 public boolean isStringType() {
359 return type == HmValueType.STRING;
363 * Returns true, if the datapoint is an enum.
365 public boolean isEnumType() {
366 return type == HmValueType.ENUM;
370 * Returns true, if the datapoint is a datetime (only for virtual datapoints).
372 public boolean isDateTimeType() {
373 return type == HmValueType.DATETIME;
377 * Returns true, if the datapoint is a variable.
379 public boolean isVariable() {
380 return channel.isGatewayVariable();
384 * Returns true, if the datapoint is a program.
386 public boolean isScript() {
387 return channel.isGatewayScript();
391 * Returns true, if the name of the datapoint starts with PRESS_.
393 public boolean isPressDatapoint() {
394 return name != null && name.startsWith("PRESS_");
398 * Sets the trigger flag.
400 public void setTrigger(boolean trigger) {
401 this.trigger = trigger;
405 * Returns true, if the datapoint should be handled as a trigger.
407 public boolean isTrigger() {
412 public HmDatapoint clone() {
413 HmDatapoint dp = new HmDatapoint(name, description, type, value, readOnly, paramsetType);
414 dp.setChannel(channel);
415 dp.setMinValue(minValue);
416 dp.setMaxValue(maxValue);
417 dp.setOptions(options);
420 dp.setVirtual(virtual);
421 dp.setReadable(readable);
422 dp.setTrigger(trigger);
423 dp.setDefaultValue(defaultValue);
428 public String toString() {
429 return String.format("%s[name=%s,value=%s,defaultValue=%s,type=%s,minValue=%s,maxValue=%s,options=%s,"
430 + "readOnly=%b,readable=%b,unit=%s,description=%s,info=%s,paramsetType=%s,virtual=%b,trigger=%b]",
431 getClass().getSimpleName(), name, value, defaultValue, type, minValue, maxValue,
432 (options == null ? null : String.join(";", options)), readOnly, readable, unit, description, info,
433 paramsetType, virtual, trigger);