From 8b4d7653cad8b7b4caeeefa83dda9e9d1bd3a8e5 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Thu, 7 Dec 2023 19:04:49 +0800 Subject: [PATCH] update --- MQTT.py | 11 +++++++---- test.py | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 test.py diff --git a/MQTT.py b/MQTT.py index fa2090a..56816f7 100644 --- a/MQTT.py +++ b/MQTT.py @@ -13,6 +13,9 @@ def on_connect(client, userdata, flags, rc): def valid(msg, client): + p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate() + device_name = out.decode('utf-8') 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) @@ -20,6 +23,8 @@ def valid(msg, client): if 'device_name' not in origin_data: client.publish('error', payload='device_name must be supplied', qos=0) return False + client.publish('error', payload=origin_data['device_name'], qos=0) + client.publish('error', payload=device_name, qos=0) if origin_data['device_name'] != 'test': return False return True @@ -27,7 +32,7 @@ def valid(msg, client): # Message receiving callback def on_message(client, userdata, msg): - if valid(msg, client) is False: + if not valid(msg, client): client.publish('error', payload='验证失败', qos=0) return client.publish('success', payload='验证通过', qos=0) @@ -72,9 +77,7 @@ def exec_shutdown(): import os if __name__ == '__main__': - p = subprocess.Popen(['cat /home/pi/device_name'], shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) - out, err = p.communicate() - device_name = out.decode('utf-8') + while True: try: client = mqtt.Client(client_id='s') diff --git a/test.py b/test.py new file mode 100644 index 0000000..f4c5973 --- /dev/null +++ b/test.py @@ -0,0 +1,3 @@ +import os + +print('test' == os.getenv('device_name')) \ No newline at end of file