]> git.basschouten.com Git - openhab-addons.git/blob
0e644e7e4302d26f65d2616db2ad4a009a809139
[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.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         info.requestFirmwareVersion();
42     }
43
44     /**
45      * Requests the current state of the given Channel.
46      *
47      * @param info the modules ModInfo cache
48      * @param channelGroup the Channel group
49      * @param number the Channel's number within the Channel group
50      */
51     protected void requestVariable(ModInfo info, LcnChannelGroup channelGroup, int number) {
52         try {
53             Variable var = getVariable(channelGroup, number);
54             info.refreshVariable(var);
55         } catch (IllegalArgumentException e) {
56             logger.warn("Could not parse variable name: {}{}", channelGroup, (number + 1));
57         }
58     }
59
60     /**
61      * Gets a Variable from the given parameters.
62      *
63      * @param channelGroup the Channel group the Variable is in
64      * @param number the number of the Variable's Channel
65      * @return the Variable
66      * @throws IllegalArgumentException when the Channel group and number do not exist
67      */
68     protected Variable getVariable(LcnChannelGroup channelGroup, int number) throws IllegalArgumentException {
69         return Variable.valueOf(channelGroup.name() + (number + 1));
70     }
71
72     /**
73      * Calculates the relative change between the current and the demanded value of a Variable.
74      *
75      * @param command the requested value
76      * @param variable the Variable type
77      * @return the difference
78      * @throws LcnException when the difference is too big
79      */
80     protected int getRelativeChange(DecimalType command, Variable variable) throws LcnException {
81         // LCN doesn't support setting thresholds or variables with absolute values. So, calculate the relative change.
82         int relativeVariableChange = (int) (command.longValue() - info.getVariableValue(variable));
83
84         int result;
85         if (relativeVariableChange > 0) {
86             result = Math.min(relativeVariableChange, getMaxAbsChange(variable));
87         } else {
88             result = Math.max(relativeVariableChange, -getMaxAbsChange(variable));
89         }
90         if (result != relativeVariableChange) {
91             logger.warn("Relative change of {} too big, limiting: {}", variable, relativeVariableChange);
92         }
93         return result;
94     }
95
96     private int getMaxAbsChange(Variable variable) {
97         switch (variable) {
98             case RVARSETPOINT1:
99             case RVARSETPOINT2:
100             case THRESHOLDREGISTER11:
101             case THRESHOLDREGISTER12:
102             case THRESHOLDREGISTER13:
103             case THRESHOLDREGISTER14:
104             case THRESHOLDREGISTER15:
105             case THRESHOLDREGISTER21:
106             case THRESHOLDREGISTER22:
107             case THRESHOLDREGISTER23:
108             case THRESHOLDREGISTER24:
109             case THRESHOLDREGISTER31:
110             case THRESHOLDREGISTER32:
111             case THRESHOLDREGISTER33:
112             case THRESHOLDREGISTER34:
113             case THRESHOLDREGISTER41:
114             case THRESHOLDREGISTER42:
115             case THRESHOLDREGISTER43:
116             case THRESHOLDREGISTER44:
117                 return 1000;
118             case VARIABLE1:
119             case VARIABLE2:
120             case VARIABLE3:
121             case VARIABLE4:
122             case VARIABLE5:
123             case VARIABLE6:
124             case VARIABLE7:
125             case VARIABLE8:
126             case VARIABLE9:
127             case VARIABLE10:
128             case VARIABLE11:
129             case VARIABLE12:
130                 return 4000;
131             case UNKNOWN:
132             case S0INPUT1:
133             case S0INPUT2:
134             case S0INPUT3:
135             case S0INPUT4:
136             default:
137                 return 0;
138         }
139     }
140 }