mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Send source for console commands to items (#5079)
Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
+6
@@ -76,6 +76,12 @@ public class OSGiConsole implements Console {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getUser() {
|
||||
Object result = session.get("USER");
|
||||
return result != null ? result.toString() : null;
|
||||
}
|
||||
|
||||
public Session getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
+9
@@ -53,4 +53,13 @@ public interface Console {
|
||||
default String readLine(String prompt, final @Nullable Character mask) throws IOException {
|
||||
throw new UnsupportedOperationException("readLine not supported");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user name associated with the console, or null if no user is associated.
|
||||
*
|
||||
* @return the user name, or null if no user is associated
|
||||
*/
|
||||
default @Nullable String getUser() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -22,7 +22,6 @@ import org.openhab.core.io.console.Console;
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public abstract class AbstractConsoleCommandExtension implements ConsoleCommandExtension {
|
||||
|
||||
private final String cmd;
|
||||
private final String desc;
|
||||
|
||||
|
||||
-1
@@ -44,7 +44,6 @@ import org.osgi.service.component.annotations.Reference;
|
||||
@Component(service = ConsoleCommandExtension.class)
|
||||
@NonNullByDefault
|
||||
public class ItemConsoleCommandExtension extends AbstractConsoleCommandExtension {
|
||||
|
||||
private static final String SUBCMD_LIST = "list";
|
||||
private static final String SUBCMD_CLEAR = "clear";
|
||||
private static final String SUBCMD_REMOVE = "remove";
|
||||
|
||||
+4
-1
@@ -17,6 +17,7 @@ import java.util.Objects;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.events.AbstractEvent;
|
||||
import org.openhab.core.events.EventPublisher;
|
||||
import org.openhab.core.io.console.Console;
|
||||
import org.openhab.core.io.console.ConsoleCommandCompleter;
|
||||
@@ -44,6 +45,7 @@ import org.osgi.service.component.annotations.Reference;
|
||||
@Component(service = ConsoleCommandExtension.class)
|
||||
@NonNullByDefault
|
||||
public class SendConsoleCommandExtension extends AbstractConsoleCommandExtension {
|
||||
private static final String CONSOLE_SOURCE = "org.openhab.core.io.console";
|
||||
|
||||
private final ItemRegistry itemRegistry;
|
||||
private final EventPublisher eventPublisher;
|
||||
@@ -71,7 +73,8 @@ public class SendConsoleCommandExtension extends AbstractConsoleCommandExtension
|
||||
String commandName = args[1];
|
||||
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
|
||||
if (command != null) {
|
||||
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
|
||||
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command,
|
||||
AbstractEvent.buildSource(CONSOLE_SOURCE, console.getUser())));
|
||||
console.println("Command has been sent successfully.");
|
||||
} else {
|
||||
console.println(
|
||||
|
||||
+4
-1
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.events.AbstractEvent;
|
||||
import org.openhab.core.events.EventPublisher;
|
||||
import org.openhab.core.io.console.Console;
|
||||
import org.openhab.core.io.console.ConsoleCommandCompleter;
|
||||
@@ -43,6 +44,7 @@ import org.osgi.service.component.annotations.Reference;
|
||||
@Component(service = ConsoleCommandExtension.class)
|
||||
@NonNullByDefault
|
||||
public class UpdateConsoleCommandExtension extends AbstractConsoleCommandExtension {
|
||||
private static final String CONSOLE_SOURCE = "org.openhab.core.io.console";
|
||||
|
||||
private final ItemRegistry itemRegistry;
|
||||
private final EventPublisher eventPublisher;
|
||||
@@ -70,7 +72,8 @@ public class UpdateConsoleCommandExtension extends AbstractConsoleCommandExtensi
|
||||
String stateName = args[1];
|
||||
State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateName);
|
||||
if (state != null) {
|
||||
eventPublisher.post(ItemEventFactory.createStateEvent(item.getName(), state));
|
||||
eventPublisher.post(ItemEventFactory.createStateEvent(item.getName(), state,
|
||||
AbstractEvent.buildSource(CONSOLE_SOURCE, console.getUser())));
|
||||
console.println("Update has been sent successfully.");
|
||||
} else {
|
||||
console.println("Error: State '" + stateName + "' is not valid for item '" + itemName + "'");
|
||||
|
||||
Reference in New Issue
Block a user