xmqtt.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import paho.mqtt.client as mqtt
  2. import time
  3. import json
  4. class Client(mqtt.Client):
  5. def __init__(self, host='127.0.0.1', port=41883):
  6. super().__init__()
  7. self.connect(host=host, port=port, keepalive=60)
  8. # if subscribe_topic:
  9. # def on_message(client, userdata, message):
  10. # # print(f"#userdata: {userdata}")
  11. # # print(f"#message.payload: {str(message.payload.decode())}")
  12. # print(f"#message.payload: {json.loads(message.payload)}")
  13. # print(f"#message.topic: {message.topic}")
  14. #
  15. # self.on_message = on_message
  16. # self.subscribe(subscribe_topic)
  17. # self.loop_forever()
  18. def start_subscribe_loop(self, decorate_method, subscribe_topic):
  19. """启动订阅循环"""
  20. self.on_message = decorate_method
  21. self.subscribe(subscribe_topic)
  22. self.loop_forever()
  23. def publish_message(self, publish_topic, message):
  24. """发布消息"""
  25. try:
  26. results = self.publish(publish_topic, message)
  27. result_code, message_id = results
  28. """
  29. result_code: 错误码
  30. result_code.0: 成功
  31. result_code.1: 失败
  32. result_code.4: 无法连接到 MQTT 代理
  33. result_code.7: QoS错误
  34. """
  35. print(f"#result_code: {result_code}, #message_id: {message_id}")
  36. except Exception as e:
  37. print(f'#Exception: {e.__class__.__name__}')
  38. if __name__ == '__main__':
  39. """
  40. cd /home/sri/repositories/repositories/SRI-DINO.Server-py/sri-pysdk/xclient && python3 xmqtt.py
  41. pip3 install paho-mqtt==1.6.1
  42. python3 xmqtt-a1.py
  43. python3 xmqtt-a2.py
  44. python3 xmqtt.py
  45. pip3 install paho-mqtt==1.6.1 -i hhttps://mirror.baidu.com/pypi/simple
  46. """
  47. # --- init ---
  48. # c1 = Client(host='192.168.131.23', port=41883)
  49. # c1 = Client(host='127.0.0.1', port=41883)
  50. # c1 = Client(host='10.10.10.116', port=41883)
  51. c1 = Client(host='10.10.60.237', port=41883)
  52. # --- test subscribe ---
  53. def m1(_, __, p3):
  54. # print(f'#message: {p3.payload}')
  55. print(f"#message.payload: {p3.payload}")
  56. # print(f"#message.payload: {json.loads(p3.payload)}")
  57. # subscribe_topic = 'bg/log'
  58. # subscribe_topic = 'qtmqtt'
  59. # subscribe_topic = 'client'
  60. # subscribe_topic = 'Vehicle/ControlVehicle/Veh001'
  61. subscribe_topic = 'Cockpit/CanBus/CanId001/Cop001'
  62. c1.start_subscribe_loop(decorate_method=m1, subscribe_topic=subscribe_topic)
  63. # --- test publish ---
  64. # while True:
  65. # data = {
  66. # 'code': 1001,
  67. # '方向': 6000,
  68. # '刹车': 6000,
  69. # '油门': 8000,
  70. # }
  71. # # c1.publish_message('bg/log', json.dumps(data))
  72. # c1.publish_message('hs/vehicle/state', json.dumps(data))
  73. # time.sleep(3)
  74. # --- test publish ---
  75. # while True:
  76. # data = {
  77. # 'address': "192.168.131.180",
  78. # 'state': 2,
  79. # 'direction': 22,
  80. # }
  81. # c1.publish_message('hs/vehicle/state', json.dumps(data))
  82. # time.sleep(3)