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