Example #1
0
  /** {@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);
    }
  }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * 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 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;
  }
Example #5
0
  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
      }
    }
  }
Example #6
0
  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});
    }
  }