Search in sources :

Example 11 with Command

use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.

the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.

@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
    ThingUID thingUID = new ThingUID("binding:type:thing");
    Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
    ThingHandler thingHandler = new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void handleConfigurationUpdate(@NonNull Map<@NonNull String, @NonNull Object> configurationParameters) {
            changedParameters = configurationParameters;
        }
    };
    thing.setHandler(thingHandler);
    ThingProvider thingProvider = new ThingProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }

        @Override
        public Collection<@NonNull Thing> getAll() {
            return Collections.singleton(thing);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }
    };
    registerService(thingProvider);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", 1);
    thingRegistry.updateConfiguration(thingUID, parameters);
    assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) HashMap(java.util.HashMap) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) HashMap(java.util.HashMap) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with Command

use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.

the class ScriptBusEvent 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 Object sendCommand(String itemName, String commandString) {
    if (eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
            eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(ScriptBusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Command(org.eclipse.smarthome.core.types.Command) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 13 with Command

use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.

the class ItemCommandActionHandler method execute.

@Override
public Map<String, Object> execute(Map<String, Object> inputs) {
    String itemName = (String) module.getConfiguration().get(ITEM_NAME);
    String command = (String) module.getConfiguration().get(COMMAND);
    if (itemName != null && command != null && eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            Command commandObj = TypeParser.parseCommand(item.getAcceptedCommandTypes(), command);
            ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, commandObj);
            logger.debug("Executing ItemCommandAction on Item {} with Command {}", itemCommandEvent.getItemName(), itemCommandEvent.getItemCommand());
            eventPublisher.post(itemCommandEvent);
        } catch (ItemNotFoundException e) {
            logger.error("Item with name {} not found in ItemRegistry.", itemName);
        }
    } else {
        logger.error("Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}", itemName, command, eventPublisher, itemRegistry);
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) Command(org.eclipse.smarthome.core.types.Command) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 14 with Command

use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.

the class ItemEventFactory method createCommandEvent.

private Event createCommandEvent(String topic, String payload, String source) {
    String itemName = getItemName(topic);
    ItemEventPayloadBean bean = deserializePayload(payload, ItemEventPayloadBean.class);
    Command command = parseType(bean.getType(), bean.getValue(), Command.class);
    return new ItemCommandEvent(topic, payload, itemName, command, source);
}
Also used : Command(org.eclipse.smarthome.core.types.Command)

Example 15 with Command

use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.

the class RuleTriggerManager method internalGetCommandRules.

private void internalGetCommandRules(String name, Boolean isGroup, List<Class<? extends Command>> acceptedCommandTypes, Command command, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(COMMAND, mapName)) {
        for (final EventTrigger t : rule.getEventtrigger()) {
            String triggerCommandString = null;
            if ((!isGroup) && (t instanceof CommandEventTrigger)) {
                final CommandEventTrigger ct = (CommandEventTrigger) t;
                if (ct.getItem().equals(name)) {
                    triggerCommandString = ct.getCommand();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberCommandEventTrigger)) {
                final GroupMemberCommandEventTrigger gmct = (GroupMemberCommandEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerCommandString = gmct.getCommand();
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerCommandString != null) {
                final Command triggerCommand = TypeParser.parseCommand(acceptedCommandTypes, triggerCommandString);
                if (!command.equals(triggerCommand)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
Also used : GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) Command(org.eclipse.smarthome.core.types.Command) Rule(org.eclipse.smarthome.model.rule.rules.Rule) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) EventTrigger(org.eclipse.smarthome.model.rule.rules.EventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)

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