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