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