PotDataMessageListener.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from hub import methods, Global
  2. import threading
  3. import time
  4. import json
  5. class PotDataMessageListener(object):
  6. @staticmethod
  7. def decorate_method(client, userdata, message):
  8. """消息处理方法"""
  9. try:
  10. # --- update ---
  11. """
  12. PotData: 渣罐数据
  13. PotData.pot_name: 渣罐别称(例: M.24)
  14. PotData.pot_x: 坐标值
  15. PotData.pot_y: 坐标值
  16. PotData.mark_pot_pose: 渣罐姿态
  17. """
  18. message = json.loads(message.payload)
  19. unique_dict = {'pot_name': message.get('pot_name')}
  20. update_dict = {
  21. 'pot_x': message.get('pot_x'),
  22. 'pot_y': message.get('pot_y'),
  23. 'mark_pot_pose': message.get('mark_pot_pose'),
  24. }
  25. Global.mdb.update_one('PotData', unique_dict, update_dict)
  26. methods.debug_log(f"PotDataMessageListener.30", f"#message: {message}")
  27. except Exception as exception:
  28. methods.debug_log('PotDataMessageListener.33', f"#exception: {exception}")
  29. methods.debug_log('PotDataMessageListener.33', f"#traceback: {methods.trace_log()}")
  30. @classmethod
  31. def start_check_loop(cls):
  32. """启动循环"""
  33. """
  34. topic: hs/pot/data
  35. message.name: 渣罐名称
  36. message.pot_x: 坐标值
  37. message.pot_y: 坐标值
  38. message.mark_pot_pose: 渣罐姿态
  39. """
  40. Global.mq02.start_subscribe_loop(
  41. decorate_method=PotDataMessageListener.decorate_method,
  42. subscribe_topic='hs/pot/data'
  43. )
  44. @classmethod
  45. def run_background(cls, background_is=True):
  46. """启动现成"""
  47. p1 = threading.Thread(target=cls.start_check_loop)
  48. p1.start()
  49. if __name__ == '__main__':
  50. # --- test ---
  51. MessageListener.run_background()