-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add console command extension for serial inspection (#1172)
* add console command extension for serial inspection Signed-off-by: Markus Rathgeb <[email protected]>
- Loading branch information
1 parent
fd3732e
commit 492526d
Showing
2 changed files
with
118 additions
and
0 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
110 changes: 110 additions & 0 deletions
110
...va/org/eclipse/smarthome/io/transport/serial/internal/console/SerialCommandExtension.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,110 @@ | ||
/** | ||
* Copyright (c) 2010-2019 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.eclipse.smarthome.io.transport.serial.internal.console; | ||
|
||
import java.util.Arrays; | ||
import java.util.Deque; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.smarthome.io.console.Console; | ||
import org.eclipse.smarthome.io.console.extensions.AbstractConsoleCommandExtension; | ||
import org.eclipse.smarthome.io.console.extensions.ConsoleCommandExtension; | ||
import org.eclipse.smarthome.io.transport.serial.SerialPortIdentifier; | ||
import org.eclipse.smarthome.io.transport.serial.SerialPortManager; | ||
import org.eclipse.smarthome.io.transport.serial.internal.SerialPortRegistry; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* {@link SerialCommandExtension} provides console commands for serial ports. | ||
* | ||
* @author Markus Rathgeb - Initial contribution | ||
*/ | ||
@Component(service = ConsoleCommandExtension.class) | ||
public class SerialCommandExtension extends AbstractConsoleCommandExtension { | ||
|
||
private static final String CMD_SERIAL = "serial"; | ||
private static final String SUBCMD_IDENTIFIER_ALL = "identifiers"; | ||
private static final String SUBCMD_IDENTIFIER_NAME = "identifier"; | ||
private static final String SUBCMD_PORT_CREATORS = "creators"; | ||
|
||
private SerialPortManager serialPortManager; | ||
private SerialPortRegistry serialPortRegistry; | ||
|
||
@Activate | ||
public SerialCommandExtension(final @Reference SerialPortManager serialPortManager, | ||
final @Reference SerialPortRegistry serialPortRegistry) { | ||
super(CMD_SERIAL, "Access your serial port interfaces."); | ||
this.serialPortManager = serialPortManager; | ||
this.serialPortRegistry = serialPortRegistry; | ||
} | ||
|
||
@Override | ||
public void execute(String[] args, Console console) { | ||
final Deque<String> argList = new LinkedList<>(Arrays.asList(args)); | ||
if (argList.isEmpty()) { | ||
printUsage(console); | ||
return; | ||
} | ||
|
||
final String subCmd = argList.removeFirst(); | ||
switch (subCmd) { | ||
case SUBCMD_IDENTIFIER_ALL: | ||
serialPortManager.getIdentifiers().forEach(id -> { | ||
console.println(str(id)); | ||
}); | ||
return; | ||
case SUBCMD_IDENTIFIER_NAME: | ||
if (argList.isEmpty()) { | ||
console.println("Missing name"); | ||
return; | ||
} | ||
final String name = argList.removeFirst(); | ||
console.println(str(serialPortManager.getIdentifier(name))); | ||
return; | ||
case SUBCMD_PORT_CREATORS: | ||
serialPortRegistry.getPortCreators().forEach(provider -> { | ||
console.printf("%s, accepted protocols: %s, port identifiers: %s%n", provider.getClass(), | ||
provider.getAcceptedProtocols().collect(Collectors.toList()), | ||
provider.getSerialPortIdentifiers().map(SerialCommandExtension::str) | ||
.collect(Collectors.toList())); | ||
}); | ||
return; | ||
default: | ||
console.printf("Unknown sub command: %s%n", subCmd); | ||
return; | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getUsages() { | ||
return Arrays.asList(new String[] { // | ||
buildCommandUsage(SUBCMD_IDENTIFIER_ALL, "lists all identifiers"), // | ||
buildCommandUsage(SUBCMD_IDENTIFIER_NAME, "lists a specific identifier"), // | ||
buildCommandUsage(SUBCMD_PORT_CREATORS, "gets details about the port creators") // | ||
}); | ||
} | ||
|
||
private static String str(final @Nullable SerialPortIdentifier id) { | ||
if (id == null) { | ||
return "<null>"; | ||
} else { | ||
return String.format("[name: %s, current owner: %s]", id.getName(), id.getCurrentOwner()); | ||
} | ||
} | ||
|
||
} |
492526d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on openHAB Community. There might be relevant details there:
https://community.openhab.org/t/openhab-milestone-builds/50359/520