2 * Copyright (c) 2010-2021 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);
41 info.requestFirmwareVersion();
45 * Requests the current state of the given Channel.
47 * @param info the modules ModInfo cache
48 * @param channelGroup the Channel group
49 * @param number the Channel's number within the Channel group
51 protected void requestVariable(ModInfo info, LcnChannelGroup channelGroup, int number) {
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));
61 * Gets a Variable from the given parameters.
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
68 protected Variable getVariable(LcnChannelGroup channelGroup, int number) throws IllegalArgumentException {
69 return Variable.valueOf(channelGroup.name() + (number + 1));
73 * Calculates the relative change between the current and the demanded value of a Variable.
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
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));
85 if (relativeVariableChange > 0) {
86 result = Math.min(relativeVariableChange, getMaxAbsChange(variable));
88 result = Math.max(relativeVariableChange, -getMaxAbsChange(variable));
90 if (result != relativeVariableChange) {
91 logger.warn("Relative change of {} too big, limiting: {}", variable, relativeVariableChange);
96 private int getMaxAbsChange(Variable variable) {
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: