Exemple #1
0
  protected String transformResponse(String transformation, String response) {
    String transformedResponse;

    try {
      String[] parts = splitTransformationConfig(transformation);
      String transformationType = parts[0];
      String transformationFunction = parts[1];

      TransformationService transformationService =
          TransformationHelper.getTransformationService(
              TCPActivator.getContext(), transformationType);
      if (transformationService != null) {
        transformedResponse = transformationService.transform(transformationFunction, response);
      } else {
        transformedResponse = response;
        logger.warn(
            "couldn't transform response because transformationService of type '{}' is unavailable",
            transformationType);
      }
    } catch (TransformationException te) {
      logger.error(
          "transformation throws exception [transformation="
              + transformation
              + ", response="
              + response
              + "]",
          te);

      // in case of an error we return the response without any
      // transformation
      transformedResponse = response;
    }

    logger.debug("transformed response is '{}'", transformedResponse);

    return transformedResponse;
  }
 /**
  * Resolves LCN commands (with mappings) to plain commands.
  *
  * @param lcnTarget the target or a mapping
  * @param openHABcmd the command send by openHAB
  * @return the resolved result (can be null)
  */
 private static String resolveMappings(String lcnTarget, String openHABcmd) {
   String result = null;
   Matcher matcher = PATTERN_MAPPING.matcher(lcnTarget);
   if (!matcher.matches()) {
     result = lcnTarget;
   } else {
     matcher.reset();
     matcher.find();
     String s1 = matcher.group(1);
     String s2 = matcher.group(2);
     TransformationService transformationService =
         TransformationHelper.getTransformationService(LcnBindingActivator.getContext(), s1);
     if (transformationService != null) {
       try {
         result = transformationService.transform(s2, openHABcmd);
       } catch (TransformationException e) {
         result = lcnTarget;
       }
     } else {
       result = lcnTarget;
     }
   }
   return result;
 }
Exemple #3
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;
    }
  }