]> git.basschouten.com Git - openhab-addons.git/blob
5241f77ebb61795fa18a1f9c46eb8f0465ec2bbc
[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.Arrays;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import java.util.stream.IntStream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.lcn.internal.LcnBindingConstants;
24 import org.openhab.binding.lcn.internal.LcnModuleHandler;
25 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
26 import org.openhab.binding.lcn.internal.common.LcnDefs;
27 import org.openhab.binding.lcn.internal.common.LcnException;
28 import org.openhab.binding.lcn.internal.common.PckGenerator;
29 import org.openhab.binding.lcn.internal.common.Variable;
30 import org.openhab.binding.lcn.internal.connection.ModInfo;
31 import org.openhab.core.library.types.DecimalType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Handles Commands and State changes of thresholds of an LCN module.
37  *
38  * @author Fabian Wolter - Initial contribution
39  */
40 @NonNullByDefault
41 public class LcnModuleThresholdSubHandler extends AbstractLcnModuleVariableSubHandler {
42     private final Logger logger = LoggerFactory.getLogger(LcnModuleThresholdSubHandler.class);
43     private static final Pattern PATTERN = Pattern
44             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.T(?<registerId>\\d)(?<thresholdId>\\d)(?<value>\\d+)");
45     private static final Pattern PATTERN_BEFORE_2013 = Pattern.compile(LcnBindingConstants.ADDRESS_REGEX
46             + "\\.S1(?<value0>\\d{5})(?<value1>\\d{5})(?<value2>\\d{5})(?<value3>\\d{5})(?<value4>\\d{5})(?<hyst>\\d{5})");
47
48     public LcnModuleThresholdSubHandler(LcnModuleHandler handler, ModInfo info) {
49         super(handler, info);
50     }
51
52     @Override
53     public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
54             throws LcnException {
55         Variable variable = getVariable(channelGroup, number);
56         try {
57             int relativeChange = getRelativeChange(command, variable);
58             handler.sendPck(PckGenerator.setThresholdRelative(variable, LcnDefs.RelVarRef.CURRENT, relativeChange,
59                     info.hasExtendedMeasurementProcessing()));
60
61             // request new value, if the module doesn't send it on itself
62             if (info.getFirmwareVersion().map(v -> variable.shouldPollStatusAfterCommand(v)).orElse(true)) {
63                 info.refreshVariable(variable);
64             }
65         } catch (LcnException e) {
66             // current value unknown for some reason, refresh it in case we come again here
67             info.refreshVariable(variable);
68             throw e;
69         }
70     }
71
72     @Override
73     public void handleStatusMessage(Matcher matcher) {
74         IntStream stream;
75         Optional<String> groupSuffix;
76         int registerNumber;
77         if (matcher.pattern() == PATTERN) {
78             int thresholdId = Integer.parseInt(matcher.group("thresholdId")) - 1;
79             registerNumber = Integer.parseInt(matcher.group("registerId")) - 1;
80             stream = IntStream.rangeClosed(thresholdId, thresholdId);
81             groupSuffix = Optional.of("");
82         } else if (matcher.pattern() == PATTERN_BEFORE_2013) {
83             stream = IntStream.range(0, LcnDefs.THRESHOLD_COUNT_BEFORE_2013);
84             groupSuffix = Optional.empty();
85             registerNumber = 0;
86         } else {
87             logger.warn("Unexpected pattern: {}", matcher.pattern());
88             return;
89         }
90
91         stream.forEach(i -> {
92             try {
93                 fireUpdateAndReset(matcher, groupSuffix.orElse(String.valueOf(i)),
94                         Variable.thrsIdToVar(registerNumber, i));
95             } catch (LcnException e) {
96                 logger.warn("Parse error: {}", e.getMessage());
97             }
98         });
99     }
100
101     @Override
102     public Collection<Pattern> getPckStatusMessagePatterns() {
103         return Arrays.asList(PATTERN, PATTERN_BEFORE_2013);
104     }
105 }