diff --git a/src/MQTT.py b/src/MQTT.py index e4a7d8a..5f25074 100644 --- a/src/MQTT.py +++ b/src/MQTT.py @@ -17,11 +17,13 @@ def on_connect(client, userdata, flags, rc): def valid(msg, client): origin_data = json.loads(msg.payload.decode('utf-8')) - dev_num = origin_data['device_name'] - if dev_num is None: - client.publish('error', payload='dev_num must be supplied', qos=0) + if 'msg' not in origin_data: + client.publish('error', payload='msg must be supplied', qos=0) return False - if dev_num != device_name: + 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 @@ -29,13 +31,12 @@ def valid(msg, client): # Message receiving callback def on_message(client, userdata, msg): if not valid(msg, client): + # print("验证失败") return + # print("验证通过") try: origin_data = json.loads(msg.payload.decode('utf-8')) data = origin_data["msg"] - if data is None: - client.publish('error', payload='msg must be supplied', qos=0) - return if data == "push_stream": # 启动推流视频 push_stream(client) diff --git a/src/__pycache__/tool.cpython-310.pyc b/src/__pycache__/tool.cpython-310.pyc index f00202c..de4864f 100644 Binary files a/src/__pycache__/tool.cpython-310.pyc and b/src/__pycache__/tool.cpython-310.pyc differ diff --git a/src/tool.py b/src/tool.py index 7e41d63..a28008f 100644 --- a/src/tool.py +++ b/src/tool.py @@ -25,7 +25,11 @@ def close_stream(client): def exec_sh(msg, client): - cmd = json.loads(msg.payload.decode('utf-8'))["data"] + origin_data = json.loads(msg.payload.decode('utf-8')) + if 'data' not in origin_data: + client.publish('error', payload='data must be supplied', qos=0) + return + cmd = origin_data["data"] if cmd == "supervisorctl stop __mqtt__" or cmd == "supervisorctl restart __mqtt__": return if cmd == "supervisorctl reload":