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.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;
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 a 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 a option list.
152 public String getOptionValue() {
153 if (options != null && value != null) {
155 if (value instanceof Integer) {
158 idx = Integer.parseInt(value.toString());
160 if (idx < options.length) {
168 * Returns the max value.
170 public Number getMaxValue() {
175 * Sets the max value.
177 public void setMaxValue(Number maxValue) {
178 this.maxValue = maxValue;
182 * Returns the min value.
184 public Number getMinValue() {
189 * Sets the min value.
191 public void setMinValue(Number minValue) {
192 this.minValue = minValue;
196 * Returns the step size.
198 public Number getStep() {
203 * Sets the step size.
205 public void setStep(Number step) {
210 * Returns true, if the datapoint is readOnly.
212 public boolean isReadOnly() {
217 * Sets the readOnly flag.
219 public void setReadOnly(boolean readOnly) {
220 this.readOnly = readOnly;
224 * Returns true, if the datapoint is readable.
226 public boolean isReadable() {
231 * Sets the readable flag.
233 public void setReadable(boolean readable) {
234 this.readable = readable;
238 * Returns extra infos for this datapoint.
240 public String getInfo() {
245 * Sets extra infos for this datapoint.
247 public void setInfo(String info) {
254 public String getUnit() {
261 public void setUnit(String unit) {
268 public HmValueType getType() {
275 public void setType(HmValueType type) {
280 * Returns the paramset type.
282 public HmParamsetType getParamsetType() {
287 * Sets the paramset type.
289 public void setParamsetType(HmParamsetType paramsetType) {
290 this.paramsetType = paramsetType;
294 * Returns the default value.
296 public Object getDefaultValue() {
301 * Sets the default value.
303 public void setDefaultValue(Object defaultValue) {
304 this.defaultValue = defaultValue;
308 * Returns true, if the datapoint is a virtual datapoint.
310 public boolean isVirtual() {
315 * Marks the datapoint as a virtual datapoint.
317 public void setVirtual(boolean virtual) {
318 this.virtual = virtual;
322 * Returns true, if the datapoint is a action.
324 public boolean isActionType() {
325 return type == HmValueType.ACTION;
329 * Returns true, if the datapoint is a boolean.
331 public boolean isBooleanType() {
332 return type == HmValueType.BOOL || type == HmValueType.ACTION;
336 * Returns true, if the datapoint is a float.
338 public boolean isFloatType() {
339 return type == HmValueType.FLOAT;
343 * Returns true, if the datapoint is a integer.
345 public boolean isIntegerType() {
346 return type == HmValueType.INTEGER;
350 * Returns true, if the datapoint is a number.
352 public boolean isNumberType() {
353 return isIntegerType() || isFloatType();
357 * Returns true, if the datapoint is a string.
359 public boolean isStringType() {
360 return type == HmValueType.STRING;
364 * Returns true, if the datapoint is a enum.
366 public boolean isEnumType() {
367 return type == HmValueType.ENUM;
371 * Returns true, if the datapoint is a datetime (only for virtual datapoints).
373 public boolean isDateTimeType() {
374 return type == HmValueType.DATETIME;
378 * Returns true, if the datapoint is a variable.
380 public boolean isVariable() {
381 return channel.isGatewayVariable();
385 * Returns true, if the datapoint is a program.
387 public boolean isScript() {
388 return channel.isGatewayScript();
392 * Returns true, if the name of the datapoint starts with PRESS_.
394 public boolean isPressDatapoint() {
395 return name != null && name.startsWith("PRESS_");
399 * Sets the trigger flag.
401 public void setTrigger(boolean trigger) {
402 this.trigger = trigger;
406 * Returns true, if the datapoint should be handled as a trigger.
408 public boolean isTrigger() {
413 public HmDatapoint clone() {
414 HmDatapoint dp = new HmDatapoint(name, description, type, value, readOnly, paramsetType);
415 dp.setChannel(channel);
416 dp.setMinValue(minValue);
417 dp.setMaxValue(maxValue);
419 dp.setOptions(options);
422 dp.setVirtual(virtual);
423 dp.setReadable(readable);
424 dp.setTrigger(trigger);
425 dp.setDefaultValue(defaultValue);
430 public String toString() {
431 return String.format("%s[name=%s,value=%s,defaultValue=%s,type=%s,minValue=%s,maxValue=%s,step=%s,options=%s,"
432 + "readOnly=%b,readable=%b,unit=%s,description=%s,info=%s,paramsetType=%s,virtual=%b,trigger=%b]",
433 getClass().getSimpleName(), name, value, defaultValue, type, minValue, maxValue, step,
434 (options == null ? null : String.join(";", options)), readOnly, readable, unit, description, info,
435 paramsetType, virtual, trigger);