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.regex.Matcher;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.lcn.internal.LcnModuleHandler;
20 import org.openhab.binding.lcn.internal.common.DimmerOutputCommand;
21 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
22 import org.openhab.binding.lcn.internal.common.LcnDefs;
23 import org.openhab.binding.lcn.internal.common.LcnDefs.RelayStateModifier;
24 import org.openhab.binding.lcn.internal.common.LcnException;
25 import org.openhab.binding.lcn.internal.common.Variable;
26 import org.openhab.binding.lcn.internal.common.VariableValue;
27 import org.openhab.binding.lcn.internal.connection.ModInfo;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.HSBType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StopMoveType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.library.types.UpDownType;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.State;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Base class for LCN module Thing sub handlers.
43 * @author Fabian Wolter - Initial contribution
46 public abstract class AbstractLcnModuleSubHandler implements ILcnModuleSubHandler {
47 private final Logger logger = LoggerFactory.getLogger(AbstractLcnModuleSubHandler.class);
48 protected final LcnModuleHandler handler;
49 protected final ModInfo info;
51 public AbstractLcnModuleSubHandler(LcnModuleHandler handler, ModInfo info) {
52 this.handler = handler;
57 public void handleRefresh(String groupId) {
58 // can be overwritten by subclasses.
62 public void handleCommandOnOff(OnOffType command, LcnChannelGroup channelGroup, int number) throws LcnException {
63 unsupportedCommand(command);
67 public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, int number)
69 unsupportedCommand(command);
73 public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, String idWithoutGroup)
75 unsupportedCommand(command);
79 public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
81 unsupportedCommand(command);
85 public void handleCommandDimmerOutput(DimmerOutputCommand command, int number) throws LcnException {
86 unsupportedCommand(command);
90 public void handleCommandString(StringType command, int number) throws LcnException {
91 unsupportedCommand(command);
95 public void handleCommandUpDown(UpDownType command, LcnChannelGroup channelGroup, int number, boolean invertUpDown)
97 unsupportedCommand(command);
101 public void handleCommandStopMove(StopMoveType command, LcnChannelGroup channelGroup, int number)
102 throws LcnException {
103 unsupportedCommand(command);
107 public void handleCommandHsb(HSBType command, String groupId) throws LcnException {
108 unsupportedCommand(command);
111 private void unsupportedCommand(Command command) {
112 logger.warn("Unsupported command: {}: {}", getClass().getSimpleName(), command.getClass().getSimpleName());
116 * Tries to parses the given PCK message. Fails silently to let another sub handler give the chance to process the
119 * @param pck the message to process
120 * @return true, if the message could be processed successfully
122 public void tryParse(String pck) {
123 getPckStatusMessagePatterns().stream() //
124 .map(p -> p.matcher(pck)) //
125 .filter(Matcher::matches) //
126 .filter(m -> handler.isMyAddress(m.group("segId"), m.group("modId"))) //
127 .forEach(matcher -> {
129 handleStatusMessage(matcher);
130 } catch (LcnException e) {
131 logger.warn("Parse error: {}", e.getMessage());
137 * Creates a RelayStateModifier array with all elements set to NOCHANGE.
139 * @return the created array
141 protected RelayStateModifier[] createRelayStateModifierArray() {
142 RelayStateModifier[] ret = new LcnDefs.RelayStateModifier[LcnChannelGroup.RELAY.getCount()];
143 Arrays.fill(ret, LcnDefs.RelayStateModifier.NOCHANGE);
148 * Updates the state of the LCN module.
150 * @param type the channel type which shall be updated
151 * @param number the Channel's number within the channel type, zero-based
152 * @param state the new state
154 protected void fireUpdate(LcnChannelGroup type, int number, State state) {
155 handler.updateChannel(type, (number + 1) + "", state);
159 * Fires the current state of a Variable to openHAB. Resets running value request logic.
161 * @param matcher the pre-matched matcher
162 * @param channelId the Channel's ID to update
163 * @param variable the Variable to update
164 * @return the new variable's value
166 protected VariableValue fireUpdateAndReset(Matcher matcher, String channelId, Variable variable) {
167 VariableValue value = new VariableValue(Long.parseLong(matcher.group("value" + channelId)));
169 info.updateVariableValue(variable, value);
170 info.onVariableResponseReceived(variable);
172 fireUpdate(variable.getChannelType(), variable.getThresholdNumber().orElse(variable.getNumber()),
173 value.getState(variable));