/** Item processing for the LCN bindings. {@inheritDoc} */
 @Override
 public void processBindingConfiguration(String context, Item item, String bindingConfig)
     throws BindingConfigParseException {
   super.processBindingConfiguration(context, item, bindingConfig);
   Matcher matcher = PATTERN_BINDING_GENERAL.matcher(bindingConfig);
   if (!matcher.matches()) {
     throw new BindingConfigParseException(bindingConfig + "' contains no valid binding!");
   }
   matcher.reset();
   LcnBindingConfig bc = new LcnBindingConfig(item);
   while (matcher.find()) {
     String binding = matcher.group(1);
     if (binding != null && !binding.trim().isEmpty()) {
       String openHabCmd = null;
       String connId, lcnTarget;
       Matcher openHabMatcher = PATTERN_BINDING_WITH_OPENHAB.matcher(binding);
       Matcher pureMatcher = PATTERN_BINDING_PURE.matcher(binding);
       if (openHabMatcher.matches()) {
         openHabCmd = openHabMatcher.group(1);
         connId = openHabMatcher.group(2);
         lcnTarget = openHabMatcher.group(3);
       } else if (pureMatcher.matches()) {
         connId = pureMatcher.group(1);
         lcnTarget = pureMatcher.group(2);
       } else {
         throw new BindingConfigParseException(
             "Invalid binding configuration for " + binding + "!");
       }
       String lcnShort = resolveMappings(lcnTarget, openHabCmd);
       if (lcnShort == null || lcnShort.equals(openHabCmd)) {
         lcnShort = lcnTarget;
       }
       Command cmd =
           openHabCmd == null
               ? TypeParser.parseCommand(new StringItem("").getAcceptedCommandTypes(), "")
               : openHabCmd.equals("%i")
                   ? new StringType("%i")
                   : TypeParser.parseCommand(item.getAcceptedCommandTypes(), openHabCmd);
       bc.add(new LcnBindingConfig.Mapping(cmd, connId, lcnShort));
     }
   }
   // Finished
   this.addBindingConfig(item, bc);
   for (LcnAddrMod addr : bc.getRelatedModules()) {
     HashSet<String> l = this.itemNamesByModulCache.get(addr);
     if (l == null) {
       l = new HashSet<String>();
       this.itemNamesByModulCache.put(addr, l);
     }
     l.add(item.getName());
   }
 }
  /**
   * Creates a {@link Command} out of the given <code>commandAsString</code> incorporating the
   * {@link TypeParser}.
   *
   * @param item
   * @param commandAsString
   * @return an appropriate Command (see {@link TypeParser} for more information
   * @throws BindingConfigParseException if the {@link TypeParser} couldn't create a command
   *     appropriately
   * @see {@link TypeParser}
   */
  private Command createCommandFromString(Item item, String commandAsString)
      throws BindingConfigParseException {

    Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandAsString);

    if (command == null) {
      throw new BindingConfigParseException(
          "couldn't create Command from '" + commandAsString + "' ");
    }

    return command;
  }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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;
 }