提交 93cb8905 authored 作者: zxd's avatar zxd

发送mqtt消息

上级 22bc57a0
...@@ -13,5 +13,35 @@ ...@@ -13,5 +13,35 @@
"port_address": 1 "port_address": 1
} }
] ]
},
{
"emq_host": "139.224.69.89",
"emq_port": 31101,
"serial_no": "VT00002",
"measurements": [
{
"measurement": "door",
"type": 0,
"random_type": 1,
"port": "c",
"port_address": 1
}
]
},
{
"emq_host": "139.224.69.89",
"emq_port": 31101,
"serial_no": "VT00003",
"measurements": [
{
"measurement": "door",
"type": 0,
"random_type": 1,
"port": "c",
"port_address": 1
}
]
} }
] ]
\ No newline at end of file
# encoding=utf8 # encoding=utf8
import json
import random
import sys import sys
from paho.mqtt import client as mqtt_client
######################## ########################
# 虚拟终端上报数据脚本 # 虚拟终端上报数据脚本
######################## ########################
import time
client_id = f'python-mqtt-{random.randint(0, 1000)}'
topic = "wftest"
username = "admin"
password = "public"
cmdTemplate = """
{{
"devSN": "{devSN}",
"data": {{
"offline": false,
"telemetry": {telemetry}
}},
"time": {time},
"mqttTopic": "/{topic}/{devSn1}/lalala",
"type": "data",
"deviceName": "105104012101126339",
"group": "saaspri",
"network": "wifi",
"middleware": "hub"
}}
"""
telemetryTemplate = """
{{
"port": "{port}",
"portAddress": {protAddress},
"time": [
{time}
],
"value": {{
"{measurement}": [
{value}
]
}}
}}
"""
def getTelemetry(param):
t = time.time()
l = len(param)
tjsons = []
for i in range(l):
dataT = telemetryTemplate.format(port=param[i].get("port"), protAddress=param[i].get("port_address"),
time=str(t),
measurement=param[i].get("measurement"), value=str(random.randint(0, 1000)))
tJson = json.loads(dataT)
tjsons.append(tJson)
return json.dumps(tjsons)
def connect_mqtt(ip, port):
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(ip, port)
return client
def publish(client, msg):
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
def run(ip, port, msg):
client = connect_mqtt(ip=ip, port=port)
client.loop_start()
publish(client, msg)
terminal_confs = [
{
"emq_host": "",
"emq_port": 11883,
"serial_no": "v000000001",
"measurements": [
{
"measurement": "door",
"type": 0,
"random_type": 1,
"port": "c",
"port_address": 1
}
]
},
]
if __name__ == '__main__':
jsonFile = "emq_terminal.json"
while(True):
with open(jsonFile, "r") as fp:
datas = json.load(fp)
dataLen = len(datas)
for key in range(dataLen):
data = datas[key]
tel = getTelemetry(data.get("measurements"))
cmd = cmdTemplate.format(devSN=data.get("serial_no"), telemetry=tel, time=str(time.time()),
devSn1=data.get("serial_no"), topic=topic)
print(cmd)
run(data.get("emq_host"), data.get("emq_port"), cmd)
time.sleep(10)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论