Skip to content

Commit

Permalink
Add mqtt message order check
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e committed Oct 22, 2022
1 parent 0facdcb commit d7faba0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/core/TuyaOpenMQ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,24 @@ export default class TuyaOpenMQ {
this.log.debug('TuyaOpenMQ end');
}

private lastPayload?;
_onMessage(topic: string, payload: Buffer) {
const { protocol, data, t } = JSON.parse(payload.toString());
const message = this._decodeMQMessage(data, this.config!.password, t);
let message = this._decodeMQMessage(data, this.config!.password, t);
this.log.debug(`TuyaOpenMQ onMessage: topic = ${topic}, protocol = ${protocol}, message = ${message}`);
message = JSON.parse(message);

// Check message order
const currentPayload = { protocol, message, t };
if (this.lastPayload && t < this.lastPayload.t) {
this.log.warn(`TuyaOpenMQ warning: message received with wrong order.
lastMessage: dataId=${this.lastPayload.message['dataId']}, t=${this.lastPayload.t}, ${new Date(this.lastPayload.t).toISOString()}
currentMessage: dataId=${message['dataId']}, t=${t}, ${new Date(t).toISOString()}`);
}
this.lastPayload = currentPayload;

for (const listener of this.messageListeners) {
listener(topic, protocol, JSON.parse(message));
listener(topic, protocol, message);
}
}

Expand Down

0 comments on commit d7faba0

Please sign in to comment.