Exemplo n.º 1
0
 /** Visualization for {@link StringType} and {@link DecimalType}. {@inheritDoc} */
 @Override
 public boolean visualizationVarStatus(
     ModStatusVar pchkInput, Command cmd, Item item, EventPublisher eventPublisher) {
   // We are actually not meant to visualize anything.
   // But (just in case) someone is really lazy in doing the item-definitions, we try to be helpful
   // by showing the
   // current value.
   if (pchkInput.getLogicalSourceAddr().equals(this.addr) && pchkInput.getVar() == this.var) {
     if (item.getAcceptedDataTypes().contains(StringType.class)) {
       String valueStr =
           pchkInput
               .getValue()
               .toVarUnitString(
                   this.unit,
                   LcnDefs.Var.isLockableRegulatorSource(this.var),
                   LcnDefs.Var.useLcnSpecialValues(this.var));
       eventPublisher.postUpdate(item.getName(), new StringType(valueStr));
       return true;
     } else if (item.getAcceptedDataTypes().contains(DecimalType.class)) {
       eventPublisher.postUpdate(
           item.getName(),
           new DecimalType(
               pchkInput
                   .getValue()
                   .toVarUnit(this.unit, LcnDefs.Var.isLockableRegulatorSource(this.var))));
       return true;
     }
   }
   return false;
 }
Exemplo n.º 2
0
 /**
  * Posts a status update for a specified item to the event bus. t
  *
  * @param item the item to send the status update for
  * @param state the new state of the item
  */
 public static Object postUpdate(Item item, State state) {
   EventPublisher publisher = (EventPublisher) ScriptActivator.eventPublisherTracker.getService();
   if (publisher != null && item != null) {
     publisher.postUpdate(item.getName(), state);
   }
   return null;
 }
Exemplo n.º 3
0
 /**
  * Sends a command for a specified item to the event bus.
  *
  * @param item the item to send the command to
  * @param command the command to send
  */
 public static Object sendCommand(Item item, Command command) {
   EventPublisher publisher = (EventPublisher) ScriptActivator.eventPublisherTracker.getService();
   if (publisher != null && item != null) {
     publisher.sendCommand(item.getName(), command);
   }
   return null;
 }
Exemplo n.º 4
0
  @Override
  public void handleXPLMessage(xPL_MessageI theMessage) {

    for (XplBindingProvider provider : providers) {
      List<String> matchingItems = provider.hasMessage(theMessage);
      for (String itemName : matchingItems) {
        XplBindingConfig config = provider.getConfig(itemName);
        if (config == null) {
          continue;
        }

        String current = theMessage.getNamedValue(config.NamedParameter);

        Item item = provider.getItem(itemName);
        if (item != null) {
          if (item instanceof SwitchItem) {
            OnOffType status =
                (current.equalsIgnoreCase("on")
                        || current.equalsIgnoreCase("true")
                        || current.equalsIgnoreCase("1")
                        || current.equalsIgnoreCase("open")
                        || current.equalsIgnoreCase("high"))
                    ? OnOffType.ON
                    : OnOffType.OFF;
            synchronized (item) {
              if (!item.getState().equals(status)) {
                eventPublisher.postUpdate(itemName, status);
                ((SwitchItem) item).setState(status);
              }
            }
          } else if (item instanceof NumberItem) {
            DecimalType value = new DecimalType(current);
            synchronized (item) {
              if (!item.getState().equals(value)) {
                eventPublisher.postUpdate(itemName, value);
                ((NumberItem) item).setState(value);
              }
            }
          }
          if (item instanceof StringItem) {
            StringType value = new StringType(current);
            synchronized (item) {
              if (!item.getState().equals(value)) {
                eventPublisher.postUpdate(itemName, value);
                ((StringItem) item).setState(value);
              }
            }
          }
        }
      }
    }
  }
Exemplo n.º 5
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.º 6
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;
 }
