]> git.basschouten.com Git - openhab-addons.git/blob
40d0fb37cf184df255914ba46238917685c6d796
[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.lcn.internal.subhandler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.lcn.internal.LcnModuleHandler;
17 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
18 import org.openhab.binding.lcn.internal.common.LcnException;
19 import org.openhab.binding.lcn.internal.common.Variable;
20 import org.openhab.binding.lcn.internal.connection.ModInfo;
21 import org.openhab.core.library.types.DecimalType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Base class for LCN module Thing sub handlers processing variables.
27  *
28  * @author Fabian Wolter - Initial contribution
29  */
30 @NonNullByDefault
31 public abstract class AbstractLcnModuleVariableSubHandler extends AbstractLcnModuleSubHandler {
32     private final Logger logger = LoggerFactory.getLogger(AbstractLcnModuleVariableSubHandler.class);
33
34     public AbstractLcnModuleVariableSubHandler(LcnModuleHandler handler, ModInfo info) {
35         super(handler, info);
36     }
37
38     @Override
39     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
40         requestVariable(info, channelGroup, number);
41     }
42
43     /**
44      * Requests the current state of the given Channel.
45      *
46      * @param info the modules ModInfo cache
47      * @param channelGroup the Channel group
48      * @param number the Channel's number within the Channel group
49      */
50     protected void requestVariable(ModInfo info, LcnChannelGroup channelGroup, int number) {
51         try {
52             Variable var = getVariable(channelGroup, number);
53             info.refreshVariable(var);
54         } catch (IllegalArgumentException e) {
55             logger.warn("Could not parse variable name: {}{}", channelGroup, (number + 1));
56         }
57     }
58
59     /**
60      * Gets a Variable from the given parameters.
61      *
62      * @param channelGroup the Channel group the Variable is in
63      * @param number the number of the Variable's Channel
64      * @return the Variable
65      * @throws IllegalArgumentException when the Channel group and number do not exist
66      */
67     protected Variable getVariable(LcnChannelGroup channelGroup, int number) throws IllegalArgumentException {
68         return Variable.valueOf(channelGroup.name() + (number + 1));
69     }
70
71     /**
72      * Calculates the relative change between the current and the demanded value of a Variable.
73      *
74      * @param command the requested value
75      * @param variable the Variable type
76      * @return the difference
77      * @throws LcnException when the difference is too big
78      */
79     protected int getRelativeChange(DecimalType command, Variable variable) throws LcnException {
80         // LCN doesn't support setting thresholds or variables with absolute values. So, calculate the relative change.
81         int relativeVariableChange = (int) (command.longValue() - info.getVariableValue(variable));
82
83         int result;
84         if (relativeVariableChange > 0) {
85             result = Math.min(relativeVariableChange, getMaxAbsChange(variable));
86         } else {
87             result = Math.max(relativeVariableChange, -getMaxAbsChange(variable));
88         }
89         if (result != relativeVariableChange) {
90             logger.warn("Relative change of {} too big, limiting: {}", variable, relativeVariableChange);
91         }
92         return result;
93     }
94
95     private int getMaxAbsChange(Variable variable) {
96         switch (variable) {
97             case RVARSETPOINT1:
98             case RVARSETPOINT2:
99             case THRESHOLDREGISTER11:
100             case THRESHOLDREGISTER12:
101             case THRESHOLDREGISTER13:
102             case THRESHOLDREGISTER14:
103             case THRESHOLDREGISTER15:
104             case THRESHOLDREGISTER21:
105             case THRESHOLDREGISTER22:
106             case THRESHOLDREGISTER23:
107             case THRESHOLDREGISTER24:
108             case THRESHOLDREGISTER31:
109             case THRESHOLDREGISTER32:
110             case THRESHOLDREGISTER33:
111             case THRESHOLDREGISTER34:
112             case THRESHOLDREGISTER41:
113             case THRESHOLDREGISTER42:
114             case THRESHOLDREGISTER43:
115             case THRESHOLDREGISTER44:
116                 return 1000;
117             case VARIABLE1:
118             case VARIABLE2:
119             case VARIABLE3:
120             case VARIABLE4:
121             case VARIABLE5:
122             case VARIABLE6:
123             case VARIABLE7:
124             case VARIABLE8:
125             case VARIABLE9:
126             case VARIABLE10:
127             case VARIABLE11:
128             case VARIABLE12:
129                 return 4000;
130             case UNKNOWN:
131             case S0INPUT1:
132             case S0INPUT2:
133             case S0INPUT3:
134             case S0INPUT4:
135             default:
136                 return 0;
137         }
138     }
139 }