]> git.basschouten.com Git - openhab-addons.git/blob
deebb56ba3747938b053c9790670407e818ce0c1
[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.regex.Matcher;
17
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;
39
40 /**
41  * Base class for LCN module Thing sub handlers.
42  *
43  * @author Fabian Wolter - Initial contribution
44  */
45 @NonNullByDefault
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;
50
51     public AbstractLcnModuleSubHandler(LcnModuleHandler handler, ModInfo info) {
52         this.handler = handler;
53         this.info = info;
54     }
55
56     @Override
57     public void handleRefresh(String groupId) {
58         // can be overwritten by subclasses.
59     }
60
61     @Override
62     public void handleCommandOnOff(OnOffType command, LcnChannelGroup channelGroup, int number) throws LcnException {
63         unsupportedCommand(command);
64     }
65
66     @Override
67     public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, int number)
68             throws LcnException {
69         unsupportedCommand(command);
70     }
71
72     @Override
73     public void handleCommandPercent(PercentType command, LcnChannelGroup channelGroup, String idWithoutGroup)
74             throws LcnException {
75         unsupportedCommand(command);
76     }
77
78     @Override
79     public void handleCommandDecimal(DecimalType command, LcnChannelGroup channelGroup, int number)
80             throws LcnException {
81         unsupportedCommand(command);
82     }
83
84     @Override
85     public void handleCommandDimmerOutput(DimmerOutputCommand command, int number) throws LcnException {
86         unsupportedCommand(command);
87     }
88
89     @Override
90     public void handleCommandString(StringType command, int number) throws LcnException {
91         unsupportedCommand(command);
92     }
93
94     @Override
95     public void handleCommandUpDown(UpDownType command, LcnChannelGroup channelGroup, int number, boolean invertUpDown)
96             throws LcnException {
97         unsupportedCommand(command);
98     }
99
100     @Override
101     public void handleCommandStopMove(StopMoveType command, LcnChannelGroup channelGroup, int number)
102             throws LcnException {
103         unsupportedCommand(command);
104     }
105
106     @Override
107     public void handleCommandHsb(HSBType command, String groupId) throws LcnException {
108         unsupportedCommand(command);
109     }
110
111     private void unsupportedCommand(Command command) {
112         logger.warn("Unsupported command: {}: {}", getClass().getSimpleName(), command.getClass().getSimpleName());
113     }
114
115     /**
116      * Tries to parses the given PCK message. Fails silently to let another sub handler give the chance to process the
117      * message.
118      *
119      * @param pck the message to process
120      */
121     public void tryParse(String pck) {
122         getPckStatusMessagePatterns().stream() //
123                 .map(p -> p.matcher(pck)) //
124                 .filter(Matcher::matches) //
125                 .filter(m -> handler.isMyAddress(m.group("segId"), m.group("modId"))) //
126                 .forEach(matcher -> {
127                     try {
128                         handleStatusMessage(matcher);
129                     } catch (LcnException e) {
130                         logger.warn("Parse error: {}", e.getMessage());
131                     }
132                 });
133     }
134
135     /**
136      * Creates a RelayStateModifier array with all elements set to NOCHANGE.
137      *
138      * @return the created array
139      */
140     protected RelayStateModifier[] createRelayStateModifierArray() {
141         RelayStateModifier[] ret = new LcnDefs.RelayStateModifier[LcnChannelGroup.RELAY.getCount()];
142         Arrays.fill(ret, LcnDefs.RelayStateModifier.NOCHANGE);
143         return ret;
144     }
145
146     /**
147      * Updates the state of the LCN module.
148      *
149      * @param type the channel type which shall be updated
150      * @param number the Channel's number within the channel type, zero-based
151      * @param state the new state
152      */
153     protected void fireUpdate(LcnChannelGroup type, int number, State state) {
154         handler.updateChannel(type, (number + 1) + "", state);
155     }
156
157     /**
158      * Fires the current state of a Variable to openHAB. Resets running value request logic.
159      *
160      * @param matcher the pre-matched matcher
161      * @param channelId the Channel's ID to update
162      * @param variable the Variable to update
163      * @return the new variable's value
164      */
165     protected VariableValue fireUpdateAndReset(Matcher matcher, String channelId, Variable variable) {
166         VariableValue value = new VariableValue(Long.parseLong(matcher.group("value" + channelId)));
167
168         info.updateVariableValue(variable, value);
169         info.onVariableResponseReceived(variable);
170
171         fireUpdate(variable.getChannelType(), variable.getThresholdNumber().orElse(variable.getNumber()),
172                 value.getState(variable));
173         return value;
174     }
175 }