use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class BusEvent method sendCommand.
/**
* Sends a command for a specified item to the event bus.
*
* @param itemName the name of the item to send the command to
* @param commandString the command to send
*/
public static Object sendCommand(String itemName, String commandString) {
ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
if (publisher != null && registry != null) {
try {
Item item = registry.getItem(itemName);
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
if (command != null) {
publisher.post(ItemEventFactory.createCommandEvent(itemName, command));
} else {
LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a command type which item '{}' accepts: {}.", commandString, itemName, getAcceptedCommandNames(item));
}
} catch (ItemNotFoundException e) {
LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
}
}
return null;
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class AbstractRuleBasedInterpreter method itemRule.
/**
* Creates an item rule on base of a head and a tail expression, where the middle part of the new rule's expression
* will consist of an item
* name expression. Either the head expression or the tail expression should contain at least one {@link cmd}
* generated expression.
*
* @param headExpression The head expression.
* @param tailExpression The tail expression.
* @return The created rule.
*/
protected Rule itemRule(Object headExpression, Object tailExpression) {
Expression tail = exp(tailExpression);
Expression expression = tail == null ? seq(headExpression, name()) : seq(headExpression, name(tail), tail);
return new Rule(expression) {
@Override
public InterpretationResult interpretAST(ResourceBundle language, ASTNode node) {
String[] name = node.findValueAsStringArray(NAME);
ASTNode cmdNode = node.findNode(CMD);
Object tag = cmdNode.getTag();
Object value = cmdNode.getValue();
Command command;
if (tag instanceof Command) {
command = (Command) tag;
} else if (value instanceof Number) {
command = new DecimalType(((Number) value).longValue());
} else {
command = new StringType(cmdNode.getValueAsString());
}
if (name != null && command != null) {
try {
return new InterpretationResult(true, executeSingle(language, name, command));
} catch (InterpretationException ex) {
return new InterpretationResult(ex);
}
}
return InterpretationResult.SEMANTIC_ERROR;
}
};
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_modifiedUninitializedThing.
@Test
public void testChildHandlerInitialized_modifiedUninitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
try {
childHandlerInitializedSemaphore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
if (getBridge() == null) {
throw new RuntimeException("Fail because of missing bridge");
}
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
try {
thingUpdatedSemapthore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
managedThingProvider.add(bridge);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, bridge.getStatus());
});
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.add(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
thing.setBridgeUID(bridge.getUID());
managedThingProvider.update(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing.getStatus());
});
// childHandlerInitialized(...) must be called
waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
// thingUpdated(...) is not called
assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedUnitializedThing.
@Test
public void testChildHandlerInitialized_replacedUnitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
try {
childHandlerInitializedSemaphore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
if (getBridge() == null) {
throw new RuntimeException("Fail because of missing bridge");
}
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
try {
thingUpdatedSemapthore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
managedThingProvider.add(bridge);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, bridge.getStatus());
});
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.add(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
managedThingProvider.update(thing2);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing2.getStatus());
});
// childHandlerInitialized(...) must be called
waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
// thingUpdated(...) is not called
assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class RuleEngineImpl method receiveCommand.
private void receiveCommand(ItemCommandEvent commandEvent) {
if (!starting && triggerManager != null && itemRegistry != null) {
String itemName = commandEvent.getItemName();
Command command = commandEvent.getItemCommand();
try {
Item item = itemRegistry.getItem(itemName);
Iterable<Rule> rules = triggerManager.getRules(COMMAND, item, command);
executeRules(rules, item, command);
} catch (ItemNotFoundException e) {
// ignore commands for non-existent items
}
}
}
Aggregations