import paho.mqtt.client as mqtt
import time
import json

client = mqtt.Client()
# client.connect(host='10.10.61.229', port=41883)
client.connect(host='10.10.10.116', port=41883)
# client.connect(host='10.10.10.73', port=41883)
# client.connect(host='127.0.0.1', port=41883)  # 本地环境


def test_bg_log():
    """
    mqtt消息注释:
    {
      "1": "2024-12-12 01:01:21",  # 发送时间
      "2": 6000,  # 方向值
      "3": 6000,  # 手油门值
      "4": 6000,  # 脚油门值
      "5": 6000   # 刹车值
    }
    """
    data = {
        1: '2024-12-12 01:01:21',  # 发送时间
        2: 6000,  # 方向值
        3: 6000,  # 油门值
        4: 6000,  # 刹车值
    }
    # results = client.publish('bg/log', json.dumps(data))
    results = client.publish('qtmqtt', json.dumps(data))
    result_code, message_id = results
    print(f"#result_code: {result_code}, #message_id: {message_id}")


if __name__ == '__main__':
    # --- test ---
    while True:
        test_bg_log()
        time.sleep(3)