Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public String readLine(String prompt, final @Nullable Character mask) throws IOE
}
}

@Override
public @Nullable String getUser() {
Object result = session.get("USER");
return result != null ? result.toString() : null;
}

public Session getSession() {
return session;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ default void printf(String format, Object... args) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
@NonNullByDefault
public abstract class AbstractConsoleCommandExtension implements ConsoleCommandExtension {

private final String cmd;
private final String desc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
@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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;
Expand Down Expand Up @@ -44,6 +45,7 @@
@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;
Expand Down Expand Up @@ -71,7 +73,8 @@ public void execute(String[] args, Console console) {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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;
Expand Down Expand Up @@ -43,6 +44,7 @@
@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;
Expand Down Expand Up @@ -70,7 +72,8 @@ public void execute(String[] args, Console console) {
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 + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
@NonNullByDefault
public abstract class AbstractEvent implements Event {
public static final String ACTOR_SEPARATOR = "$";
public static final String DELEGATION_SEPARATOR = "=>";

private final String topic;

Expand Down Expand Up @@ -95,4 +97,45 @@ public boolean equals(@Nullable Object obj) {
}
return true;
}

/**
* Utility method to build a source string from a package and an optional actor.
*
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @param actor the actor
* @return the final source string
*/
public static String buildSource(String packageName, @Nullable String actor) {
if (actor == null || actor.isEmpty()) {
return packageName;
}
return packageName + ACTOR_SEPARATOR + actor;
}

/**
* Utility method to build a delegated source string from an original source and a package
*
* @param originalSource the original source (may be null)
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @return the final source string
*/
public static String buildDelegatedSource(@Nullable String originalSource, String packageName) {
if (originalSource == null || originalSource.isEmpty()) {
return packageName;
}
return originalSource + DELEGATION_SEPARATOR + packageName;
}

/**
* Utility method to build a delegated source string from an original source, a package and an optional actor.
*
* @param originalSource the original source (may be null)
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @param actor the actor
* @return the final source string
*/
public static String buildDelegatedSource(@Nullable String originalSource, String packageName,
@Nullable String actor) {
return buildDelegatedSource(originalSource, buildSource(packageName, actor));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2025 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.core.events;

import static org.junit.jupiter.api.Assertions.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;

/**
* {@link AbstractEventTest} tests the utility methods in {@link org.openhab.core.events.AbstractEvent}.
*
* @author Cody Cutrer - Initial contribution
*/
@NonNullByDefault
public class AbstractEventTest {
@Test
public void testBuildSource() throws Exception {
assertEquals(AbstractEvent.buildSource("org.openhab.core.thing", null), "org.openhab.core.thing");
assertEquals(AbstractEvent.buildSource("org.openhab.core.thing", "actor"), "org.openhab.core.thing$actor");
}

@Test
public void testBuildDelegatedSource() throws Exception {
assertEquals(AbstractEvent.buildDelegatedSource(null, "org.openhab.core.thing"), "org.openhab.core.thing");
assertEquals(AbstractEvent.buildDelegatedSource("org.openhab.binding.matter", "org.openhab.core.thing"),
"org.openhab.binding.matter=>org.openhab.core.thing");
assertEquals(AbstractEvent.buildDelegatedSource(null, "org.openhab.core.thing", "actor"),
"org.openhab.core.thing$actor");
assertEquals(
AbstractEvent.buildDelegatedSource("org.openhab.binding.matter", "org.openhab.core.thing", "actor"),
"org.openhab.binding.matter=>org.openhab.core.thing$actor");
assertEquals(AbstractEvent.buildDelegatedSource("org.openhab.binding.matter$originalActor",
"org.openhab.core.thing", "actor"),
"org.openhab.binding.matter$originalActor=>org.openhab.core.thing$actor");
}
}