]> git.basschouten.com Git - openhab-addons.git/blob
eaf715432234e14355c119dfa8974cd48b2657eb
[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 java.util.Arrays;
16 import java.util.Collection;
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.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.connection.ModInfo;
29 import org.openhab.core.library.types.DecimalType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Handles Commands and State changes of variables of an LCN module.
35  *
36  * @author Fabian Wolter - Initial contribution
37  */
38 @NonNullByDefault
39 public class LcnModuleVariableSubHandler extends AbstractLcnModuleVariableSubHandler {
40     private final Logger logger = LoggerFactory.getLogger(LcnModuleVariableSubHandler.class);
41     private static final Pattern PATTERN = Pattern
42             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.A(?<id>\\d{3})(?<value>\\d+)");
43     private static final Pattern PATTERN_LEGACY = Pattern
44             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.(?<value>\\d+)");
45
46     public LcnModuleVariableSubHandler(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         Variable variable = getVariable(channelGroup, number);
54         try {
55             int relativeChange = getRelativeChange(command, variable);
56             handler.sendPck(PckGenerator.setVariableRelative(variable, LcnDefs.RelVarRef.CURRENT, relativeChange));
57
58             // request new value, if the module doesn't send it on itself
59             if (variable.shouldPollStatusAfterCommand(info.getFirmwareVersion())) {
60                 info.refreshVariable(variable);
61             }
62         } catch (LcnException e) {
63             // current value unknown for some reason, refresh it in case we come again here
64             info.refreshVariable(variable);
65             throw e;
66         }
67     }
68
69     @Override
70     public void handleStatusMessage(Matcher matcher) throws LcnException {
71         Variable variable;
72         if (matcher.pattern() == PATTERN) {
73             variable = Variable.varIdToVar(Integer.parseInt(matcher.group("id")) - 1);
74         } else if (matcher.pattern() == PATTERN_LEGACY) {
75             variable = info.getLastRequestedVarWithoutTypeInResponse();
76             info.setLastRequestedVarWithoutTypeInResponse(Variable.UNKNOWN); // Reset
77         } else {
78             logger.warn("Unexpected pattern: {}", matcher.pattern());
79             return;
80         }
81         fireUpdateAndReset(matcher, "", variable);
82     }
83
84     @Override
85     public Collection<Pattern> getPckStatusMessagePatterns() {
86         return Arrays.asList(PATTERN, PATTERN_LEGACY);
87     }
88 }