java – compare objects and calculate new value on time from mqtt

I would like to calculate a average value of a Datastream from MQTT with specific Timerange.
I get every second Message from MQTT and create a Java Object on it:

{timestamp=1636544445334, topic="topicname", value=10.0, aggregationtime=1636544446834}

The different from timestamp and aggregationtime is the Range to calculate average Value on value.

I think i need to store the reference Object with the aggregationtime and compare the timestamp from stored reference Object with the new received Message: aggregationtime

Example:

Message 0.

{timestamp=1636544445334, topic="topicname", value=10.0, aggregationtime=1636544446834}

Message 1:

{timestamp=1636544445345, topic="topicname", value=4.0, aggregationtime=1636544457811}

Message 2:

{timestamp=1636544445388, topic="topicname", value=11.0, aggregationtime=1636544498443}

Calculation like this for MessageX:

if(message0.aggregationtime > Message1.timestamp)
    value=average(message0.value,message1.value)

How can i store the first Message as a reference Object in Java and compare it with new Messages? What is the best practice to solve this?

THX

Read more here: Source link