xmqtt.py 3.1 KB

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