use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class CmdServlet method service.
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
for (Object key : req.getParameterMap().keySet()) {
String itemName = key.toString();
if (!itemName.startsWith("__")) {
// all additional webapp params start with "__" and should be ignored
String commandName = req.getParameter(itemName);
try {
Item item = itemRegistry.getItem(itemName);
// into real commands by the webapp.
if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
}
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
if (command != null) {
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
} else {
logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
}
} catch (ItemNotFoundException e) {
logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
}
}
}
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class CmdServlet method service.
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/plain");
for (Object key : req.getParameterMap().keySet()) {
String itemName = key.toString();
if (!itemName.startsWith("__")) {
// all additional webapp params start with "__" and should be ignored
String commandName = req.getParameter(itemName);
try {
Item item = itemRegistry.getItem(itemName);
// into real commands by the webapp.
if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
}
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
if (command != null) {
eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
} else {
logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
}
} catch (ItemNotFoundException e) {
logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
}
}
}
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class CommunicationManagerTest method testItemCommandEvent_typeDowncast.
@Test
public void testItemCommandEvent_typeDowncast() {
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(ChannelBuilder.create(STATE_CHANNEL_UID_2, "Dimmer").withKind(ChannelKind.STATE).build()).build();
thing.setHandler(mockHandler);
when(thingRegistry.get(eq(THING_UID))).thenReturn(thing);
manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
waitForAssert(() -> {
ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
verify(stateProfile).onCommandFromItem(commandCaptor.capture());
Command command = commandCaptor.getValue();
assertNotNull(command);
assertEquals(PercentType.class, command.getClass());
});
verifyNoMoreInteractions(stateProfile);
verifyNoMoreInteractions(triggerProfile);
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testInitializeOnlyIfInitializable.
@Test
public void testInitializeOnlyIfInitializable() throws Exception {
registerThingTypeProvider();
registerChannelTypeProvider();
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
});
ConfigDescriptionProvider mockConfigDescriptionProvider = mock(ConfigDescriptionProvider.class);
List<ConfigDescriptionParameter> parameters = //
Collections.singletonList(//
ConfigDescriptionParameterBuilder.create(CONFIG_PARAM_NAME, Type.TEXT).withRequired(true).build());
registerService(mockConfigDescriptionProvider, ConfigDescriptionProvider.class.getName());
// verify a missing mandatory thing config prevents it from getting initialized
when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_THING), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_THING, parameters));
assertThingStatus(Collections.emptyMap(), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
// verify a missing mandatory channel config prevents it from getting initialized
when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_CHANNEL), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_CHANNEL, parameters));
assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
// verify a satisfied config does not prevent it from getting initialized anymore
assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.singletonMap(CONFIG_PARAM_NAME, "value"), ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedInitializedThing.
@Test
public void testChildHandlerInitialized_replacedInitializedThing() {
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() {
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.ONLINE, thing.getStatus());
});
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(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
// ThingHandler.thingUpdated(...) must be called
assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
Aggregations