]> git.basschouten.com Git - openhab-addons.git/blob
b4e6750affe8bce3e5ed4d5657ab559198c5eca0
[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 = 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 REMOTE_CONTROL_PATTERN = Pattern.compile(LcnBindingConstants.ADDRESS_REGEX
39             + "\\.ZI(?<byte0>\\d{3})(?<byte1>\\d{3})(?<byte2>\\d{3})(?<key>\\d{3})(?<action>\\d{3})");
40
41     public LcnModuleCodeSubHandler(LcnModuleHandler handler, ModInfo info) {
42         super(handler, info);
43     }
44
45     @Override
46     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
47         // nothing
48     }
49
50     @Override
51     public void handleStatusMessage(Matcher matcher) {
52         String code;
53
54         if (matcher.pattern() == FINGERPRINT_PATTERN) {
55             code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0"), 16),
56                     Integer.parseInt(matcher.group("byte1"), 16), Integer.parseInt(matcher.group("byte2"), 16));
57         } else {
58             code = String.format("%02X%02X%02X", Integer.parseInt(matcher.group("byte0")),
59                     Integer.parseInt(matcher.group("byte1")), Integer.parseInt(matcher.group("byte2")));
60         }
61
62         if (matcher.pattern() == TRANSPONDER_PATTERN) {
63             handler.triggerChannel(LcnChannelGroup.CODE, "transponder", code);
64         } else if (matcher.pattern() == FINGERPRINT_PATTERN) {
65             handler.triggerChannel(LcnChannelGroup.CODE, "fingerprint", code);
66         } else if (matcher.pattern() == REMOTE_CONTROL_PATTERN) {
67             int keyNumber = Integer.parseInt(matcher.group("key"));
68             String keyLayer;
69
70             if (keyNumber > 30) {
71                 keyLayer = "D";
72                 keyNumber -= 30;
73             } else if (keyNumber > 20) {
74                 keyLayer = "C";
75                 keyNumber -= 20;
76             } else if (keyNumber > 10) {
77                 keyLayer = "B";
78                 keyNumber -= 10;
79             } else if (keyNumber > 0) {
80                 keyLayer = "A";
81             } else {
82                 return;
83             }
84
85             int action = Integer.parseInt(matcher.group("action"));
86
87             if (action > 10) {
88                 handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolbatterylow", code);
89                 action -= 10;
90             }
91
92             LcnDefs.SendKeyCommand actionType;
93             switch (action) {
94                 case 1:
95                     actionType = LcnDefs.SendKeyCommand.HIT;
96                     break;
97                 case 2:
98                     actionType = LcnDefs.SendKeyCommand.MAKE;
99                     break;
100                 case 3:
101                     actionType = LcnDefs.SendKeyCommand.BREAK;
102                     break;
103                 default:
104                     return;
105             }
106
107             handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolkey",
108                     keyLayer + keyNumber + ":" + actionType.name());
109
110             handler.triggerChannel(LcnChannelGroup.CODE, "remotecontrolcode",
111                     code + ":" + keyLayer + keyNumber + ":" + actionType.name());
112         }
113     }
114
115     @Override
116     public Collection<Pattern> getPckStatusMessagePatterns() {
117         return Arrays.asList(TRANSPONDER_PATTERN, FINGERPRINT_PATTERN, REMOTE_CONTROL_PATTERN);
118     }
119 }