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 java.util.Collection;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.lcn.internal.LcnBindingConstants;
22 import org.openhab.binding.lcn.internal.LcnModuleHandler;
23 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
24 import org.openhab.binding.lcn.internal.common.LcnDefs;
25 import org.openhab.binding.lcn.internal.common.LcnException;
26 import org.openhab.binding.lcn.internal.common.PckGenerator;
27 import org.openhab.binding.lcn.internal.common.Variable;
28 import org.openhab.binding.lcn.internal.common.Variable.Type;
29 import org.openhab.binding.lcn.internal.common.VariableValue;
30 import org.openhab.binding.lcn.internal.connection.ModInfo;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Handles Commands and State changes of regulator setpoints of an LCN module.
39 * @author Fabian Wolter - Initial contribution
42 public class LcnModuleRvarSetpointSubHandler extends AbstractLcnModuleVariableSubHandler {
43 private final Logger logger = LoggerFactory.getLogger(LcnModuleRvarSetpointSubHandler.class);
44 private static final Pattern PATTERN = Pattern
45 .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.S(?<id>\\d)(?<value>\\d{1,5})");
47 public LcnModuleRvarSetpointSubHandler(LcnModuleHandler handler, ModInfo info) {
52 public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
54 Variable variable = getVariable(channelGroup, number);
56 if (info.hasExtendedMeasurementProcessing()) {
57 handler.sendPck(PckGenerator.setSetpointAbsolute(number, command.intValue()));
60 int relativeVariableChange = getRelativeChange(command, variable);
62 PckGenerator.setSetpointRelative(number, LcnDefs.RelVarRef.CURRENT, relativeVariableChange));
63 } catch (LcnException e) {
64 // current value unknown for some reason, refresh it in case we come again here
65 info.refreshVariable(variable);
72 public void handleStatusMessage(Matcher matcher) throws LcnException {
74 if (matcher.pattern() == PATTERN) {
75 variable = Variable.setPointIdToVar(Integer.parseInt(matcher.group("id")) - 1);
76 } else if (matcher.pattern() == LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013) {
77 variable = info.getLastRequestedVarWithoutTypeInResponse();
79 if (variable.getType() != Type.REGULATOR) {
83 logger.warn("Unexpected pattern: {}", matcher.pattern());
86 VariableValue value = fireUpdateAndReset(matcher, "", variable);
88 fireUpdate(LcnChannelGroup.RVARLOCK, variable.getNumber(), OnOffType.from(value.isRegulatorLocked()));
92 public Collection<Pattern> getPckStatusMessagePatterns() {
93 return List.of(PATTERN, LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013);