Add timestamp when publishing and reporting messages.

This commit is contained in:
Skylar Grant 2025-01-18 16:05:52 -05:00
parent e29b6f6c82
commit 178d279b9b

View File

@ -86,6 +86,7 @@ export class Communicator {
const msgStr = message.toString();
const msgJson = JSON.parse(msgStr);
console.log(`Message received on topic ${topic}: ${msgStr}`);
if (msgJson.timestamp) console.log(`Timestamp: ${msgJson.timestamp}`);
console.log(msgJson);
state[msgJson.name].on = msgJson.on;
if (msgJson.feedRate) state[msgJson.name].feedRate = msgJson.feedRate;
@ -95,6 +96,10 @@ export class Communicator {
// Publish a message to the MQTT Broker
send(topic, message) {
// Append timestamp to the message
const msgJson = JSON.parse(message);
msgJson.timestamp = Date.now();
message = JSON.stringify(msgJson);
// Publish with retain flag set to true
this.client.publish(topic, message, { retain: true }, (err) => {
if (err) {