Exemplo n.º 7
0
 /** Visualization for {@link DecimalType} and {@link StringType}. {@inheritDoc} */
 @Override
 public boolean visualizationLedsAndLogicOpsStatus(
     ModStatusLedsAndLogicOps pchkInput, Command cmd, Item item, EventPublisher eventPublisher) {
   if (pchkInput.getLogicalSourceAddr().equals(this.addr)) {
     if (item.getAcceptedDataTypes().contains(DecimalType.class)) {
       int n;
       switch (pchkInput.getLedState(this.ledId)) {
         case OFF:
           n = 0;
           break;
         case ON:
           n = 1;
           break;
         case BLINK:
           n = 2;
           break;
         case FLICKER:
           n = 3;
           break;
         default:
           throw new Error();
       }
       eventPublisher.postUpdate(item.getName(), new DecimalType(n));
       return true;
     } else if (item.getAcceptedDataTypes().contains(StringType.class)) {
       String s;
       switch (pchkInput.getLedState(this.ledId)) {
         case OFF:
           s = this.stateTexts[0];
           break;
         case ON:
           s = this.stateTexts[1];
           break;
         case BLINK:
           s = this.stateTexts[2];
           break;
         case FLICKER:
           s = this.stateTexts[3];
           break;
         default:
           throw new Error();
       }
       eventPublisher.postUpdate(item.getName(), new StringType(s));
       return true;
     }
   }
   return false;
 }
Exemplo n.º 8
0
  /**
   * publish configured zibase item messages on openhab bus
   *
   * @param zbResponse
   */
  protected void publishEvents(ZbResponse zbResponse) {

    String zbResponseStr = zbResponse.getMessage();
    String id = this.extractIdFromZbResponse(zbResponseStr);
    logger.debug("Found event from ID " + id);

    if (id == null) {
      return;
    }

    // ...retreive all itemNames that use this id...
    Vector<String> listOfItemNames = ZibaseBinding.getBindingProvider().getItemNamesById(id);
    if (listOfItemNames == null) {
      return;
    }

    logger.debug("trying to publish events for " + id);

    // then post update for all items that use this id
    for (String itemName : listOfItemNames) {
      ZibaseBindingConfig config =
          ZibaseBinding.getBindingProvider().getItemConfigByUniqueId(itemName + "_" + id);
      logger.debug("Getting config for " + itemName + " (id = " + id + ") ");

      if (config != null) {
        org.openhab.core.types.State value =
            config.getOpenhabStateFromZibaseValue(zibase, zbResponseStr);
        logger.debug("publishing update for " + itemName + " : " + value);
        eventPubisher.postUpdate(itemName, value);
      }
    }
  }
Exemplo n.º 9
0
    /**
     * Processes a monitor event for a given binding type
     *
     * @param event the monitor event to process
     * @param bindingType the binding type of the items to process
     */
    private void handleEventType(MonitorEvent event, String bindingType) {
      for (FritzboxBindingProvider provider : providers) {
        for (String itemName : provider.getItemNamesForType(bindingType)) {
          Class<? extends Item> itemType = provider.getItemType(itemName);

          org.openhab.core.types.State state = null;
          if (event.eventType.equals("DISCONNECT")) {
            state = itemType.isAssignableFrom(SwitchItem.class) ? OnOffType.OFF : CallType.EMPTY;
          } else if (event.eventType.equals("CONNECT")) {
            if (bindingType.equals(FritzboxBindingProvider.TYPE_ACTIVE)) {
              state =
                  itemType.isAssignableFrom(SwitchItem.class)
                      ? OnOffType.ON
                      : new CallType(event.externalNo, event.line);
            } else {
              state = itemType.isAssignableFrom(SwitchItem.class) ? OnOffType.OFF : CallType.EMPTY;
            }
          } else if (event.eventType.equals("RING")
              && bindingType.equals(FritzboxBindingProvider.TYPE_INBOUND)) {
            state =
                itemType.isAssignableFrom(SwitchItem.class)
                    ? OnOffType.ON
                    : new CallType(event.externalNo, event.internalNo);
          } else if (event.eventType.equals("CALL")
              && bindingType.equals(FritzboxBindingProvider.TYPE_OUTBOUND)) {
            state =
                itemType.isAssignableFrom(SwitchItem.class)
                    ? OnOffType.ON
                    : new CallType(event.internalNo, event.externalNo);
          }
          if (state != null) {
            eventPublisher.postUpdate(itemName, state);
          }
        }
      }
    }
