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.apache.commons.lang.StringUtils;
16 import org.apache.commons.lang.builder.ToStringBuilder;
17 import org.apache.commons.lang.builder.ToStringStyle;
18 import org.openhab.binding.homematic.internal.misc.MiscUtils;
21 * Object that holds the metadata and values for a datapoint.
23 * @author Gerhard Riegler - Initial contribution
25 public class HmDatapoint implements Cloneable {
27 private HmChannel channel;
29 private String description;
31 private Object previousValue;
32 private Object defaultValue;
33 private HmValueType type;
34 private HmParamsetType paramsetType;
35 private Number minValue;
36 private Number maxValue;
38 private String[] options;
39 private boolean readOnly;
40 private boolean readable;
43 private boolean virtual;
44 private boolean trigger;
46 public HmDatapoint() {
49 public HmDatapoint(String name, String description, HmValueType type, Object value, boolean readOnly,
50 HmParamsetType paramsetType) {
51 this.description = description;
53 this.readOnly = readOnly;
54 this.paramsetType = paramsetType;
62 public String getName() {
69 public void setName(String name) {
70 this.name = MiscUtils.validateCharacters(name, "Datapoint name", "_");
74 * Returns the description.
76 public String getDescription() {
81 * Sets the description.
83 public void setDescription(String description) {
84 this.description = description;
88 * Returns the channel of the datapoint.
90 public HmChannel getChannel() {
95 * Sets the channel of the datapoint.
97 public void setChannel(HmChannel channel) {
98 this.channel = channel;
104 public Object getValue() {
109 * Returns the previous value.
111 public Object getPreviousValue() {
112 return previousValue;
118 public void setValue(Object value) {
119 previousValue = this.value;
124 * Returns the option list.
126 public String[] getOptions() {
131 * Sets the option list.
133 public void setOptions(String[] options) {
134 this.options = options;
138 * Returns the index of the value in a option list.
140 public int getOptionIndex(String option) {
141 if (options != null && option != null) {
142 for (int i = 0; i < options.length; i++) {
143 String value = options[i];
144 if (option.equalsIgnoreCase(value)) {
153 * Returns the value of a option list.
155 public String getOptionValue() {
156 if (options != null && value != null) {
158 if (value instanceof Integer) {
161 idx = Integer.parseInt(value.toString());
163 if (idx < options.length) {
171 * Returns the max value.
173 public Number getMaxValue() {
178 * Sets the max value.
180 public void setMaxValue(Number maxValue) {
181 this.maxValue = maxValue;
185 * Returns the min value.
187 public Number getMinValue() {
192 * Sets the min value.
194 public void setMinValue(Number minValue) {
195 this.minValue = minValue;
199 * Returns the step size.
201 public Number getStep() {
206 * Sets the step size.
208 public void setStep(Number step) {
213 * Returns true, if the datapoint is readOnly.
215 public boolean isReadOnly() {
220 * Sets the readOnly flag.
222 public void setReadOnly(boolean readOnly) {
223 this.readOnly = readOnly;
227 * Returns true, if the datapoint is readable.
229 public boolean isReadable() {
234 * Sets the readable flag.
236 public void setReadable(boolean readable) {
237 this.readable = readable;
241 * Returns extra infos for this datapoint.
243 public String getInfo() {
248 * Sets extra infos for this datapoint.
250 public void setInfo(String info) {
257 public String getUnit() {
264 public void setUnit(String unit) {
271 public HmValueType getType() {
278 public void setType(HmValueType type) {
283 * Returns the paramset type.
285 public HmParamsetType getParamsetType() {
290 * Sets the paramset type.
292 public void setParamsetType(HmParamsetType paramsetType) {
293 this.paramsetType = paramsetType;
297 * Returns the default value.
299 public Object getDefaultValue() {
304 * Sets the default value.
306 public void setDefaultValue(Object defaultValue) {
307 this.defaultValue = defaultValue;
311 * Returns true, if the datapoint is a virtual datapoint.
313 public boolean isVirtual() {
318 * Marks the datapoint as a virtual datapoint.
320 public void setVirtual(boolean virtual) {
321 this.virtual = virtual;
325 * Returns true, if the datapoint is a action.
327 public boolean isActionType() {
328 return type == HmValueType.ACTION;
332 * Returns true, if the datapoint is a boolean.
334 public boolean isBooleanType() {
335 return type == HmValueType.BOOL || type == HmValueType.ACTION;
339 * Returns true, if the datapoint is a float.
341 public boolean isFloatType() {
342 return type == HmValueType.FLOAT;
346 * Returns true, if the datapoint is a integer.
348 public boolean isIntegerType() {
349 return type == HmValueType.INTEGER;
353 * Returns true, if the datapoint is a number.
355 public boolean isNumberType() {
356 return isIntegerType() || isFloatType();
360 * Returns true, if the datapoint is a string.
362 public boolean isStringType() {
363 return type == HmValueType.STRING;
367 * Returns true, if the datapoint is a enum.
369 public boolean isEnumType() {
370 return type == HmValueType.ENUM;
374 * Returns true, if the datapoint is a datetime (only for virtual datapoints).
376 public boolean isDateTimeType() {
377 return type == HmValueType.DATETIME;
381 * Returns true, if the datapoint is a variable.
383 public boolean isVariable() {
384 return channel.isGatewayVariable();
388 * Returns true, if the datapoint is a program.
390 public boolean isScript() {
391 return channel.isGatewayScript();
395 * Returns true, if the name of the datapoint starts with PRESS_.
397 public boolean isPressDatapoint() {
398 return name != null && name.startsWith("PRESS_");
402 * Sets the trigger flag.
404 public void setTrigger(boolean trigger) {
405 this.trigger = trigger;
409 * Returns true, if the datapoint should be handled as a trigger.
411 public boolean isTrigger() {
416 public HmDatapoint clone() {
417 HmDatapoint dp = new HmDatapoint(name, description, type, value, readOnly, paramsetType);
418 dp.setChannel(channel);
419 dp.setMinValue(minValue);
420 dp.setMaxValue(maxValue);
422 dp.setOptions(options);
425 dp.setVirtual(virtual);
426 dp.setReadable(readable);
427 dp.setTrigger(trigger);
428 dp.setDefaultValue(defaultValue);
433 public String toString() {
434 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("name", name).append("value", value)
435 .append("defaultValue", defaultValue).append("type", type).append("minValue", minValue)
436 .append("maxValue", maxValue).append("step", step).append("options", StringUtils.join(options, ";"))
437 .append("readOnly", readOnly).append("readable", readable).append("unit", unit)
438 .append("description", description).append("info", info).append("paramsetType", paramsetType)
439 .append("virtual", virtual).append("trigger", trigger).toString();