]> git.basschouten.com Git - openhab-addons.git/blob
919acaa3d25687959aec8c7394d5ff6fef7588f6
[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 java.util.Collection;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
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.LcnException;
25 import org.openhab.binding.lcn.internal.common.PckGenerator;
26 import org.openhab.binding.lcn.internal.common.Variable;
27 import org.openhab.binding.lcn.internal.common.Variable.Type;
28 import org.openhab.binding.lcn.internal.common.VariableValue;
29 import org.openhab.binding.lcn.internal.connection.ModInfo;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Handles Commands and State changes of regulator setpoints of an LCN module.
37  *
38  * @author Fabian Wolter - Initial contribution
39  */
40 @NonNullByDefault
41 public class LcnModuleRvarSetpointSubHandler extends AbstractLcnModuleVariableSubHandler {
42     private final Logger logger = LoggerFactory.getLogger(LcnModuleRvarSetpointSubHandler.class);
43     private static final Pattern PATTERN = Pattern
44             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.S(?<id>\\d)(?<value>\\d{1,5})");
45
46     public LcnModuleRvarSetpointSubHandler(LcnModuleHandler handler, ModInfo info) {
47         super(handler, info);
48     }
49
50     @Override
51     public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
52             throws LcnException {
53         handler.sendPck(PckGenerator.setSetpointAbsolute(number, command.intValue()));
54     }
55
56     @Override
57     @SuppressWarnings("PMD.CompareObjectsWithEquals")
58     public void handleStatusMessage(Matcher matcher) throws LcnException {
59         Variable variable;
60         if (matcher.pattern() == PATTERN) {
61             variable = Variable.setPointIdToVar(Integer.parseInt(matcher.group("id")) - 1);
62         } else if (matcher.pattern() == LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013) {
63             variable = info.getLastRequestedVarWithoutTypeInResponse();
64
65             if (variable.getType() != Type.REGULATOR) {
66                 return;
67             }
68         } else {
69             logger.warn("Unexpected pattern: {}", matcher.pattern());
70             return;
71         }
72         VariableValue value = fireUpdateAndReset(matcher, "", variable);
73
74         fireUpdate(LcnChannelGroup.RVARLOCK, variable.getNumber(), OnOffType.from(value.isRegulatorLocked()));
75     }
76
77     @Override
78     public Collection<Pattern> getPckStatusMessagePatterns() {
79         return List.of(PATTERN, LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013);
80     }
81 }