12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import paho.mqtt.client as mqtt
- import time
- import json
- class Client(mqtt.Client):
- def __init__(self, host='127.0.0.1', port=41883):
- super().__init__()
- self.connect(host=host, port=port, keepalive=60)
-
-
-
-
-
-
-
-
-
-
- def start_subscribe_loop(self, decorate_method, subscribe_topic):
- """启动订阅循环"""
- self.on_message = decorate_method
- self.subscribe(subscribe_topic)
- self.loop_forever()
- def publish_message(self, publish_topic, message):
- """发布消息"""
- try:
- results = self.publish(publish_topic, message)
- result_code, message_id = results
- """
- result_code: 错误码
- result_code.0: 成功
- result_code.1: 失败
- result_code.4: 无法连接到 MQTT 代理
- result_code.7: QoS错误
- """
- print(f"#result_code: {result_code}, #message_id: {message_id}")
- except Exception as e:
- print(f'#Exception: {e.__class__.__name__}')
- if __name__ == '__main__':
- """
- cd /home/sri/repositories/repositories/SRI-DINO.Server-py/sri-pysdk/xclient && python3 xmqtt.py
-
- pip3 install paho-mqtt==1.6.1
-
- python3 xmqtt-a1.py
- python3 xmqtt-a2.py
- python3 xmqtt.py
-
- pip3 install paho-mqtt==1.6.1 -i hhttps://mirror.baidu.com/pypi/simple
-
-
- """
-
-
-
- c1 = Client(host='10.10.10.116', port=41883)
-
- def m1(_, __, p3):
-
- print(f"#message.payload: {json.loads(p3.payload)}")
-
-
-
- subscribe_topic = 'Vehicle/ControlVehicle/Veh001'
- c1.start_subscribe_loop(decorate_method=m1, subscribe_topic=subscribe_topic)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|