2 * Copyright (c) 2010-2022 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.openhab.binding.homematic.internal.misc.MiscUtils;
18 * Object that holds the metadata and values for a datapoint.
20 * @author Gerhard Riegler - Initial contribution
22 public class HmDatapoint implements Cloneable {
24 private HmChannel channel;
26 private String description;
28 private Object previousValue;
29 private Object defaultValue;
30 private HmValueType type;
31 private HmParamsetType paramsetType;
32 private Number minValue;
33 private Number maxValue;
34 private String[] options;
35 private boolean readOnly;
36 private boolean readable;
39 private boolean virtual;
40 private boolean trigger;
42 public HmDatapoint() {
45 public HmDatapoint(String name, String description, HmValueType type, Object value, boolean readOnly,
46 HmParamsetType paramsetType) {
47 this.description = description;
49 this.readOnly = readOnly;
50 this.paramsetType = paramsetType;
58 public String getName() {
65 public void setName(String name) {
66 this.name = MiscUtils.validateCharacters(name, "Datapoint name", "_");
70 * Returns the description.
72 public String getDescription() {
77 * Sets the description.
79 public void setDescription(String description) {
80 this.description = description;
84 * Returns the channel of the datapoint.
86 public HmChannel getChannel() {
91 * Sets the channel of the datapoint.
93 public void setChannel(HmChannel channel) {
94 this.channel = channel;
100 public Object getValue() {
105 * Returns the previous value.
107 public Object getPreviousValue() {
108 return previousValue;
114 public void setValue(Object value) {
115 previousValue = this.value;
120 * Returns the option list.
122 public String[] getOptions() {
127 * Sets the option list.
129 public void setOptions(String[] options) {
130 this.options = options;
134 * Returns the index of the value in a option list.
136 public int getOptionIndex(String option) {
137 if (options != null && option != null) {
138 for (int i = 0; i < options.length; i++) {
139 String value = options[i];
140 if (option.equalsIgnoreCase(value)) {
149 * Returns the value of a option list.
151 public String getOptionValue() {
152 if (options != null && value != null) {
154 if (value instanceof Integer) {
157 idx = Integer.parseInt(value.toString());
159 if (idx < options.length) {
167 * Returns the max value.
169 public Number getMaxValue() {
174 * Sets the max value.
176 public void setMaxValue(Number maxValue) {
177 this.maxValue = maxValue;
181 * Returns the min value.
183 public Number getMinValue() {
188 * Sets the min value.
190 public void setMinValue(Number minValue) {
191 this.minValue = minValue;
195 * Returns true, if the datapoint is readOnly.
197 public boolean isReadOnly() {
202 * Sets the readOnly flag.
204 public void setReadOnly(boolean readOnly) {
205 this.readOnly = readOnly;
209 * Returns true, if the datapoint is readable.
211 public boolean isReadable() {
216 * Sets the readable flag.
218 public void setReadable(boolean readable) {
219 this.readable = readable;
223 * Returns extra infos for this datapoint.
225 public String getInfo() {
230 * Sets extra infos for this datapoint.
232 public void setInfo(String info) {
239 public String getUnit() {
246 public void setUnit(String unit) {
253 public HmValueType getType() {
260 public void setType(HmValueType type) {
265 * Returns the paramset type.
267 public HmParamsetType getParamsetType() {
272 * Sets the paramset type.
274 public void setParamsetType(HmParamsetType paramsetType) {
275 this.paramsetType = paramsetType;
279 * Returns the default value.
281 public Object getDefaultValue() {
286 * Sets the default value.
288 public void setDefaultValue(Object defaultValue) {
289 this.defaultValue = defaultValue;
293 * Returns true, if the datapoint is a virtual datapoint.
295 public boolean isVirtual() {
300 * Marks the datapoint as a virtual datapoint.
302 public void setVirtual(boolean virtual) {
303 this.virtual = virtual;
307 * Returns true, if the datapoint is a action.
309 public boolean isActionType() {
310 return type == HmValueType.ACTION;
314 * Returns true, if the datapoint is a boolean.
316 public boolean isBooleanType() {
317 return type == HmValueType.BOOL || type == HmValueType.ACTION;
321 * Returns true, if the datapoint is a float.
323 public boolean isFloatType() {
324 return type == HmValueType.FLOAT;
328 * Returns true, if the datapoint is a integer.
330 public boolean isIntegerType() {
331 return type == HmValueType.INTEGER;
335 * Returns true, if the datapoint is a number.
337 public boolean isNumberType() {
338 return isIntegerType() || isFloatType();
342 * Returns true, if the datapoint is a string.
344 public boolean isStringType() {
345 return type == HmValueType.STRING;
349 * Returns true, if the datapoint is a enum.
351 public boolean isEnumType() {
352 return type == HmValueType.ENUM;
356 * Returns true, if the datapoint is a datetime (only for virtual datapoints).
358 public boolean isDateTimeType() {
359 return type == HmValueType.DATETIME;
363 * Returns true, if the datapoint is a variable.
365 public boolean isVariable() {
366 return channel.isGatewayVariable();
370 * Returns true, if the datapoint is a program.
372 public boolean isScript() {
373 return channel.isGatewayScript();
377 * Returns true, if the name of the datapoint starts with PRESS_.
379 public boolean isPressDatapoint() {
380 return name != null && name.startsWith("PRESS_");
384 * Sets the trigger flag.
386 public void setTrigger(boolean trigger) {
387 this.trigger = trigger;
391 * Returns true, if the datapoint should be handled as a trigger.
393 public boolean isTrigger() {
398 public HmDatapoint clone() {
399 HmDatapoint dp = new HmDatapoint(name, description, type, value, readOnly, paramsetType);
400 dp.setChannel(channel);
401 dp.setMinValue(minValue);
402 dp.setMaxValue(maxValue);
403 dp.setOptions(options);
406 dp.setVirtual(virtual);
407 dp.setReadable(readable);
408 dp.setTrigger(trigger);
409 dp.setDefaultValue(defaultValue);
414 public String toString() {
415 return String.format("%s[name=%s,value=%s,defaultValue=%s,type=%s,minValue=%s,maxValue=%s,options=%s,"
416 + "readOnly=%b,readable=%b,unit=%s,description=%s,info=%s,paramsetType=%s,virtual=%b,trigger=%b]",
417 getClass().getSimpleName(), name, value, defaultValue, type, minValue, maxValue,
418 (options == null ? null : String.join(";", options)), readOnly, readable, unit, description, info,
419 paramsetType, virtual, trigger);