From e73133611131968fc77e77993f72c0f6866c9c28 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Thu, 7 Dec 2023 19:00:16 +0800 Subject: [PATCH] update --- MQTT.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/MQTT.py b/MQTT.py index cccbc9d..3253ee9 100644 --- a/MQTT.py +++ b/MQTT.py @@ -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"]