This commit is contained in:
xyj 2023-12-07 19:15:41 +08:00
parent fc92747de2
commit f862b67c99
2 changed files with 5 additions and 6 deletions

11
MQTT.py
View File

@ -13,9 +13,6 @@ def on_connect(client, userdata, flags, rc):
def valid(msg, client): 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')) origin_data = json.loads(msg.payload.decode('utf-8'))
if 'msg' not in origin_data: if 'msg' not in origin_data:
client.publish('error', payload='msg must be supplied', qos=0) client.publish('error', payload='msg must be supplied', qos=0)
@ -23,7 +20,7 @@ def valid(msg, client):
if 'device_name' not in origin_data: if 'device_name' not in origin_data:
client.publish('error', payload='device_name must be supplied', qos=0) client.publish('error', payload='device_name must be supplied', qos=0)
return False return False
if device_name.casefold() != origin_data['device_name'].casefold(): if device_name != origin_data['device_name']:
return False return False
return True return True
@ -75,10 +72,12 @@ def exec_shutdown():
import os import os
if __name__ == '__main__': 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').strip()
while True: while True:
try: try:
client = mqtt.Client(client_id='s') client = mqtt.Client(client_id=device_name)
client.username_pw_set("demo", "123456") client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect

Binary file not shown.