Exemplo n.º 10
0
  @Override
  public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.CTS:
      case SerialPortEvent.DSR:
      case SerialPortEvent.RI:
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;
      case SerialPortEvent.DATA_AVAILABLE:
        // we get here if data has been received
        StringBuilder sb = new StringBuilder();
        byte[] readBuffer = new byte[20];
        try {
          do {
            // read data from serial device
            while (inputStream.available() > 0) {
              int bytes = inputStream.read(readBuffer);
              sb.append(new String(readBuffer, 0, bytes));
            }
            try {
              // add wait states around reading the stream, so that interrupted transmissions are
              // merged
              Thread.sleep(100);
            } catch (InterruptedException e) {
              // ignore interruption
            }
          } while (inputStream.available() > 0);
          // sent data
          String result = sb.toString();

          // send data to the bus
          logger.debug("Received message '{}' on serial port {}", new String[] {result, port});

          if (eventPublisher != null) {
            if (configMap != null && !configMap.isEmpty()) {
              for (Entry<String, ItemType> entry : configMap.entrySet()) {

                // use pattern
                if (entry.getValue().pattern != null) {

                  if (transformationService == null) {
                    logger.error("No transformation service available!");

                  } else {
                    try {
                      String value =
                          transformationService.transform(entry.getValue().pattern, result);
                      if (entry.getValue().type.equals(NumberItem.class)) {
                        try {
                          eventPublisher.postUpdate(entry.getKey(), new DecimalType(value));
                        } catch (NumberFormatException e) {
                          logger.warn(
                              "Unable to convert regex result '{}' for item {} to number",
                              new String[] {result, entry.getKey()});
                        }
                      } else {
                        eventPublisher.postUpdate(entry.getKey(), new StringType(value));
                      }

                    } catch (TransformationException e) {
                      logger.error("Unable to transform!", e);
                    }
                  }

                } else if (entry.getValue().type == StringItem.class) {
                  if (entry.getValue().base64) {
                    result = Base64.encodeBase64String(result.getBytes());
                  }
                  eventPublisher.postUpdate(entry.getKey(), new StringType(result));

                } else if (entry.getValue().type == SwitchItem.class && result.trim().isEmpty()) {
                  eventPublisher.postUpdate(entry.getKey(), OnOffType.ON);
                  eventPublisher.postUpdate(entry.getKey(), OnOffType.OFF);
                }
              }
            }
          }

        } catch (IOException e) {
          logger.debug(
              "Error receiving data on serial port {}: {}", new String[] {port, e.getMessage()});
        }
        break;
    }
  }
