]> git.basschouten.com Git - openhab-addons.git/blob
78f6c69398f42cad4c561da7c0d9491b02404ec8
[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
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.lcn.internal.LcnBindingConstants;
22 import org.openhab.binding.lcn.internal.LcnModuleHandler;
23 import org.openhab.binding.lcn.internal.common.LcnChannelGroup;
24 import org.openhab.binding.lcn.internal.common.LcnDefs;
25 import org.openhab.binding.lcn.internal.connection.ModInfo;
26
27 /**
28  * Handles State changes of transponders and remote controls of an LCN module.
29  *
30  * @author Fabian Wolter - Initial contribution
31  */
32 @NonNullByDefault
33 public class LcnModuleCodeSubHandler extends AbstractLcnModuleSubHandler {
34     private static final Pattern TRANSPONDER_PATTERN = Pattern
35             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.ZT(?<byte0>\\d{3})(?<byte1>\\d{3})(?<byte2>\\d{3})");
36     private static final Pattern FINGERPRINT_PATTERN_HEX = Pattern.compile(LcnBindingConstants.ADDRESS_REGEX
37             + "\\.ZF(?<byte0>[0-9A-Fa-f]{2})(?<byte1>[0-9A-Fa-f]{2})(?<byte2>[0-9A-Fa-f]{2})$");
38     private static final Pattern FINGERPRINT_PATTERN_DEC = Pattern
39             .compile(LcnBindingConstants.ADDRESS_REGEX + "\\.ZF(?<byte0>\\d{3})(?<byte1>\\d{3})(?<byte2>\\d{3})");
40     private static final Pattern REMOTE_CONTROL_PATTERN = Pattern.compile(LcnBindingConstants.ADDRESS_REGEX
41             + "\\.ZI(?<byte0>\\d{3})(?<byte1>\\d{3})(?<byte2>\\d{3})(?<key>\\d{3})(?<action>\\d{3})");
42
43     public LcnModuleCodeSubHandler(LcnModuleHandler handler, ModInfo info) {
44         super(handler, info);
45     }
46
47     @Override
48     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
49         // nothing
50     }
51
52     @Override
53     public void handleStatusMessage(Matcher matcher) {
54         String code;
55
56         int base = 10;
57         if (matcher.pattern() == FINGERPRINT_PATTERN_HEX) {
58             base = 16;
59         }
60
61         code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0"), base),
62                 Integer.parseInt(matcher.group("byte1"), base), Integer.parseInt(matcher.group("byte2"), base));
63
64         if (matcher.pattern() == TRANSPONDER_PATTERN) {
65             handler.triggerChannel(LcnChannelGroup.CODE, "transponder", code);
66         } else if (matcher.pattern() == FINGERPRINT_PATTERN_HEX || matcher.pattern() == FINGERPRINT_PATTERN_DEC) {
67             handler.triggerChannel(LcnChannelGroup.CODE, "fingerprint", code);
68         } else if (matcher.pattern() == REMOTE_CONTROL_PATTERN) {
69             int keyNumber = Integer.parseInt(matcher.group("key"));
70             String keyLayer;
71
72             if (keyNumber > 30) {
73                 keyLayer = "D";
74                 keyNumber -= 30;
75             } else if (keyNumber > 20) {
76                 keyLayer = "C";
77                 keyNumber -= 20;
78             } else if (keyNumber > 10) {
79                 keyLayer = "B";
80                 keyNumber -= 10;
81             } else if (keyNumber > 0) {
82                 keyLayer = "A";
83             } else {
84                 return;
85             }
86
87             int action = Integer.parseInt(matcher.group("action"));
88
89             if (action > 10) {
90                 handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolbatterylow", code);
91                 action -= 10;
92             }
93
94             LcnDefs.SendKeyCommand actionType;
95             switch (action) {
96                 case 1:
97                     actionType = LcnDefs.SendKeyCommand.HIT;
98                     break;
99                 case 2:
100                     actionType = LcnDefs.SendKeyCommand.MAKE;
101                     break;
102                 case 3:
103                     actionType = LcnDefs.SendKeyCommand.BREAK;
104                     break;
105                 default:
106                     return;
107             }
108
109             handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolkey",
110                     keyLayer + keyNumber + ":" + actionType.name());
111
112             handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolcode",
113                     code + ":" + keyLayer + keyNumber + ":" + actionType.name());
114         }
115     }
116
117     @Override
118     public Collection<Pattern> getPckStatusMessagePatterns() {
119         return Arrays.asList(TRANSPONDER_PATTERN, FINGERPRINT_PATTERN_HEX, FINGERPRINT_PATTERN_DEC,
120                 REMOTE_CONTROL_PATTERN);
121     }
122 }