This commit is contained in:
xyj 2023-12-07 19:00:16 +08:00
parent 00a58c9e7e
commit e731336111
1 changed files with 15 additions and 17 deletions

32
MQTT.py
View File

@ -12,27 +12,25 @@ def on_connect(client, userdata, flags, rc):
client.subscribe('lot_mqtt')
# def valid(msg, client):
# origin_data = json.loads(msg.payload.decode('utf-8'))
# if 'msg' not in origin_data:
# client.publish('error', payload='msg must be supplied', qos=0)
# return False
# if 'device_name' not in origin_data:
# client.publish('error', payload='device_name must be supplied', qos=0)
# return False
# return True
def valid(msg, client):
origin_data = json.loads(msg.payload.decode('utf-8'))
if 'msg' not in origin_data:
client.publish('error', payload='msg must be supplied', qos=0)
return False
if 'device_name' not in origin_data:
client.publish('error', payload='device_name must be supplied', qos=0)
return False
if origin_data['device_name'] != device_name:
return False
return True
# Message receiving callback
def on_message(client, userdata, msg):
# if valid(msg, client) is False:
# client.publish('error', payload='验证失败', qos=0)
# return
# print("验证通过")
import os
# device_name = os.environ['device_name']
client.publish('error', payload=device_name, qos=0)
# client.publish('success', payload='验证通过', qos=0)
if valid(msg, client) is False:
client.publish('error', payload='验证失败', qos=0)
return
client.publish('success', payload='验证通过', qos=0)
try:
origin_data = json.loads(msg.payload.decode('utf-8'))
data = origin_data["msg"]