forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lcn] Add support for commands from LCN to openHAB (openhab#8284)
* [lcn] Add support for commands from LCN to openHAB * Add options tag for sendKeys Channel * Add ID to enum and change field name Signed-off-by: Fabian Wolter <[email protected]> Signed-off-by: Daan Meijer <[email protected]>
- Loading branch information
1 parent
adf4511
commit ee1059e
Showing
9 changed files
with
306 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...main/java/org/openhab/binding/lcn/internal/subhandler/LcnModuleHostCommandSubHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.lcn.internal.subhandler; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.lcn.internal.LcnModuleHandler; | ||
import org.openhab.binding.lcn.internal.common.LcnChannelGroup; | ||
import org.openhab.binding.lcn.internal.common.LcnDefs; | ||
import org.openhab.binding.lcn.internal.common.LcnException; | ||
import org.openhab.binding.lcn.internal.connection.ModInfo; | ||
|
||
/** | ||
* Handles 'send key' commands sent to this PCK host. | ||
* | ||
* @author Fabian Wolter - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class LcnModuleHostCommandSubHandler extends AbstractLcnModuleSubHandler { | ||
private static final Pattern SEND_KEY_PATTERN = Pattern | ||
.compile("\\+M(?<hostId>\\d{3})(?<segId>\\d{3})(?<modId>\\d{3})\\.STH(?<byte0>\\d{3})(?<byte1>\\d{3})"); | ||
|
||
public LcnModuleHostCommandSubHandler(LcnModuleHandler handler, ModInfo info) { | ||
super(handler, info); | ||
} | ||
|
||
@Override | ||
public void handleRefresh(LcnChannelGroup channelGroup, int number) { | ||
// nothing | ||
} | ||
|
||
@Override | ||
public void handleStatusMessage(Matcher matcher) throws LcnException { | ||
int keyTableAndActionMask = Integer.parseInt(matcher.group("byte0")); | ||
int keyNumberMask = Integer.parseInt(matcher.group("byte1")); | ||
|
||
if ((keyTableAndActionMask & (1 << 6)) == 0) { | ||
return; | ||
} | ||
|
||
// PCHK 3.22 supports only the old 'send key' command with key tables A-C | ||
for (int keyTableNumber = 0; keyTableNumber < LcnDefs.KEY_TABLE_COUNT_UNTIL_0C030C0; keyTableNumber++) { | ||
String keyTableName = LcnDefs.KeyTable.values()[keyTableNumber].name(); | ||
|
||
for (int keyNumber = 0; keyNumber < LcnDefs.KEY_COUNT; keyNumber++) { | ||
int actionRaw = (keyTableAndActionMask >> (keyTableNumber * 2)) & 3; | ||
|
||
if (actionRaw > LcnDefs.SendKeyCommand.DONTSEND.getId() | ||
&& actionRaw <= LcnDefs.SendKeyCommand.BREAK.getId() | ||
&& ((1 << keyNumber) & keyNumberMask) != 0) { | ||
String actionName = LcnDefs.SendKeyCommand.get(actionRaw).name(); | ||
|
||
handler.triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", | ||
keyTableName + (keyNumber + 1) + ":" + actionName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<Pattern> getPckStatusMessagePatterns() { | ||
return Collections.singleton(SEND_KEY_PATTERN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../java/org/openhab/binding/lcn/internal/subhandler/LcnModuleHostCommandSubHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.lcn.internal.subhandler; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openhab.binding.lcn.internal.common.LcnChannelGroup; | ||
|
||
/** | ||
* Test class. | ||
* | ||
* @author Fabian Wolter - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class LcnModuleHostCommandSubHandlerTest extends AbstractTestLcnModuleSubHandler { | ||
private @NonNullByDefault({}) LcnModuleHostCommandSubHandler subHandler; | ||
|
||
@Override | ||
@Before | ||
public void setUp() { | ||
super.setUp(); | ||
|
||
subHandler = new LcnModuleHostCommandSubHandler(handler, info); | ||
} | ||
|
||
@Test | ||
public void testA1Hit() { | ||
subHandler.tryParse("+M004000005.STH065001"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "A1:HIT"); | ||
} | ||
|
||
@Test | ||
public void testA1Make() { | ||
subHandler.tryParse("+M004000005.STH066001"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "A1:MAKE"); | ||
} | ||
|
||
@Test | ||
public void testC8Break() { | ||
subHandler.tryParse("+M004000005.STH112128"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "C8:BREAK"); | ||
} | ||
|
||
@Test | ||
public void testC1Hit() { | ||
subHandler.tryParse("+M004000005.STH080001"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "C1:HIT"); | ||
} | ||
|
||
@Test | ||
public void testMultiple() { | ||
subHandler.tryParse("+M004000005.STH121034"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "A2:HIT"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "A6:HIT"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "B2:MAKE"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "B6:MAKE"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "C2:BREAK"); | ||
verify(handler).triggerChannel(LcnChannelGroup.HOSTCOMMAND, "sendKeys", "C6:BREAK"); | ||
} | ||
} |