]> git.basschouten.com Git - openhab-addons.git/blob
02f796e2684007248f1be5fcf48dc0937f4c71e6
[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.Collection;
16 import java.util.Set;
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.LcnModuleHandler;
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.LcnException;
25 import org.openhab.binding.lcn.internal.connection.ModInfo;
26
27 /**
28  * Handles 'send key' commands sent to this PCK host.
29  *
30  * @author Fabian Wolter - Initial contribution
31  */
32 @NonNullByDefault
33 public class LcnModuleHostCommandSubHandler extends AbstractLcnModuleSubHandler {
34     private static final Pattern SEND_KEY_PATTERN = Pattern
35             .compile("\\+M(?<hostId>\\d{3})(?<segId>\\d{3})(?<modId>\\d{3})\\.STH(?<byte0>\\d{3})(?<byte1>\\d{3})");
36
37     public LcnModuleHostCommandSubHandler(LcnModuleHandler handler, ModInfo info) {
38         super(handler, info);
39     }
40
41     @Override
42     public void handleRefresh(LcnChannelGroup channelGroup, int number) {
43         // nothing
44     }
45
46     @Override
47     public void handleStatusMessage(Matcher matcher) throws LcnException {
48         int keyTableAndActionMask = Integer.parseInt(matcher.group("byte0"));
49         int keyNumberMask = Integer.parseInt(matcher.group("byte1"));
50
51         if ((keyTableAndActionMask & (1 << 6)) == 0) {
52             return;
53         }
54
55         // PCHK 3.22 supports only the old 'send key' command with key tables A-C
56         for (int keyTableNumber = 0; keyTableNumber < LcnDefs.KEY_TABLE_COUNT_UNTIL_0C030C0; keyTableNumber++) {
57             String keyTableName = LcnDefs.KeyTable.values()[keyTableNumber].name();
58
59             for (int keyNumber = 0; keyNumber < LcnDefs.KEY_COUNT; keyNumber++) {
60                 int actionRaw = (keyTableAndActionMask >> (keyTableNumber * 2)) & 3;
61
62                 if (actionRaw > LcnDefs.SendKeyCommand.DONTSEND.getId()
63                         && actionRaw <= LcnDefs.SendKeyCommand.BREAK.getId()
64                         && ((1 << keyNumber) & keyNumberMask) != 0) {
65                     String actionName = LcnDefs.SendKeyCommand.get(actionRaw).name();
66
67                     handler.triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys",
68                             keyTableName + (keyNumber + 1) + ":" + actionName);
69                 }
70             }
71         }
72     }
73
74     @Override
75     public Collection<Pattern> getPckStatusMessagePatterns() {
76         return Set.of(SEND_KEY_PATTERN);
77     }
78 }