Exemplo n.º 11
0
  public void handleEvent(ProtocolRead p_protocol_read) throws Exception {
    // the events on the bus are now received
    // map them to events on the openhab bus
    logger.debug(
        "Gateway ["
            + m_gateway_id
            + "], Bticino WHO ["
            + p_protocol_read.getProperty("who")
            + "], WHAT ["
            + p_protocol_read.getProperty("what")
            + "], WHERE ["
            + p_protocol_read.getProperty("where")
            + "]");

    // Get all the configs that are connected to this (who,where), multiple
    // possible
    List<BticinoBindingConfig> l_binding_configs =
        m_bticino_binding.getItemForBticinoBindingConfig(
            p_protocol_read.getProperty("who"), p_protocol_read.getProperty("where"));

    // log it when an event has occured that no item is bound to
    if (l_binding_configs.isEmpty()) {
      logger.debug(
          "Gateway ["
              + m_gateway_id
              + "], No Item found for bticino event, WHO ["
              + p_protocol_read.getProperty("who")
              + "], WHAT ["
              + p_protocol_read.getProperty("what")
              + "], WHERE ["
              + p_protocol_read.getProperty("where")
              + "]");
    }

    // every item associated with this who/where update the status
    for (BticinoBindingConfig l_binding_config : l_binding_configs) {
      // Get the Item out of the config
      Item l_item = l_binding_config.getItem();

      if (l_item instanceof SwitchItem) {
        // Lights
        if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("lighting")) {
          logger.debug(
              "Gateway ["
                  + m_gateway_id
                  + "], RECEIVED EVENT FOR SwitchItem ["
                  + l_item.getName()
                  + "], TRANSLATE TO OPENHAB BUS EVENT");

          if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Light ON")) {
            eventPublisher.postUpdate(l_item.getName(), OnOffType.ON);
          } else if (p_protocol_read
              .getProperty("messageDescription")
              .equalsIgnoreCase("Light OFF")) {
            eventPublisher.postUpdate(l_item.getName(), OnOffType.OFF);
          }
        }
        // CENs
        else if (p_protocol_read
            .getProperty("messageType")
            .equalsIgnoreCase("CEN Basic and Evolved")) {
          // Pushbutton virtual address must match
          if (l_binding_config.what.equalsIgnoreCase(p_protocol_read.getProperty("what"))) {
            logger.debug(
                "Gateway ["
                    + m_gateway_id
                    + "], RECEIVED EVENT FOR SwitchItem ["
                    + l_item.getName()
                    + "], TRANSLATE TO OPENHAB BUS EVENT");

            if (p_protocol_read
                .getProperty("messageDescription")
                .equalsIgnoreCase("Virtual pressure")) {
              // only returns when finished
              eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
            } else if (p_protocol_read
                .getProperty("messageDescription")
                .equalsIgnoreCase("Virtual release after short pressure")) {
              // only returns when finished
              eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
            } else if (p_protocol_read
                .getProperty("messageDescription")
                .equalsIgnoreCase("Virtual release after an extended pressure")) {
              // only returns when finished
              eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
            } else if (p_protocol_read
                .getProperty("messageDescription")
                .equalsIgnoreCase("Virtual extended pressure")) {
              // only returns when finished
              eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
            }
          }
        }
      } else if (l_item instanceof RollershutterItem) {
        logger.debug(
            "Gateway ["
                + m_gateway_id
                + "], RECEIVED EVENT FOR RollershutterItem ["
                + l_item.getName()
                + "], TRANSLATE TO OPENHAB BUS EVENT");

        if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("automation")) {

          if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Automation UP")) {
            eventPublisher.postUpdate(l_item.getName(), UpDownType.UP);
          } else if (p_protocol_read
              .getProperty("messageDescription")
              .equalsIgnoreCase("Automation DOWN")) {
            eventPublisher.postUpdate(l_item.getName(), UpDownType.DOWN);
          }
        }
      } else if (l_item instanceof NumberItem) {
        logger.debug(
            "Gateway ["
                + m_gateway_id
                + "], RECEIVED EVENT FOR NumberItem ["
                + l_item.getName()
                + "], TRANSLATE TO OPENHAB BUS EVENT");

        // THERMOREGULATION
        if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("thermoregulation")) {

          if (p_protocol_read
              .getProperty("messageDescription")
              .equalsIgnoreCase("Temperature value")) {
            eventPublisher.postUpdate(
                l_item.getName(), DecimalType.valueOf(p_protocol_read.getProperty("temperature")));
          }
        }
      }
    }
  }