/** * 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 */ static public 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; }
/** * Posts a status update for a specified item to the event bus. * * @param itemName the name of the item to send the status update for * @param stateAsString the new state of the item */ static public Object postUpdate(String itemName, String stateString) { ItemRegistry registry = ScriptServiceUtil.getItemRegistry(); EventPublisher publisher = ScriptServiceUtil.getEventPublisher(); if (publisher != null && registry != null) { try { Item item = registry.getItem(itemName); State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString); if (state != null) { publisher.post(ItemEventFactory.createStateEvent(itemName, state)); } else { LoggerFactory.getLogger(BusEvent.class).warn( "Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName, getAcceptedDataTypeNames(item)); } } catch (ItemNotFoundException e) { LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName); } } return null; }