2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.modbus.helioseasycontrols.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
19 * This class represents a variable of the Helios modbus.
21 * @author Bernhard Bauer - Initial contribution
25 public class HeliosVariable implements Comparable<HeliosVariable> {
30 public static final String ACCESS_R = "R";
35 public static final String ACCESS_W = "W";
38 * Read and write access
40 public static final String ACCESS_RW = "RW";
45 public static final String TYPE_INTEGER = "int";
50 public static final String TYPE_FLOAT = "float";
55 public static final String TYPE_STRING = "string";
60 public static final String UNIT_VOLT = "V";
65 public static final String UNIT_PERCENT = "%";
70 public static final String UNIT_PPM = "ppm";
73 * Unit degrees Celsius
75 public static final String UNIT_TEMP = "°C";
80 public static final String UNIT_DAY = "d";
85 public static final String UNIT_HOUR = "h";
90 public static final String UNIT_MIN = "min";
95 public static final String UNIT_SEC = "s";
100 private int variable;
110 private @Nullable String group;
113 * The access to the variable
115 private String access;
118 * The length of the variable (number of chars)
123 * The register count for this variable
133 * The variable's unit
135 private @Nullable String unit;
138 * The minimal value (or null if not applicable)
140 private @Nullable Double minVal;
143 * The maximum value (or null if not applicable)
145 private @Nullable Double maxVal;
148 * Constructor to set the member variables
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)
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;
166 this.access = access;
167 this.length = length;
171 this.minVal = minVal;
172 this.maxVal = maxVal;
176 * Constructor to set the member variables
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
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);
193 * Constructor to set the member variables
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)
203 public HeliosVariable(int variable, String name, @Nullable String group, String access, int length, int count,
205 this(variable, name, group, access, length, count, type, null, null, null);
209 * Getter for variable
213 public int getVariable() {
214 return this.variable;
218 * Returns a formatted string representation for the variable
220 * @return String The string representation for the variable (e.g. 'v00020' for variable number 20)
222 public String getVariableString() {
223 return String.format("v%05d", variable);
230 public void setName(String name) {
239 public String getName() {
248 public @Nullable String getGroup() {
257 public String getAccess() {
266 public int getLength() {
275 public int getCount() {
280 * Getter for the type
282 public String getType() {
287 * Getter for the unit
289 public @Nullable String getUnit() {
294 * Getter for minimum value
296 * @return minimum value
298 public @Nullable Double getMinVal() {
303 * Getter for maximum value
305 * @return maximum value
307 public @Nullable Double getMaxVal() {
312 * Returns the variable's name and prefixes the group, separated by a # if available
314 * @return the variable's name and prefixes the group, separated by a # if available
316 public String getGroupAndName() {
317 return this.group != null ? this.group + "#" + this.name : this.name;
321 * Checks if the variable's data are consistent
323 * @return true if the variable contains consistent data
325 public boolean isOk() {
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));
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)));
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)));
343 check = check && (this.length > 0);
346 check = check && (this.count > 0);
352 * Checks if the variable has write access
354 * @return true if the variable has write access
356 public boolean hasWriteAccess() {
357 return (this.access.equals(HeliosVariable.ACCESS_W)) || (this.access.equals(HeliosVariable.ACCESS_RW));
361 * Checks if the variable has read access
363 * @return true if the variable has read access
365 public boolean hasReadAccess() {
366 return (this.access.equals(HeliosVariable.ACCESS_R)) || (this.access.equals(HeliosVariable.ACCESS_RW));
370 * Checks if the provided value is within the accepted range
372 * @param value The value as a string
373 * @return true if the value is within the accepted range
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
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);
389 } catch (NumberFormatException e) {
393 return true; // no range to check
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)) {
405 throw new HeliosException("Illegal value for variable " + this.getName() + ": " + value);
410 * Depending on the type of variable this method formats the provided value according to the interpretation of its
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
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:
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);
485 public int compareTo(HeliosVariable v) {
486 return getVariable() - v.getVariable();
490 public String toString() {
491 return this.getVariableString() + ": " + this.getName() + " (" + this.getAccess() + ", " + this.getType()
492 + (this.getMinVal() != null ? "[" + this.getMinVal() + "," + this.getMaxVal() + "]" : ")");