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