Add timestamp when publishing and reporting messages.
This commit is contained in:
parent
e29b6f6c82
commit
178d279b9b
@ -86,6 +86,7 @@ export class Communicator {
|
|||||||
const msgStr = message.toString();
|
const msgStr = message.toString();
|
||||||
const msgJson = JSON.parse(msgStr);
|
const msgJson = JSON.parse(msgStr);
|
||||||
console.log(`Message received on topic ${topic}: ${msgStr}`);
|
console.log(`Message received on topic ${topic}: ${msgStr}`);
|
||||||
|
if (msgJson.timestamp) console.log(`Timestamp: ${msgJson.timestamp}`);
|
||||||
console.log(msgJson);
|
console.log(msgJson);
|
||||||
state[msgJson.name].on = msgJson.on;
|
state[msgJson.name].on = msgJson.on;
|
||||||
if (msgJson.feedRate) state[msgJson.name].feedRate = msgJson.feedRate;
|
if (msgJson.feedRate) state[msgJson.name].feedRate = msgJson.feedRate;
|
||||||
@ -95,6 +96,10 @@ export class Communicator {
|
|||||||
|
|
||||||
// Publish a message to the MQTT Broker
|
// Publish a message to the MQTT Broker
|
||||||
send(topic, message) {
|
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
|
// Publish with retain flag set to true
|
||||||
this.client.publish(topic, message, { retain: true }, (err) => {
|
this.client.publish(topic, message, { retain: true }, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
Loading…
Reference in New Issue
Block a user