]> git.basschouten.com Git - openhab-addons.git/blob
9c75c303c363418851712755194d596cceefdb83
[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.modbus.helioseasycontrols.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * This class represents a variable of the Helios modbus.
20  *
21  * @author Bernhard Bauer - Initial contribution
22  * @version 2.0
23  */
24 @NonNullByDefault
25 public class HeliosVariable implements Comparable<HeliosVariable> {
26
27     /**
28      * Read access
29      */
30     public static final String ACCESS_R = "R";
31
32     /**
33      * Write access
34      */
35     public static final String ACCESS_W = "W";
36
37     /**
38      * Read and write access
39      */
40     public static final String ACCESS_RW = "RW";
41
42     /**
43      * Integer type
44      */
45     public static final String TYPE_INTEGER = "int";
46
47     /**
48      * Float type
49      */
50     public static final String TYPE_FLOAT = "float";
51
52     /**
53      * String type
54      */
55     public static final String TYPE_STRING = "string";
56
57     /**
58      * Unit Volt
59      */
60     public static final String UNIT_VOLT = "V";
61
62     /**
63      * Unit %
64      */
65     public static final String UNIT_PERCENT = "%";
66
67     /**
68      * Unit ppm
69      */
70     public static final String UNIT_PPM = "ppm";
71
72     /**
73      * Unit degrees Celsius
74      */
75     public static final String UNIT_TEMP = "°C";
76
77     /**
78      * Unit day
79      */
80     public static final String UNIT_DAY = "d";
81
82     /**
83      * Unit hour
84      */
85     public static final String UNIT_HOUR = "h";
86
87     /**
88      * Unit minute
89      */
90     public static final String UNIT_MIN = "min";
91
92     /**
93      * Unit second
94      */
95     public static final String UNIT_SEC = "s";
96
97     /**
98      * The variable number
99      */
100     private int variable;
101
102     /**
103      * The variable name
104      */
105     private String name;
106
107     /**
108      * The variable group
109      */
110     private @Nullable String group;
111
112     /**
113      * The access to the variable
114      */
115     private String access;
116
117     /**
118      * The length of the variable (number of chars)
119      */
120     private int length;
121
122     /**
123      * The register count for this variable
124      */
125     private int count;
126
127     /**
128      * The variable type
129      */
130     private String type;
131
132     /**
133      * The variable's unit
134      */
135     private @Nullable String unit;
136
137     /**
138      * The minimal value (or null if not applicable)
139      */
140     private @Nullable Double minVal;
141
142     /**
143      * The maximum value (or null if not applicable)
144      */
145     private @Nullable Double maxVal;
146
147     /**
148      * Constructor to set the member variables
149      *
150      * @param variable The variable's number
151      * @param name The variable's name
152      * @param group The variable's group
153      * @param access Access possibilities
154      * @param length Number of expected characters when writing to / reading from Modbus
155      * @param count Exact number of characters to write to Modbus
156      * @param type Variable type (string, integer or float)
157      * @param unit Variable's unit
158      * @param minVal Minimum value (only applicable for numeric values)
159      * @param maxVal Maximum value (only applicable for numeric values)
160      */
161     public HeliosVariable(int variable, String name, @Nullable String group, String access, int length, int count,
162             String type, @Nullable String unit, @Nullable Double minVal, @Nullable Double maxVal) {
163         this.variable = variable;
164         this.name = name;
165         this.group = group;
166         this.access = access;
167         this.length = length;
168         this.count = count;
169         this.type = type;
170         this.unit = unit;
171         this.minVal = minVal;
172         this.maxVal = maxVal;
173     }
174
175     /**
176      * Constructor to set the member variables
177      *
178      * @param variable The variable's number
179      * @param name The variable's name
180      * @param group The variable's group
181      * @param access Access possibilities
182      * @param length Number of expected characters when writing to / reading from Modbus
183      * @param count Exact number of characters to write to Modbus
184      * @param type Variable type (string, integer or float)
185      * @param unit Variable's unit
186      */
187     public HeliosVariable(int variable, String name, @Nullable String group, String access, int length, int count,
188             String type, String unit) {
189         this(variable, name, group, access, length, count, type, unit, null, null);
190     }
191
192     /**
193      * Constructor to set the member variables
194      *
195      * @param variable The variable's number
196      * @param name The variable's name
197      * @param group The variable's group
198      * @param access Access possibilities
199      * @param length Number of expected characters when writing to / reading from Modbus
200      * @param count Exact number of characters to write to Modbus
201      * @param type Variable type (string, integer or float)
202      */
203     public HeliosVariable(int variable, String name, @Nullable String group, String access, int length, int count,
204             String type) {
205         this(variable, name, group, access, length, count, type, null, null, null);
206     }
207
208     /**
209      * Getter for variable
210      *
211      * @return variable
212      */
213     public int getVariable() {
214         return this.variable;
215     }
216
217     /**
218      * Returns a formatted string representation for the variable
219      *
220      * @return String The string representation for the variable (e.g. 'v00020' for variable number 20)
221      */
222     public String getVariableString() {
223         return String.format("v%05d", variable);
224     }
225
226     /**
227      * Setter for name
228      *
229      */
230     public void setName(String name) {
231         this.name = name;
232     }
233
234     /**
235      * Getter for name
236      *
237      * @return name
238      */
239     public String getName() {
240         return this.name;
241     }
242
243     /**
244      * Getter for group
245      *
246      * @return group
247      */
248     public @Nullable String getGroup() {
249         return this.group;
250     }
251
252     /**
253      * Getter for access
254      *
255      * @return access
256      */
257     public String getAccess() {
258         return this.access;
259     }
260
261     /**
262      * Getter for length
263      *
264      * @return length
265      */
266     public int getLength() {
267         return this.length;
268     }
269
270     /**
271      * Getter for count
272      *
273      * @return count
274      */
275     public int getCount() {
276         return this.count;
277     }
278
279     /**
280      * Getter for the type
281      */
282     public String getType() {
283         return this.type;
284     }
285
286     /**
287      * Getter for the unit
288      */
289     public @Nullable String getUnit() {
290         return this.unit;
291     }
292
293     /**
294      * Getter for minimum value
295      *
296      * @return minimum value
297      */
298     public @Nullable Double getMinVal() {
299         return this.minVal;
300     }
301
302     /**
303      * Getter for maximum value
304      *
305      * @return maximum value
306      */
307     public @Nullable Double getMaxVal() {
308         return this.maxVal;
309     }
310
311     /**
312      * Returns the variable's name and prefixes the group, separated by a # if available
313      *
314      * @return the variable's name and prefixes the group, separated by a # if available
315      */
316     public String getGroupAndName() {
317         return this.group != null ? this.group + "#" + this.name : this.name;
318     }
319
320     /**
321      * Checks if the variable's data are consistent
322      *
323      * @return true if the variable contains consistent data
324      */
325     public boolean isOk() {
326         boolean check;
327
328         // this.access has one of the allowed values
329         check = (this.access.equals(HeliosVariable.ACCESS_R)) || (this.access.equals(HeliosVariable.ACCESS_W))
330                 || (this.access.equals(HeliosVariable.ACCESS_RW));
331
332         // this.type has one of the allowed values
333         check = check && ((this.type.equals(HeliosVariable.TYPE_STRING))
334                 || (this.type.equals(HeliosVariable.TYPE_INTEGER)) || (this.type.equals(HeliosVariable.TYPE_FLOAT)));
335
336         // this.minValue and this.maxValue are either not set or minValue is less than maxValue
337         Double minVal = this.getMinVal();
338         Double maxVal = this.getMaxVal();
339         check = check && (((minVal == null) && (maxVal == null))
340                 || ((minVal != null) && (maxVal != null) && (minVal <= maxVal)));
341
342         // length is set
343         check = check && (this.length > 0);
344
345         // count is set
346         check = check && (this.count > 0);
347
348         return check;
349     }
350
351     /**
352      * Checks if the variable has write access
353      *
354      * @return true if the variable has write access
355      */
356     public boolean hasWriteAccess() {
357         return (this.access.equals(HeliosVariable.ACCESS_W)) || (this.access.equals(HeliosVariable.ACCESS_RW));
358     }
359
360     /**
361      * Checks if the variable has read access
362      *
363      * @return true if the variable has read access
364      */
365     public boolean hasReadAccess() {
366         return (this.access.equals(HeliosVariable.ACCESS_R)) || (this.access.equals(HeliosVariable.ACCESS_RW));
367     }
368
369     /**
370      * Checks if the provided value is within the accepted range
371      *
372      * @param value The value as a string
373      * @return true if the value is within the accepted range
374      */
375     public boolean isInAllowedRange(String value) {
376         Double minVal = this.getMinVal();
377         Double maxVal = this.getMaxVal();
378         if ((minVal != null) && (maxVal != null)) { // min and max value are set
379             try {
380                 if (this.type.equals(HeliosVariable.TYPE_INTEGER)) {
381                     // using long becuase some variable are specified with a max of 2^32-1
382                     // parsing double to allow floating point values to be processed as well
383                     long l = Double.valueOf(value).longValue();
384                     return (minVal.longValue() <= l) && (maxVal.longValue() >= l);
385                 } else if (this.type.equals(HeliosVariable.TYPE_FLOAT)) {
386                     double d = Double.parseDouble(value);
387                     return (minVal <= d) && (maxVal >= d);
388                 }
389             } catch (NumberFormatException e) {
390                 return false;
391             }
392         } else {
393             return true; // no range to check
394         }
395         return false;
396     }
397
398     private String getText(String property, String value, HeliosEasyControlsTranslationProvider translationProvider)
399             throws HeliosException {
400         String key = "property." + property + "." + value;
401         String text = translationProvider.getText(key);
402         if (!key.equals(text)) {
403             return text;
404         } else {
405             throw new HeliosException("Illegal value for variable " + this.getName() + ": " + value);
406         }
407     }
408
409     /**
410      * Depending on the type of variable this method formats the provided value according to the interpretation of its
411      * value
412      *
413      * @param value The value to format
414      * @param translationProvider The i18n translation provider to use for finding internationalized texts
415      * @return The formatted value
416      * @throws HeliosException if the provided value doesn't fit to the variable
417      */
418     public String formatPropertyValue(String value, HeliosEasyControlsTranslationProvider translationProvider)
419             throws HeliosException {
420         switch (this.getName()) {
421             case HeliosEasyControlsBindingConstants.DATE_FORMAT:
422             case HeliosEasyControlsBindingConstants.UNIT_CONFIG:
423             case HeliosEasyControlsBindingConstants.HEAT_EXCHANGER_TYPE:
424             case HeliosEasyControlsBindingConstants.ASSIGNMENT_FAN_STAGES:
425             case HeliosEasyControlsBindingConstants.VHZ_TYPE:
426                 return this.getText(this.getName(), value, translationProvider);
427             case HeliosEasyControlsBindingConstants.KWL_BE:
428             case HeliosEasyControlsBindingConstants.KWL_BEC:
429                 return this.getText("onOff", value, translationProvider);
430             case HeliosEasyControlsBindingConstants.EXTERNAL_CONTACT:
431             case HeliosEasyControlsBindingConstants.FUNCTION_TYPE_KWL_EM:
432                 return this.getText("function", value, translationProvider);
433             case HeliosEasyControlsBindingConstants.OFFSET_EXTRACT_AIR:
434             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_1_EXTRACT_AIR:
435             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_2_EXTRACT_AIR:
436             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_3_EXTRACT_AIR:
437             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_4_EXTRACT_AIR:
438             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_1_SUPPLY_AIR:
439             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_2_SUPPLY_AIR:
440             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_3_SUPPLY_AIR:
441             case HeliosEasyControlsBindingConstants.VOLTAGE_FAN_STAGE_4_SUPPLY_AIR:
442                 return this.getUnit() != null ? value + this.getUnit() : value;
443             case HeliosEasyControlsBindingConstants.FAN_STAGE_STEPPED_0TO2V:
444             case HeliosEasyControlsBindingConstants.FAN_STAGE_STEPPED_2TO4V:
445             case HeliosEasyControlsBindingConstants.FAN_STAGE_STEPPED_4TO6V:
446             case HeliosEasyControlsBindingConstants.FAN_STAGE_STEPPED_6TO8V:
447             case HeliosEasyControlsBindingConstants.FAN_STAGE_STEPPED_8TO10V:
448             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_1:
449             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_2:
450             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_3:
451             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_4:
452             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_5:
453             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_6:
454             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_7:
455             case HeliosEasyControlsBindingConstants.SENSOR_NAME_HUMIDITY_AND_TEMP_8:
456                 return value;
457             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_0:
458             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_1:
459             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_2:
460             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_3:
461             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_4:
462             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_5:
463             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_6:
464             case HeliosEasyControlsBindingConstants.KWL_FTF_CONFIG_7:
465                 return this.getText("kwlFtfConfig", value, translationProvider);
466             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_1:
467             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_2:
468             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_3:
469             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_4:
470             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_5:
471             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_6:
472             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_7:
473             case HeliosEasyControlsBindingConstants.SENSOR_CONFIG_KWL_FTF_8:
474                 return this.getText("sensorConfig", value, translationProvider);
475             case HeliosEasyControlsBindingConstants.HUMIDITY_CONTROL_STATUS:
476             case HeliosEasyControlsBindingConstants.CO2_CONTROL_STATUS:
477             case HeliosEasyControlsBindingConstants.VOC_CONTROL_STATUS:
478                 return this.getText("controlStatus", value, translationProvider);
479             default:
480                 return value;
481         }
482     }
483
484     @Override
485     public int compareTo(HeliosVariable v) {
486         return getVariable() - v.getVariable();
487     }
488
489     @Override
490     public String toString() {
491         return this.getVariableString() + ": " + this.getName() + " (" + this.getAccess() + ", " + this.getType()
492                 + (this.getMinVal() != null ? "[" + this.getMinVal() + "," + this.getMaxVal() + "]" : ")");
493     }
494 }