Skip to content

MessageAuditReplay

Kamil Baczkowicz edited this page Jan 20, 2016 · 1 revision

In order to replay messages from a mqtt-spy message audit log file, use the messageLog object. The available methods are:

  • readFromFile (takes the location of the message log file; returns number of messages available)
  • getMessageCount (gets the available number of messages)
  • start (starts the message log time updater and sets the start position to the first message)
  • stop (stops the time updater)
  • setSpeed (sets replay speed, e.g. 1 is the normal speed (as in the message log), 2 is twice the normal, 0.5 is half the normal)
  • isReadyToPublish (checks if a message with the given index is ready to be published; 0 is the first message)
  • getMessage (retrieves message with the given index; 0 is the first message)

See the samples section for a replay script example.

Samples

Message log replay

function replay()
{
	// Get the number of available messages (0 when run for the first time)
	var messageCount = messageLog.getMessageCount();
	
	// If repeat = true, only read the message log once
	if (messageCount == 0)
	{
		messageCount = messageLog.readFromFile("/home/kamil/mqtt-spy-daemon.messages");		
		messageLog.setSpeed(2);		
	}
	
	// If there are messages to replay...
	if (messageCount > 0)
	{
		// Start the message log time updater...
		messageLog.start();
			
		var Thread = Java.type("java.lang.Thread");	
	
		// For all messages
		for (i = 0; i < messageCount; i++)
		{
			// Wait until this message is ready to be published
			while (!messageLog.isReadyToPublish(i))		
			{
				try 
				{
					Thread.sleep(10);
				}
				catch(err) 
				{
					return false;				
				}
			}
			
			// When ready, publish the message
			mqttspy.publish(messageLog.getMessage(i).getTopic(), messageLog.getMessage(i).getRawMessage(), 0, false);				
		}
	}
	else
	{
		logger.warn("No messages available");
	}
	
	return true;
}

replay();