2 * Copyright (c) 2010-2022 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.Optional;
17 import java.util.regex.Matcher;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lcn.internal.LcnModuleHandler;
21 import org.openhab.binding.lcn.internal.common.DimmerOutputCommand;
22 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
23 import org.openhab.binding.lcn.internal.common.LcnDefs;
24 import org.openhab.binding.lcn.internal.common.LcnDefs.RelayStateModifier;
25 import org.openhab.binding.lcn.internal.common.LcnException;
26 import org.openhab.binding.lcn.internal.common.Variable;
27 import org.openhab.binding.lcn.internal.common.VariableValue;
28 import org.openhab.binding.lcn.internal.connection.ModInfo;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.HSBType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.library.types.StopMoveType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.library.types.UpDownType;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.State;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Base class for LCN module Thing sub handlers.
44 * @author Fabian Wolter - Initial contribution
47 public abstract class AbstractLcnModuleSubHandler implements ILcnModuleSubHandler {
48 private final Logger logger = LoggerFactory.getLogger(AbstractLcnModuleSubHandler.class);
49 protected final LcnModuleHandler handler;
50 protected final ModInfo info;
52 public AbstractLcnModuleSubHandler(LcnModuleHandler handler, ModInfo info) {
53 this.handler = handler;
58 public void handleRefresh(String groupId) {
59 // can be overwritten by subclasses.
63 public void handleCommandOnOff(OnOffType command, LcnChannelGroup channelGroup, int number) throws LcnException {
64 unsupportedCommand(command);
68 public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, int number)
70 unsupportedCommand(command);
74 public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, String idWithoutGroup)
76 unsupportedCommand(command);
80 public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
82 unsupportedCommand(command);
86 public void handleCommandDimmerOutput(DimmerOutputCommand command, int number) throws LcnException {
87 unsupportedCommand(command);
91 public void handleCommandString(StringType command, int number) throws LcnException {
92 unsupportedCommand(command);
96 public void handleCommandUpDown(UpDownType command, LcnChannelGroup channelGroup, int number, boolean invertUpDown)
98 unsupportedCommand(command);
102 public void handleCommandStopMove(StopMoveType command, LcnChannelGroup channelGroup, int number)
103 throws LcnException {
104 unsupportedCommand(command);
108 public void handleCommandHsb(HSBType command, String groupId) throws LcnException {
109 unsupportedCommand(command);
112 private void unsupportedCommand(Command command) {
113 logger.warn("Unsupported command: {}: {}", getClass().getSimpleName(), command.getClass().getSimpleName());
117 * Tries to parses the given PCK message. Fails silently to let another sub handler give the chance to process the
120 * @param pck the message to process
121 * @return true, if the message could be processed successfully
123 public void tryParse(String pck) {
124 Optional<Matcher> firstSuccessfulMatcher = getPckStatusMessagePatterns().stream().map(p -> p.matcher(pck))
125 .filter(Matcher::matches).filter(m -> handler.isMyAddress(m.group("segId"), m.group("modId")))
128 firstSuccessfulMatcher.ifPresent(matcher -> {
130 handleStatusMessage(matcher);
131 } catch (LcnException e) {
132 logger.warn("Parse error: {}", e.getMessage());
138 * Creates a RelayStateModifier array with all elements set to NOCHANGE.
140 * @return the created array
142 protected RelayStateModifier[] createRelayStateModifierArray() {
143 RelayStateModifier[] ret = new LcnDefs.RelayStateModifier[LcnChannelGroup.RELAY.getCount()];
144 Arrays.fill(ret, LcnDefs.RelayStateModifier.NOCHANGE);
149 * Updates the state of the LCN module.
151 * @param type the channel type which shall be updated
152 * @param number the Channel's number within the channel type, zero-based
153 * @param state the new state
155 protected void fireUpdate(LcnChannelGroup type, int number, State state) {
156 handler.updateChannel(type, (number + 1) + "", state);
160 * Fires the current state of a Variable to openHAB. Resets running value request logic.
162 * @param matcher the pre-matched matcher
163 * @param channelId the Channel's ID to update
164 * @param variable the Variable to update
165 * @return the new variable's value
167 protected VariableValue fireUpdateAndReset(Matcher matcher, String channelId, Variable variable) {
168 VariableValue value = new VariableValue(Long.parseLong(matcher.group("value" + channelId)));
170 info.updateVariableValue(variable, value);
171 info.onVariableResponseReceived(variable);
173 fireUpdate(variable.getChannelType(), variable.getThresholdNumber().orElse(variable.getNumber()),
174 value.getState(variable));