]> git.basschouten.com Git - openhab-addons.git/blob
5e23182fe472a16a42dcbde5f4cdfd55acdb4b65
[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.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.connection.ModInfo;
30 import org.openhab.core.library.types.DecimalType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Handles Commands and State changes of variables of an LCN module.
36  *
37  * @author Fabian Wolter - Initial contribution
38  */
39 @NonNullByDefault
40 public class LcnModuleVariableSubHandler extends AbstractLcnModuleVariableSubHandler {
41     private final Logger logger = LoggerFactory.getLogger(LcnModuleVariableSubHandler.class);
42     private static final Pattern PATTERN = Pattern
43             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.A(?<id>\\d{3})(?<value>\\d+)");
44
45     public LcnModuleVariableSubHandler(LcnModuleHandler handler, ModInfo info) {
46         super(handler, info);
47     }
48
49     @Override
50     public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
51             throws LcnException {
52         Variable variable = getVariable(channelGroup, number);
53         try {
54             int relativeChange = getRelativeChange(command, variable);
55             handler.sendPck(PckGenerator.setVariableRelative(variable, LcnDefs.RelVarRef.CURRENT, relativeChange));
56
57             // request new value, if the module doesn't send it on itself
58             if (info.getFirmwareVersion().map(v -> variable.shouldPollStatusAfterCommand(v)).orElse(true)) {
59                 info.refreshVariable(variable);
60             }
61         } catch (LcnException e) {
62             // current value unknown for some reason, refresh it in case we come again here
63             info.refreshVariable(variable);
64             throw e;
65         }
66     }
67
68     @Override
69     @SuppressWarnings("PMD.CompareObjectsWithEquals")
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() == LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013) {
75             variable = info.getLastRequestedVarWithoutTypeInResponse();
76
77             if (variable == Variable.UNKNOWN || variable.getType() != Type.VARIABLE) {
78                 return;
79             }
80         } else {
81             logger.warn("Unexpected pattern: {}", matcher.pattern());
82             return;
83         }
84         fireUpdateAndReset(matcher, "", variable);
85     }
86
87     @Override
88     public Collection<Pattern> getPckStatusMessagePatterns() {
89         return List.of(PATTERN, LcnBindingConstants.MEASUREMENT_PATTERN_BEFORE_2013);
90     }
91 }