]> git.basschouten.com Git - openhab-addons.git/blob
8f48646caadda32cbe548dfc61bfe11c991bbccf
[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.Collection;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.IntStream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.lcn.internal.LcnBindingConstants;
23 import org.openhab.binding.lcn.internal.LcnModuleHandler;
24 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
25 import org.openhab.binding.lcn.internal.common.LcnDefs;
26 import org.openhab.binding.lcn.internal.common.LcnDefs.LogicOpStatus;
27 import org.openhab.binding.lcn.internal.connection.ModInfo;
28 import org.openhab.core.library.types.StringType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Handles State changes of logic operations of an LCN module.
34  *
35  * @author Fabian Wolter - Initial contribution
36  */
37 @NonNullByDefault
38 public class LcnModuleLogicSubHandler extends AbstractLcnModuleSubHandler {
39     private final Logger logger = LoggerFactory.getLogger(LcnModuleLogicSubHandler.class);
40     private static final Pattern PATTERN_SINGLE_LOGIC = Pattern
41             .compile(LcnBindingConstants.ADDRESS_REGEX + "S(?<id>\\d{1})(?<logicOpState>\\d{3})");
42     private static final Pattern PATTERN_ALL = Pattern
43             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.TL(?<ledStates>[AEBF]{12})(?<logicOpStates>[NTV]{4})");
44
45     public LcnModuleLogicSubHandler(LcnModuleHandler handler, ModInfo info) {
46         super(handler, info);
47     }
48
49     @Override
50     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
51         info.refreshLedsAndLogic();
52     }
53
54     @Override
55     public void handleStatusMessage(Matcher matcher) {
56         info.onLedsAndLogicResponseReceived();
57
58         if (matcher.pattern() == PATTERN_ALL) {
59             IntStream.range(0, LcnChannelGroup.LED.getCount()).forEach(i -> {
60                 switch (matcher.group("ledStates").toUpperCase().charAt(i)) {
61                     case 'A':
62                         fireLed(i, LcnDefs.LedStatus.OFF);
63                         break;
64                     case 'E':
65                         fireLed(i, LcnDefs.LedStatus.ON);
66                         break;
67                     case 'B':
68                         fireLed(i, LcnDefs.LedStatus.BLINK);
69                         break;
70                     case 'F':
71                         fireLed(i, LcnDefs.LedStatus.FLICKER);
72                         break;
73                     default:
74                         logger.warn("Failed to parse LED state: {}", matcher.group("ledStates"));
75                 }
76             });
77             IntStream.range(0, LcnChannelGroup.LOGIC.getCount()).forEach(i -> {
78                 switch (matcher.group("logicOpStates").toUpperCase().charAt(i)) {
79                     case 'N':
80                         fireLogic(i, LcnDefs.LogicOpStatus.NOT);
81                         break;
82                     case 'T':
83                         fireLogic(i, LcnDefs.LogicOpStatus.OR);
84                         break;
85                     case 'V':
86                         fireLogic(i, LcnDefs.LogicOpStatus.AND);
87                         break;
88                     default:
89                         logger.warn("Failed to parse logic state: {}", matcher.group("logicOpStates"));
90                 }
91             });
92         } else if (matcher.pattern() == PATTERN_SINGLE_LOGIC) {
93             String rawState = matcher.group("logicOpState");
94
95             LogicOpStatus state;
96             switch (rawState) {
97                 case "000":
98                     state = LcnDefs.LogicOpStatus.NOT;
99                     break;
100                 case "025":
101                     state = LcnDefs.LogicOpStatus.OR;
102                     break;
103                 case "050":
104                     state = LcnDefs.LogicOpStatus.AND;
105                     break;
106                 default:
107                     logger.warn("Failed to parse logic state: {}", rawState);
108                     return;
109             }
110             fireLogic(Integer.parseInt(matcher.group("id")) - 1, state);
111         }
112     }
113
114     private void fireLed(int number, LcnDefs.LedStatus status) {
115         fireUpdate(LcnChannelGroup.LED, number, new StringType(status.toString()));
116     }
117
118     private void fireLogic(int number, LcnDefs.LogicOpStatus status) {
119         fireUpdate(LcnChannelGroup.LOGIC, number, new StringType(status.toString()));
120     }
121
122     @Override
123     public Collection<Pattern> getPckStatusMessagePatterns() {
124         return Arrays.asList(PATTERN_ALL, PATTERN_SINGLE_LOGIC);
125     }
126 }