Search in sources :

Example 21 with Command

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

the class RuleTriggerManager method internalGetRules.

private Iterable<Rule> internalGetRules(TriggerTypes triggerType, Item item, Type oldType, Type newType) {
    List<Rule> result = new ArrayList<>();
    switch(triggerType) {
        case STARTUP:
            return systemStartupTriggeredRules;
        case SHUTDOWN:
            return systemShutdownTriggeredRules;
        case TIMER:
            return timerEventTriggeredRules;
        case UPDATE:
            if (newType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State state = (State) newType;
                internalGetUpdateRules(item.getName(), false, acceptedDataTypes, state, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetUpdateRules(groupName, true, acceptedDataTypes, state, result);
                }
            }
            break;
        case CHANGE:
            if (newType instanceof State && oldType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State newState = (State) newType;
                final State oldState = (State) oldType;
                internalGetChangeRules(item.getName(), false, acceptedDataTypes, newState, oldState, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetChangeRules(groupName, true, acceptedDataTypes, newState, oldState, result);
                }
            }
            break;
        case COMMAND:
            if (newType instanceof Command) {
                List<Class<? extends Command>> acceptedCommandTypes = item.getAcceptedCommandTypes();
                final Command command = (Command) newType;
                internalGetCommandRules(item.getName(), false, acceptedCommandTypes, command, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetCommandRules(groupName, true, acceptedCommandTypes, command, result);
                }
            }
            break;
        default:
            break;
    }
    return result;
}
Also used : Command(org.eclipse.smarthome.core.types.Command) State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Example 22 with Command

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

the class ItemCommandTriggerHandler method receive.

@Override
public void receive(Event event) {
    if (ruleEngineCallback != null) {
        logger.trace("Received Event: Source: {} Topic: {} Type: {}  Payload: {}", event.getSource(), event.getTopic(), event.getType(), event.getPayload());
        Map<String, Object> values = new HashMap<>();
        if (event instanceof ItemCommandEvent) {
            Command command = ((ItemCommandEvent) event).getItemCommand();
            if (this.command == null || this.command.equals(command.toFullString())) {
                values.put("command", command);
                values.put("event", event);
                ruleEngineCallback.triggered(this.module, values);
            }
        }
    }
}
Also used : ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) HashMap(java.util.HashMap) Command(org.eclipse.smarthome.core.types.Command)

Example 23 with Command

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

the class SendConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String itemName = args[0];
        try {
            Item item = this.itemRegistry.getItemByPattern(itemName);
            if (args.length > 1) {
                String commandName = args[1];
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                    console.println("Command has been sent successfully.");
                } else {
                    console.println("Error: Command '" + commandName + "' is not valid for item '" + itemName + "'");
                    console.println("Valid command types are:");
                    for (Class<? extends Command> acceptedType : item.getAcceptedCommandTypes()) {
                        console.print("  " + acceptedType.getSimpleName());
                        if (acceptedType.isEnum()) {
                            console.print(": ");
                            for (Object e : acceptedType.getEnumConstants()) {
                                console.print(e + " ");
                            }
                        }
                        console.println("");
                    }
                }
            } else {
                printUsage(console);
            }
        } catch (ItemNotFoundException e) {
            console.println("Error: Item '" + itemName + "' does not exist.");
        } catch (ItemNotUniqueException e) {
            console.print("Error: Multiple items match this pattern: ");
            for (Item item : e.getMatchingItems()) {
                console.print(item.getName() + " ");
            }
        }
    } else {
        printUsage(console);
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) Command(org.eclipse.smarthome.core.types.Command) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

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