This commit is contained in:
xyj 2023-12-07 19:04:49 +08:00
parent 9924857d9a
commit 8b4d7653ca
2 changed files with 10 additions and 4 deletions

11
MQTT.py
View File

@ -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')

3
test.py Normal file
View File

@ -0,0 +1,3 @@
import os
print('test' == os.getenv('device_name'))