]> git.basschouten.com Git - openhab-addons.git/blob
dfbea5d6539b8a4cf484b5b512c2f53743ccc84c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.homematic.internal.model;
14
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.openhab.binding.homematic.internal.misc.MiscUtils;
17
18 /**
19  * Object that holds the metadata and values for a datapoint.
20  *
21  * @author Gerhard Riegler - Initial contribution
22  */
23 public class HmDatapoint implements Cloneable {
24
25     private HmChannel channel;
26     private String name;
27     private String description;
28     private Object value;
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;
38     private String info;
39     private String unit;
40     private boolean virtual;
41     private boolean trigger;
42
43     public HmDatapoint() {
44     }
45
46     public HmDatapoint(String name, String description, HmValueType type, Object value, boolean readOnly,
47             HmParamsetType paramsetType) {
48         this.description = description;
49         this.type = type;
50         this.readOnly = readOnly;
51         this.paramsetType = paramsetType;
52         this.value = value;
53         setName(name);
54     }
55
56     /**
57      * Returns the name.
58      */
59     public String getName() {
60         return name;
61     }
62
63     /**
64      * Sets the name.
65      */
66     public void setName(String name) {
67         this.name = MiscUtils.validateCharacters(name, "Datapoint name", "_");
68     }
69
70     /**
71      * Returns the description.
72      */
73     public String getDescription() {
74         return description;
75     }
76
77     /**
78      * Sets the description.
79      */
80     public void setDescription(String description) {
81         this.description = description;
82     }
83
84     /**
85      * Returns the channel of the datapoint.
86      */
87     public HmChannel getChannel() {
88         return channel;
89     }
90
91     /**
92      * Sets the channel of the datapoint.
93      */
94     public void setChannel(HmChannel channel) {
95         this.channel = channel;
96     }
97
98     /**
99      * Returns the value.
100      */
101     public Object getValue() {
102         return value;
103     }
104
105     /**
106      * Returns the previous value.
107      */
108     public Object getPreviousValue() {
109         return previousValue;
110     }
111
112     /**
113      * Sets the value.
114      */
115     public void setValue(Object value) {
116         previousValue = this.value;
117         this.value = value;
118     }
119
120     /**
121      * Returns the option list.
122      */
123     public String[] getOptions() {
124         return options;
125     }
126
127     /**
128      * Sets the option list.
129      */
130     public void setOptions(String[] options) {
131         this.options = options;
132     }
133
134     /**
135      * Returns the index of the value in an option list.
136      */
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)) {
142                     return i;
143                 }
144             }
145         }
146         return -1;
147     }
148
149     /**
150      * Returns the value of an option list.
151      */
152     public @Nullable String getOptionValue() {
153         Integer idx = getIntegerValue();
154         if (options != null && idx != null && idx < options.length) {
155             return options[idx];
156         }
157         return null;
158     }
159
160     public @Nullable Integer getIntegerValue() {
161         if (value instanceof Integer) {
162             return (int) value;
163         } else if (value != null) {
164             return Integer.parseInt(value.toString());
165         } else {
166             return null;
167         }
168     }
169
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());
175         } else {
176             return null;
177         }
178     }
179
180     /**
181      * Returns the max value.
182      */
183     public Number getMaxValue() {
184         return maxValue;
185     }
186
187     /**
188      * Sets the max value.
189      */
190     public void setMaxValue(Number maxValue) {
191         this.maxValue = maxValue;
192     }
193
194     /**
195      * Returns the min value.
196      */
197     public Number getMinValue() {
198         return minValue;
199     }
200
201     /**
202      * Sets the min value.
203      */
204     public void setMinValue(Number minValue) {
205         this.minValue = minValue;
206     }
207
208     /**
209      * Returns true, if the datapoint is readOnly.
210      */
211     public boolean isReadOnly() {
212         return readOnly;
213     }
214
215     /**
216      * Sets the readOnly flag.
217      */
218     public void setReadOnly(boolean readOnly) {
219         this.readOnly = readOnly;
220     }
221
222     /**
223      * Returns true, if the datapoint is readable.
224      */
225     public boolean isReadable() {
226         return readable;
227     }
228
229     /**
230      * Sets the readable flag.
231      */
232     public void setReadable(boolean readable) {
233         this.readable = readable;
234     }
235
236     /**
237      * Returns extra infos for this datapoint.
238      */
239     public String getInfo() {
240         return info;
241     }
242
243     /**
244      * Sets extra infos for this datapoint.
245      */
246     public void setInfo(String info) {
247         this.info = info;
248     }
249
250     /**
251      * Returns the unit.
252      */
253     public String getUnit() {
254         return unit;
255     }
256
257     /**
258      * Sets the unit.
259      */
260     public void setUnit(String unit) {
261         this.unit = unit;
262     }
263
264     /**
265      * Returns the type.
266      */
267     public HmValueType getType() {
268         return type;
269     }
270
271     /**
272      * Sets the type.
273      */
274     public void setType(HmValueType type) {
275         this.type = type;
276     }
277
278     /**
279      * Returns the paramset type.
280      */
281     public HmParamsetType getParamsetType() {
282         return paramsetType;
283     }
284
285     /**
286      * Sets the paramset type.
287      */
288     public void setParamsetType(HmParamsetType paramsetType) {
289         this.paramsetType = paramsetType;
290     }
291
292     /**
293      * Returns the default value.
294      */
295     public Object getDefaultValue() {
296         return defaultValue;
297     }
298
299     /**
300      * Sets the default value.
301      */
302     public void setDefaultValue(Object defaultValue) {
303         this.defaultValue = defaultValue;
304     }
305
306     /**
307      * Returns true, if the datapoint is a virtual datapoint.
308      */
309     public boolean isVirtual() {
310         return virtual;
311     }
312
313     /**
314      * Marks the datapoint as a virtual datapoint.
315      */
316     public void setVirtual(boolean virtual) {
317         this.virtual = virtual;
318     }
319
320     /**
321      * Returns true, if the datapoint is an action.
322      */
323     public boolean isActionType() {
324         return type == HmValueType.ACTION;
325     }
326
327     /**
328      * Returns true, if the datapoint is a boolean.
329      */
330     public boolean isBooleanType() {
331         return type == HmValueType.BOOL || type == HmValueType.ACTION;
332     }
333
334     /**
335      * Returns true, if the datapoint is a float.
336      */
337     public boolean isFloatType() {
338         return type == HmValueType.FLOAT;
339     }
340
341     /**
342      * Returns true, if the datapoint is an integer.
343      */
344     public boolean isIntegerType() {
345         return type == HmValueType.INTEGER;
346     }
347
348     /**
349      * Returns true, if the datapoint is a number.
350      */
351     public boolean isNumberType() {
352         return isIntegerType() || isFloatType();
353     }
354
355     /**
356      * Returns true, if the datapoint is a string.
357      */
358     public boolean isStringType() {
359         return type == HmValueType.STRING;
360     }
361
362     /**
363      * Returns true, if the datapoint is an enum.
364      */
365     public boolean isEnumType() {
366         return type == HmValueType.ENUM;
367     }
368
369     /**
370      * Returns true, if the datapoint is a datetime (only for virtual datapoints).
371      */
372     public boolean isDateTimeType() {
373         return type == HmValueType.DATETIME;
374     }
375
376     /**
377      * Returns true, if the datapoint is a variable.
378      */
379     public boolean isVariable() {
380         return channel.isGatewayVariable();
381     }
382
383     /**
384      * Returns true, if the datapoint is a program.
385      */
386     public boolean isScript() {
387         return channel.isGatewayScript();
388     }
389
390     /**
391      * Returns true, if the name of the datapoint starts with PRESS_.
392      */
393     public boolean isPressDatapoint() {
394         return name != null && name.startsWith("PRESS_");
395     }
396
397     /**
398      * Sets the trigger flag.
399      */
400     public void setTrigger(boolean trigger) {
401         this.trigger = trigger;
402     }
403
404     /**
405      * Returns true, if the datapoint should be handled as a trigger.
406      */
407     public boolean isTrigger() {
408         return trigger;
409     }
410
411     @Override
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);
418         dp.setInfo(info);
419         dp.setUnit(unit);
420         dp.setVirtual(virtual);
421         dp.setReadable(readable);
422         dp.setTrigger(trigger);
423         dp.setDefaultValue(defaultValue);
424         return dp;
425     }
426
427     @Override
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);
434     }
435 }