简体   繁体   English

如何处理 MQTT 订阅者中的部分消息?

[英]How to handle partial messages in MQTT subscriber?

I have an IoT gateway device that publishes data to a MQTT broker every minute.我有一个物联网网关设备,每分钟都会向 MQTT 代理发布数据。 However, the message comes as several packets.但是,该消息以多个数据包的形式出现。 I need to recombine these partial message packets into one complete message, parse it and then store in a database.我需要将这些部分消息包重新组合成一个完整的消息,解析它然后存储在数据库中。 I'm using paho python MQTT library.我正在使用 paho python MQTT 库。 The problem is that the callback is triggered upon each received partial packet but I want to trigger it only when all packets associated with the same message are received.问题是回调是在每个接收到的部分数据包时触发的,但我只想在接收到与同一消息关联的所有数据包时触发它。 Is there anyway to do that?有没有办法做到这一点? Cheers干杯

Sample partial messages look like this:示例部分消息如下所示:

Packet 1 =数据包 1 =

{'DB3': {'CH1-01': '243.7300', 'CH1-02': '0.0000', 'CH1-03': '0.0000', 'CH1-04': '243.7300', 'CH1-05': '0.0000', 'CH1-06': '243.7300', 'CH1-07': '0.0000', 'CH1-08': '0.0000', 'CH1-09': '0.0000', 'CH1-10': '0.0440', 'CH1-11': '0.0000', 'CH1-12': '0.0000', 'CH1-13': '0.0440', 'CH1-14': '-0.0040', 'CH1-15': '0.0000', 'CH1-16': '0.0000', 'CH1-17': '-0.0040', 'CH1-18': '0.0440', 'CH1-19': '0.0000', 'CH1-20': '0.0000', 'CH1-21': '0.0440', 'CH1-22': '0.0000', 'CH1-23': '0.0000', 'CH1-24': '0.0000', 'CH1-25': '0.0000', 'CH1-26': '50.0160', 'CH1-27': '3.6400', 'CH1-28': '0.0000', 'CH1-29': '0.0000', 'CH1-30': '0.4000', 'CH1-31': '3.6400', 'CH1-32': '0.0000', 'CH1-33': '0.0000', 'CH1-34': '0.4000', 'CH1-35': '0.0000', 'CH1-36': '0.0000', 'CH1-37': '0.0000', 'CH1-38': '0.0000', 'CH1-39': '0.0000', 'CH1-40': '0.0000', 'CH1-41': '0.0000', 'CH1-42': '0.0000', 'CH2-01': '243.6100', 'CH2-02': '0.0000', 'CH2-03': '0.0000', 'CH2-04': '243.6100', 'CH2-05': '0.0000', 'CH2-06': '243.6100', 'CH2-07': '0.0000', 'CH2-08': '0.0000'}, 'ts': 1637040150000}

Packet 2 =数据包 2 =

{'DB3': {'CH2-09': '0.0000', 'CH2-10': '0.0000', 'CH2-11': '0.0000', 'CH2-12': '0.0000', 'CH2-13': '0.0000', 'CH2-14': '0.0000', 'CH2-15': '0.0000', 'CH2-16': '0.0000', 'CH2-17': '0.0000', 'CH2-18': '0.0040', 'CH2-19': '0.0000', 'CH2-20': '0.0000', 'CH2-21': '0.0040', 'CH2-22': '0.0000', 'CH2-23': '0.0000', 'CH2-24': '0.0000', 'CH2-25': '0.0000', 'CH2-26': '50.0200', 'CH2-27': '0.0000', 'CH2-28': '0.0000', 'CH2-29': '0.0000', 'CH2-30': '0.0000', 'CH2-31': '0.0000', 'CH2-32': '0.0000', 'CH2-33': '0.0000', 'CH2-34': '0.0000', 'CH2-35': '0.0000', 'CH2-36': '0.0000', 'CH2-37': '0.0000', 'CH2-38': '0.0000', 'CH2-39': '0.0000', 'CH2-40': '0.0000', 'CH2-41': '0.0000', 'CH2-42': '0.0000', 'CH3-01': '243.6400', 'CH3-02': '0.0000', 'CH3-03': '0.0000', 'CH3-04': '243.6400', 'CH3-05': '0.0000', 'CH3-06': '243.6400', 'CH3-07': '0.0000', 'CH3-08': '0.0000', 'CH3-09': '0.0000', 'CH3-10': '0.0000', 'CH3-11': '0.0000', 'CH3-12': '0.0000', 'CH3-13': '0.0000', 'CH3-14': '0.0000', 'CH3-15': '0.0000', 'CH3-16': '0.0000'}, 'ts': 1637040150000}

This isn't a really a MQTT or Paho question, both are working exactly as expected, the problem is with the device publishing the messages if there is no way to determine how many parts it will send or the start/end of a stream of packets.这不是一个真正的 MQTT 或 Paho 问题,两者都按预期工作,问题在于设备发布消息,如果无法确定它将发送多少部分或开始/结束流数据包。

My best suggestion is to set a timeout after the first message and when that expires assemble all the messages that arrived in that period.我最好的建议是在第一条消息之后设置一个超时时间,当它到期时组合在该时间段内到达的所有消息。 What you set that timeout to you'll have to determine, but if you have configured the data to come in bursts every 60 seconds then possibly start at 15 seconds.您必须确定该超时的设置,但如果您已将数据配置为每 60 秒突发一次,则可能从 15 秒开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM