2 * Copyright (c) 2010-2024 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.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;
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;
36 * Handles Commands and State changes of thresholds of an LCN module.
38 * @author Fabian Wolter - Initial contribution
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})");
48 public LcnModuleThresholdSubHandler(LcnModuleHandler handler, ModInfo info) {
53 public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
55 Variable variable = getVariable(channelGroup, number);
57 int relativeChange = getRelativeChange(command, variable);
58 handler.sendPck(PckGenerator.setThresholdRelative(variable, LcnDefs.RelVarRef.CURRENT, relativeChange,
59 info.hasExtendedMeasurementProcessing()));
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);
65 } catch (LcnException e) {
66 // current value unknown for some reason, refresh it in case we come again here
67 info.refreshVariable(variable);
73 public void handleStatusMessage(Matcher matcher) {
75 Optional<String> groupSuffix;
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();
87 logger.warn("Unexpected pattern: {}", matcher.pattern());
93 fireUpdateAndReset(matcher, groupSuffix.orElse(String.valueOf(i)),
94 Variable.thrsIdToVar(registerNumber, i));
95 } catch (LcnException e) {
96 logger.warn("Parse error: {}", e.getMessage());
102 public Collection<Pattern> getPckStatusMessagePatterns() {
103 return Arrays.asList(PATTERN, PATTERN_BEFORE_2013);