Esempio n. 1
0
  /**
   * Parses the given list of XML messages into a list of MQTT message objects.
   *
   * @param messages The list of XML messages
   * @param progress The progress updater to call with updated progress
   * @param current Current progress
   * @param max Maximum progress value
   * @return List of MQTT message objects (LoggedMqttMessage)
   * @throws MqttSpyException Thrown when cannot process the given list
   */
  public static List<LoggedMqttMessage> parseMessageLog(
      final List<String> messages,
      final ProgressUpdater progress,
      final long current,
      final long max)
      throws MqttSpyException {
    try {
      final MessageLogParser parser = new MessageLogParser();

      final List<LoggedMqttMessage> list = new ArrayList<LoggedMqttMessage>();
      long item = 0;

      for (final String message : messages) {
        if (progress != null) {
          item++;
          // Update every 1000
          if (item % 1000 == 0) {
            progress.update(current + item, max);
          }
        }

        try {
          list.add(parser.parse(message));
        } catch (XMLException e) {
          logger.error("Can't process message " + message, e);
        }
      }

      logger.info("Message log - parsed {} XML messages", list.size());

      return list;
    } catch (XMLException e) {
      throw new MqttSpyException("Can't parse the message log file", e);
    }
  }
Esempio n. 2
0
  /**
   * Turns the given list of LoggedMqttMessages into ReceivedMqttMessages.
   *
   * @param list List of logged messages to progress
   * @param progress The progress updater to call with updated progress
   * @param current Current progress
   * @param max Maximum progress value
   * @return List of MQTT message objects (ReceivedMqttMessage)
   */
  public static List<BaseMqttMessage> processMessageLog(
      final List<LoggedMqttMessage> list,
      final ProgressUpdater progress,
      final long current,
      final long max) {
    final List<BaseMqttMessage> mqttMessageList = new ArrayList<BaseMqttMessage>();
    long item = 0;

    // Process the messages
    for (final LoggedMqttMessage loggedMessage : list) {
      if (progress != null) {
        item++;
        if (item % 1000 == 0) {
          progress.update(current + item, max);
        }
      }

      final MqttMessage mqttMessage = new MqttMessage();

      if (logger.isTraceEnabled()) {
        logger.trace("Processing message with payload {}", loggedMessage.getValue());
      }

      if (Boolean.TRUE.equals(loggedMessage.isEncoded())) {
        mqttMessage.setPayload(Base64.decodeBase64(loggedMessage.getValue()));
      } else {
        mqttMessage.setPayload(ConversionUtils.stringToArray(loggedMessage.getValue()));
      }

      mqttMessage.setQos(loggedMessage.getQos() == null ? 0 : loggedMessage.getQos());
      mqttMessage.setRetained(
          loggedMessage.isRetained() == null ? false : loggedMessage.isRetained());

      mqttMessageList.add(
          new BaseMqttMessage(
              loggedMessage.getId(),
              loggedMessage.getTopic(),
              mqttMessage,
              new Date(loggedMessage.getTimestamp())));
    }
    logger.info("Message log - processed {} MQTT messages", list.size());

    return mqttMessageList;
  }