Search in sources :

Example 6 with Command

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);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 7 with Command

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);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 8 with Command

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);
}
Also used : Command(org.eclipse.smarthome.core.types.Command) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 9 with Command

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);
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigDescriptionProvider(org.eclipse.smarthome.config.core.ConfigDescriptionProvider) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 10 with Command

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());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Command (org.eclipse.smarthome.core.types.Command)23 Item (org.eclipse.smarthome.core.items.Item)9 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)7 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)6 Thing (org.eclipse.smarthome.core.thing.Thing)6 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 Test (org.junit.Test)6 GroupItem (org.eclipse.smarthome.core.items.GroupItem)5 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)5 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)4 Semaphore (java.util.concurrent.Semaphore)3 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)3 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)3 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)3 State (org.eclipse.smarthome.core.types.State)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)2