Exemple #1
0
  /**
   * Main function to parse ASCII string received
   *
   * @return
   */
  @Override
  protected void parseBuffer(
      String itemName, Command aCommand, Direction theDirection, ByteBuffer byteBuffer) {

    String theUpdate = new String(byteBuffer.array());
    ProtocolBindingProvider provider = findFirstMatchingBindingProvider(itemName);

    List<Class<? extends State>> stateTypeList = provider.getAcceptedDataTypes(itemName, aCommand);

    String transformedResponse =
        transformResponse(provider.getProtocolCommand(itemName, aCommand), theUpdate);
    State newState = createStateFromString(stateTypeList, transformedResponse);

    if (newState != null) {
      eventPublisher.postUpdate(itemName, newState);
    } else {
      logger.warn(
          "Can not parse input " + theUpdate + " to match command {} on item {}  ",
          aCommand,
          itemName);
    }
  }
Exemple #2
0
  @Override
  protected boolean internalReceiveChanneledCommand(
      String itemName, Command command, Channel sChannel, String commandAsString) {

    ProtocolBindingProvider provider = findFirstMatchingBindingProvider(itemName);

    if (command != null) {

      String transformedMessage =
          transformResponse(provider.getProtocolCommand(itemName, command), commandAsString);
      String UDPCommandName = preAmble + transformedMessage + postAmble;

      ByteBuffer outputBuffer = ByteBuffer.allocate(UDPCommandName.getBytes().length);
      try {
        outputBuffer.put(UDPCommandName.getBytes("ASCII"));
      } catch (UnsupportedEncodingException e) {
        logger.warn("Exception while attempting an unsupported encoding scheme");
      }

      // send the buffer in an asynchronous way
      ByteBuffer result = null;
      try {
        result = writeBuffer(outputBuffer, sChannel, blocking, timeOut);
      } catch (Exception e) {
        logger.error(
            "An exception occurred while writing a buffer to a channel: {}", e.getMessage());
      }

      if (result != null && blocking) {
        logger.info(
            "Received {} from the remote end {}", new String(result.array()), sChannel.toString());
        String transformedResponse =
            transformResponse(
                provider.getProtocolCommand(itemName, command), new String(result.array()));

        // if the remote-end does not send a reply in response to the string we just sent, then the
        // abstract superclass will update
        // the openhab status of the item for us. If it does reply, then an additional update is
        // done via parseBuffer.
        // since this TCP binding does not know about the specific protocol, there might be two
        // state updates (the command, and if
        // the case, the reply from the remote-end)

        if (updateWithResponse) {

          List<Class<? extends State>> stateTypeList =
              provider.getAcceptedDataTypes(itemName, command);
          State newState = createStateFromString(stateTypeList, transformedResponse);

          if (newState != null) {
            eventPublisher.postUpdate(itemName, newState);
          } else {
            logger.warn(
                "Can not parse transformed input "
                    + transformedResponse
                    + " to match command {} on item {}  ",
                command,
                itemName);
          }

          return false;
        } else {
          return true;
        }
      } else {
        return true;
      }
    }
    return false;
  }