51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import json
|
||
import os
|
||
import time
|
||
import paho.mqtt.client as mqtt
|
||
|
||
# ===== MQTT TCP 连接信息 =====
|
||
BROKER_HOST = os.getenv("MQTT_HOST", "127.0.0.1")
|
||
BROKER_PORT = int(os.getenv("MQTT_PORT", "8082"))
|
||
USERNAME = os.getenv("MQTT_USERNAME", "")
|
||
PASSWORD = os.getenv("MQTT_PASSWORD", "")
|
||
TOPIC = os.getenv("MQTT_TOPIC", "cusc/v2/SF053/QingDrsu001/data")
|
||
|
||
payload = {
|
||
"msgType": "deviceReq",
|
||
"encryptFlag": 0,
|
||
"seriesNumber": "tcp-test-001",
|
||
"reportTime": int(time.time() * 1000),
|
||
"serviceType": "rsu-traffic-lights",
|
||
"serviceData": {
|
||
"collectTime": int(time.time() * 1000),
|
||
"trafficLightsId": "TL001",
|
||
"longitude": 120.1,
|
||
"latitude": 31.1,
|
||
"intersection": "TCP测试路口",
|
||
"generateTime": int(time.time() * 1000),
|
||
"trafficLightsStatus": 1,
|
||
"phases": [
|
||
{
|
||
"phaseId": "1",
|
||
"phasePosition": 0,
|
||
"phaseColor": 1,
|
||
"trafficLightsType": 1,
|
||
"countDown": 10,
|
||
"redDuration": 30,
|
||
"greenDuration": 20,
|
||
"yellowDuration": 5
|
||
}
|
||
]
|
||
}
|
||
}
|
||
|
||
client = mqtt.Client(client_id="python-tcp-client")
|
||
if USERNAME:
|
||
client.username_pw_set(USERNAME, PASSWORD)
|
||
client.connect(BROKER_HOST, BROKER_PORT, 60)
|
||
|
||
client.publish(TOPIC, json.dumps(payload), qos=1)
|
||
print("消息已发送(MQTT over TCP)")
|
||
|
||
client.disconnect()
|