This commit is contained in:
xyj 2023-12-07 18:50:18 +08:00
parent f67220d983
commit e1b856c3a7
1 changed files with 11 additions and 14 deletions

25
MQTT.py
View File

@ -6,23 +6,20 @@ import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
if rc == 0: if rc == 0:
client.subscribe('lot_mqtt') client.subscribe('lot_mqtt')
def valid(msg, client): # def valid(msg, client):
# 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) # return False
return False # 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 # return True
return True
# Message receiving callback # Message receiving callback
@ -34,7 +31,7 @@ def on_message(client, userdata, msg):
import os import os
# device_name = os.environ['device_name'] # device_name = os.environ['device_name']
# client.publish('error', payload=device_name, qos=0) # client.publish('error', payload=device_name, qos=0)
client.publish('success', payload='验证通过', qos=0) # client.publish('success', payload='验证通过', qos=0)
try: try:
origin_data = json.loads(msg.payload.decode('utf-8')) origin_data = json.loads(msg.payload.decode('utf-8'))
data = origin_data["msg"] data = origin_data["msg"]
@ -76,7 +73,7 @@ def exec_shutdown():
if __name__ == '__main__': if __name__ == '__main__':
while True: while True:
try: try:
client = mqtt.Client(client_id=os.getenv('device_name')) client = mqtt.Client(client_id='s')
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