]> git.basschouten.com Git - openhab-addons.git/blob
62b0ca8b25d7763cdb98bb8a2bb3eac4c2add0e6
[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 Number step;
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 a 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 a option list.
151      */
152     public String getOptionValue() {
153         if (options != null && value != null) {
154             int idx = 0;
155             if (value instanceof Integer) {
156                 idx = (int) value;
157             } else {
158                 idx = Integer.parseInt(value.toString());
159             }
160             if (idx < options.length) {
161                 return options[idx];
162             }
163         }
164         return null;
165     }
166
167     /**
168      * Returns the max value.
169      */
170     public Number getMaxValue() {
171         return maxValue;
172     }
173
174     /**
175      * Sets the max value.
176      */
177     public void setMaxValue(Number maxValue) {
178         this.maxValue = maxValue;
179     }
180
181     /**
182      * Returns the min value.
183      */
184     public Number getMinValue() {
185         return minValue;
186     }
187
188     /**
189      * Sets the min value.
190      */
191     public void setMinValue(Number minValue) {
192         this.minValue = minValue;
193     }
194
195     /**
196      * Returns the step size.
197      */
198     public Number getStep() {
199         return step;
200     }
201
202     /**
203      * Sets the step size.
204      */
205     public void setStep(Number step) {
206         this.step = step;
207     }
208
209     /**
210      * Returns true, if the datapoint is readOnly.
211      */
212     public boolean isReadOnly() {
213         return readOnly;
214     }
215
216     /**
217      * Sets the readOnly flag.
218      */
219     public void setReadOnly(boolean readOnly) {
220         this.readOnly = readOnly;
221     }
222
223     /**
224      * Returns true, if the datapoint is readable.
225      */
226     public boolean isReadable() {
227         return readable;
228     }
229
230     /**
231      * Sets the readable flag.
232      */
233     public void setReadable(boolean readable) {
234         this.readable = readable;
235     }
236
237     /**
238      * Returns extra infos for this datapoint.
239      */
240     public String getInfo() {
241         return info;
242     }
243
244     /**
245      * Sets extra infos for this datapoint.
246      */
247     public void setInfo(String info) {
248         this.info = info;
249     }
250
251     /**
252      * Returns the unit.
253      */
254     public String getUnit() {
255         return unit;
256     }
257
258     /**
259      * Sets the unit.
260      */
261     public void setUnit(String unit) {
262         this.unit = unit;
263     }
264
265     /**
266      * Returns the type.
267      */
268     public HmValueType getType() {
269         return type;
270     }
271
272     /**
273      * Sets the type.
274      */
275     public void setType(HmValueType type) {
276         this.type = type;
277     }
278
279     /**
280      * Returns the paramset type.
281      */
282     public HmParamsetType getParamsetType() {
283         return paramsetType;
284     }
285
286     /**
287      * Sets the paramset type.
288      */
289     public void setParamsetType(HmParamsetType paramsetType) {
290         this.paramsetType = paramsetType;
291     }
292
293     /**
294      * Returns the default value.
295      */
296     public Object getDefaultValue() {
297         return defaultValue;
298     }
299
300     /**
301      * Sets the default value.
302      */
303     public void setDefaultValue(Object defaultValue) {
304         this.defaultValue = defaultValue;
305     }
306
307     /**
308      * Returns true, if the datapoint is a virtual datapoint.
309      */
310     public boolean isVirtual() {
311         return virtual;
312     }
313
314     /**
315      * Marks the datapoint as a virtual datapoint.
316      */
317     public void setVirtual(boolean virtual) {
318         this.virtual = virtual;
319     }
320
321     /**
322      * Returns true, if the datapoint is a action.
323      */
324     public boolean isActionType() {
325         return type == HmValueType.ACTION;
326     }
327
328     /**
329      * Returns true, if the datapoint is a boolean.
330      */
331     public boolean isBooleanType() {
332         return type == HmValueType.BOOL || type == HmValueType.ACTION;
333     }
334
335     /**
336      * Returns true, if the datapoint is a float.
337      */
338     public boolean isFloatType() {
339         return type == HmValueType.FLOAT;
340     }
341
342     /**
343      * Returns true, if the datapoint is a integer.
344      */
345     public boolean isIntegerType() {
346         return type == HmValueType.INTEGER;
347     }
348
349     /**
350      * Returns true, if the datapoint is a number.
351      */
352     public boolean isNumberType() {
353         return isIntegerType() || isFloatType();
354     }
355
356     /**
357      * Returns true, if the datapoint is a string.
358      */
359     public boolean isStringType() {
360         return type == HmValueType.STRING;
361     }
362
363     /**
364      * Returns true, if the datapoint is a enum.
365      */
366     public boolean isEnumType() {
367         return type == HmValueType.ENUM;
368     }
369
370     /**
371      * Returns true, if the datapoint is a datetime (only for virtual datapoints).
372      */
373     public boolean isDateTimeType() {
374         return type == HmValueType.DATETIME;
375     }
376
377     /**
378      * Returns true, if the datapoint is a variable.
379      */
380     public boolean isVariable() {
381         return channel.isGatewayVariable();
382     }
383
384     /**
385      * Returns true, if the datapoint is a program.
386      */
387     public boolean isScript() {
388         return channel.isGatewayScript();
389     }
390
391     /**
392      * Returns true, if the name of the datapoint starts with PRESS_.
393      */
394     public boolean isPressDatapoint() {
395         return name != null && name.startsWith("PRESS_");
396     }
397
398     /**
399      * Sets the trigger flag.
400      */
401     public void setTrigger(boolean trigger) {
402         this.trigger = trigger;
403     }
404
405     /**
406      * Returns true, if the datapoint should be handled as a trigger.
407      */
408     public boolean isTrigger() {
409         return trigger;
410     }
411
412     @Override
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);
418         dp.setStep(step);
419         dp.setOptions(options);
420         dp.setInfo(info);
421         dp.setUnit(unit);
422         dp.setVirtual(virtual);
423         dp.setReadable(readable);
424         dp.setTrigger(trigger);
425         dp.setDefaultValue(defaultValue);
426         return dp;
427     }
428
429     @Override
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);
436     }
437 }