Search in sources :

Example 31 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class AbstractWidgetRenderer method getStateAsNumber.

protected String getStateAsNumber(Widget w) {
    String itemName = w.getItem();
    if (itemName != null) {
        try {
            Item item = itemUIRegistry.getItem(itemName);
            State state = item.getState();
            if (item.getAcceptedDataTypes().contains(PercentType.class)) {
                state = item.getStateAs(PercentType.class);
            } else {
                state = item.getStateAs(DecimalType.class);
            }
            if (state != null) {
                return escapeURLPath(state.toString());
            } else {
                logger.debug("State '{}' of item '{}' is not a number!", item.getState(), itemName);
            }
        } catch (ItemNotFoundException e) {
            logger.error("Cannot retrieve item '{}' for widget {}", new Object[] { itemName, w.eClass().getInstanceTypeName() });
        }
    }
    return "NULL";
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 32 with Item

use of org.eclipse.smarthome.core.items.Item 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 33 with Item

use of org.eclipse.smarthome.core.items.Item 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 34 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class RuleEngineImpl method activate.

@Activate
public void activate() {
    injector = RulesStandaloneSetup.getInjector();
    triggerManager = injector.getInstance(RuleTriggerManager.class);
    if (!isEnabled()) {
        logger.info("Rule engine is disabled.");
        return;
    }
    logger.debug("Started rule engine");
    // read all rule files
    for (String ruleModelName : modelRepository.getAllModelNamesOfType("rules")) {
        EObject model = modelRepository.getModel(ruleModelName);
        if (model instanceof RuleModel) {
            RuleModel ruleModel = (RuleModel) model;
            triggerManager.addRuleModel(ruleModel);
        }
    }
    // register us as listeners
    itemRegistry.addRegistryChangeListener(this);
    modelRepository.addModelRepositoryChangeListener(this);
    // register us on all items which are already available in the registry
    for (Item item : itemRegistry.getItems()) {
        internalItemAdded(item);
    }
    scheduleStartupRules();
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) EObject(org.eclipse.emf.ecore.EObject) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel) Activate(org.osgi.service.component.annotations.Activate)

Example 35 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ScriptEngineOSGiTest method testDivideItemState_QuantityType.

@SuppressWarnings("null")
@Test
public void testDivideItemState_QuantityType() throws ScriptParsingException, ScriptExecutionException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_LENGTH);
    ((NumberItem) numberItem).setState(new QuantityType<>("1 m"));
    assertThat((QuantityType<?>) runScript("val length = NumberC.state as QuantityType; return length / 2 [cm];"), is(QuantityType.valueOf("50")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8