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