Search in sources :

Example 16 with Command

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

Example 17 with Command

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;
        }
    };
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) Command(org.eclipse.smarthome.core.types.Command) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) ResourceBundle(java.util.ResourceBundle)

Example 18 with Command

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());
}
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)

Example 19 with Command

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());
}
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)

Example 20 with Command

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
        }
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Command(org.eclipse.smarthome.core.types.Command) Rule(org.eclipse.smarthome.model.rule.rules.Rule) 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