update
This commit is contained in:
parent
9924857d9a
commit
8b4d7653ca
11
MQTT.py
11
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')
|
||||
|
|
Loading…
Reference in New Issue