/** {@inheritDoc} */ @Override protected void internalReceiveCommand(String itemName, Command command) { try { logger.debug("internalReceiveCommand: itemName '{}', command '{}'", itemName, command); // Lookup the MiOS Unit name and property for this item String unitName = getMiosUnitName(itemName); MiosUnitConnector connector = getMiosConnector(unitName); if (connector == null) { logger.warn( "Received command ({}) for item '{}' but no connector found for MiOS Unit '{}', ignoring", new Object[] {command.toString(), itemName, unitName}); return; } if (!connector.isConnected()) { logger.warn( "Received command ({}) for item '{}' but the connection to the MiOS Unit '{}' is down, ignoring", new Object[] {command.toString(), itemName, unitName}); return; } for (BindingProvider provider : providers) { if (provider instanceof MiosBindingProvider) { MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider; MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName); if (config != null) { ItemRegistry reg = miosProvider.getItemRegistry(); if (reg != null) { Item item = reg.getItem(config.getItemName()); State state = item.getState(); connector.invokeCommand(config, command, state); } else { logger.warn( "internalReceiveCommand: Missing ItemRegistry for item '{}' command '{}'", itemName, command); } } else { logger.trace( "internalReceiveCommand: Missing BindingConfig for item '{}' command '{}'", itemName, command); } } } } catch (Exception e) { logger.error("Error handling command", e); } }
/** * 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 = (ItemRegistry) ScriptActivator.itemRegistryTracker.getService(); EventPublisher publisher = (EventPublisher) ScriptActivator.eventPublisherTracker.getService(); if (publisher != null && registry != null) { try { Item item = registry.getItem(itemName); Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString); publisher.sendCommand(itemName, command); } catch (ItemNotFoundException e) { logger.warn("Item '" + itemName + "' does not exist."); } } 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 */ public static Object postUpdate(String itemName, String stateString) { ItemRegistry registry = (ItemRegistry) ScriptActivator.itemRegistryTracker.getService(); EventPublisher publisher = (EventPublisher) ScriptActivator.eventPublisherTracker.getService(); if (publisher != null && registry != null) { try { Item item = registry.getItem(itemName); State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString); publisher.postUpdate(itemName, state); } catch (ItemNotFoundException e) { logger.warn("Item '" + itemName + "' does not exist."); } } return null; }
public List<ItemStateData> getItemStates() throws ServiceException { List<ItemStateData> itemStates = new ArrayList<ItemStateData>(); ItemRegistry itemRegistry = getItemRegistry(); for (Item item : itemRegistry.getItems()) { logger.debug("Item: " + item.getName() + " " + item.getState()); StateTransformable state = getState(item); ItemStateData itemState = new ItemStateData(System.currentTimeMillis(), item.getName(), state); itemStates.add(itemState); } return itemStates; }
public ItemStateData getItemState(String itemId) throws ServiceException { ItemRegistry itemRegistry = getItemRegistry(); ItemStateData itemState = null; try { Item item = itemRegistry.getItem(itemId); StateTransformable state = getState(item); itemState = new ItemStateData(System.currentTimeMillis(), itemId, state); } catch (ItemNotFoundException ex) { logger.info(itemId + " not found", ex); } return itemState; }
@Override public void complete_ItemName( EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { super.complete_ItemName(model, ruleCall, context, acceptor); ItemRegistry itemRegistry = RuleModelUIActivator.itemRegistryTracker.getService(); for (Item item : itemRegistry.getItems()) { if (item.getName().startsWith(context.getPrefix())) { acceptor.accept(createCompletionProposal(item.getName(), context)); } } }
public void activate() { triggerManager = new RuleTriggerManager(); if (!isEnabled()) { logger.info("Rule engine is disabled."); return; } logger.debug("Started rule engine"); // read all rule files Iterable<String> ruleModelNames = modelRepository.getAllModelNamesOfType("rules"); ArrayList<String> clonedList = Lists.newArrayList(ruleModelNames); for (String ruleModelName : clonedList) { EObject model = modelRepository.getModel(ruleModelName); if (model instanceof RuleModel) { RuleModel ruleModel = (RuleModel) model; triggerManager.addRuleModel(ruleModel); } } // register us on all items which are already available in the registry for (Item item : itemRegistry.getItems()) { internalItemAdded(item); } runStartupRules(); }
/** {@inheritDoc} */ public void allItemsChanged(Collection<String> oldItemNames) { // add the current items again Collection<Item> items = itemRegistry.getItems(); for (Item item : items) { internalItemAdded(item); } runStartupRules(); }
public void receiveCommand(String itemName, Command command) { if (triggerManager != null && itemRegistry != null) { try { RuleEvaluationContext context; Item item = itemRegistry.getItem(itemName); Iterable<Rule> rules = triggerManager.getRules(COMMAND, item, command); for (Rule rule : rules) { context = new RuleEvaluationContext(); context.newValue(QualifiedName.create(RuleContextHelper.VAR_RECEIVED_COMMAND), command); executeRule(rule, context); } } catch (ItemNotFoundException e) { // ignore commands for non-existent items } } }
public void unsetItemRegistry(ItemRegistry itemRegistry) { itemRegistry.removeItemRegistryChangeListener(this); this.itemRegistry = null; }
public void setItemRegistry(ItemRegistry itemRegistry) { this.itemRegistry = itemRegistry; itemRegistry.addItemRegistryChangeListener(this); }
private void internalPropertyUpdate(String property, State value, boolean incremental) throws Exception { int bound = 0; if (value == null) { logger.trace("internalPropertyUpdate: Value is null for Property '{}', ignored.", property); return; } for (BindingProvider provider : providers) { if (provider instanceof MiosBindingProvider) { MiosBindingProviderImpl miosProvider = (MiosBindingProviderImpl) provider; for (String itemName : miosProvider.getItemNamesForProperty(property)) { MiosBindingConfig config = miosProvider.getMiosBindingConfig(itemName); if (config != null) { // Transform whatever value we have, based upon the // Transformation Service specified in the Binding // Config. State newValue = config.transformIn(value); if (newValue != value) { logger.trace( "internalPropertyUpdate: transformation performed, from '{}' to '{}'", value, newValue); } // // Set the value only if: // * we're running Incrementally OR; // * the CURRENT value is UNDEFINED OR; // * the CURRENT value is different from the NEW value // // This is to handle a case where the MiOS Unit // "restarts" and floods us with a bunch of the same // data. In this case, we don't want to flood the Items, // since it may re-trigger a bunch of Rules in an // unnecessary manner. // if (incremental) { logger.debug( "internalPropertyUpdate: BOUND (Incr) Updating '{} {mios=\"{}\"}' to '{}'", itemName, property, newValue); eventPublisher.postUpdate(itemName, newValue); } else { ItemRegistry reg = miosProvider.getItemRegistry(); State oldValue = reg.getItem(itemName).getState(); if ((oldValue == null && newValue != null) || (UnDefType.UNDEF.equals(oldValue) && !UnDefType.UNDEF.equals(newValue)) || !oldValue.equals(newValue)) { logger.debug( "internalPropertyUpdate: BOUND (Full) Updating '{} {mios=\"{}\"}' to '{}', was '{}'", new Object[] {itemName, property, newValue, oldValue}); eventPublisher.postUpdate(itemName, newValue); } else { logger.trace( "internalPropertyUpdate: BOUND (Full) Ignoring '{} {mios=\"{}\"}' to '{}', was '{}'", new Object[] {itemName, property, newValue, oldValue}); } } bound++; } else { logger.trace( "internalPropertyUpdate: Found null BindingConfig for item '{}' property '{}'", itemName, property); } } } } if (bound == 0) { logger.trace("internalPropertyUpdate: NOT BOUND {mios=\"{}\"}, value={}", property, value); } else { logger.trace( "internalPropertyUpdate: BOUND {mios=\"{}\"}, value={}, bound {} time(s)", new Object[] {property, value, bound}); } }