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.lcn.internal.subhandler;
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;
26 * Base class for LCN module Thing sub handlers processing variables.
28 * @author Fabian Wolter - Initial contribution
31 public abstract class AbstractLcnModuleVariableSubHandler extends AbstractLcnModuleSubHandler {
32 private final Logger logger = LoggerFactory.getLogger(AbstractLcnModuleVariableSubHandler.class);
34 public AbstractLcnModuleVariableSubHandler(LcnModuleHandler handler, ModInfo info) {
39 public void handleRefresh(LcnChannelGroup channelGroup, int number) {
40 requestVariable(info, channelGroup, number);
44 * Requests the current state of the given Channel.
46 * @param info the modules ModInfo cache
47 * @param channelGroup the Channel group
48 * @param number the Channel's number within the Channel group
50 protected void requestVariable(ModInfo info, LcnChannelGroup channelGroup, int number) {
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));
60 * Gets a Variable from the given parameters.
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
67 protected Variable getVariable(LcnChannelGroup channelGroup, int number) throws IllegalArgumentException {
68 return Variable.valueOf(channelGroup.name() + (number + 1));
72 * Calculates the relative change between the current and the demanded value of a Variable.
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
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));
84 if (relativeVariableChange > 0) {
85 result = Math.min(relativeVariableChange, getMaxAbsChange(variable));
87 result = Math.max(relativeVariableChange, -getMaxAbsChange(variable));
89 if (result != relativeVariableChange) {
90 logger.warn("Relative change of {} too big, limiting: {}", variable, relativeVariableChange);
95 private int getMaxAbsChange(Variable variable) {
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: