2023-12-02 10:53:31 +08:00
|
|
|
import json
|
2023-12-07 18:58:36 +08:00
|
|
|
import subprocess
|
2023-12-05 17:16:32 +08:00
|
|
|
import time
|
2023-12-02 10:53:31 +08:00
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
2023-12-07 18:23:48 +08:00
|
|
|
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
|
2023-12-02 10:53:31 +08:00
|
|
|
|
2023-12-07 15:27:38 +08:00
|
|
|
|
2023-12-05 17:16:32 +08:00
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
if rc == 0:
|
|
|
|
client.subscribe('lot_mqtt')
|
|
|
|
|
|
|
|
|
2023-12-07 19:00:16 +08:00
|
|
|
def valid(msg, client):
|
2023-12-07 19:04:49 +08:00
|
|
|
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')
|
2023-12-07 19:00:16 +08:00
|
|
|
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)
|
|
|
|
return False
|
|
|
|
if 'device_name' not in origin_data:
|
|
|
|
client.publish('error', payload='device_name must be supplied', qos=0)
|
|
|
|
return False
|
2023-12-07 19:04:49 +08:00
|
|
|
client.publish('error', payload=origin_data['device_name'], qos=0)
|
|
|
|
client.publish('error', payload=device_name, qos=0)
|
2023-12-07 19:01:36 +08:00
|
|
|
if origin_data['device_name'] != 'test':
|
2023-12-07 19:00:16 +08:00
|
|
|
return False
|
|
|
|
return True
|
2023-12-07 15:27:38 +08:00
|
|
|
|
|
|
|
|
2023-12-05 17:16:32 +08:00
|
|
|
# Message receiving callback
|
|
|
|
def on_message(client, userdata, msg):
|
2023-12-07 19:04:49 +08:00
|
|
|
if not valid(msg, client):
|
2023-12-07 19:00:16 +08:00
|
|
|
client.publish('error', payload='验证失败', qos=0)
|
|
|
|
return
|
|
|
|
client.publish('success', payload='验证通过', qos=0)
|
2023-12-07 09:27:37 +08:00
|
|
|
try:
|
2023-12-07 15:27:38 +08:00
|
|
|
origin_data = json.loads(msg.payload.decode('utf-8'))
|
|
|
|
data = origin_data["msg"]
|
2023-12-07 09:27:37 +08:00
|
|
|
if data == "push_stream":
|
|
|
|
# 启动推流视频
|
|
|
|
push_stream(client)
|
|
|
|
elif data == "close_stream":
|
|
|
|
# 关闭推流视频
|
|
|
|
close_stream(client)
|
|
|
|
elif data == "exec":
|
|
|
|
# 执行命令
|
|
|
|
exec_sh(msg, client)
|
|
|
|
elif data == "update":
|
|
|
|
# git更新项目和配置文件
|
|
|
|
update(client)
|
|
|
|
elif data == "record_list":
|
|
|
|
# 查看录像列表
|
|
|
|
get_list_record()
|
|
|
|
elif data == "record":
|
|
|
|
# 获取录像
|
|
|
|
get_record(msg, client)
|
|
|
|
elif data == "status":
|
|
|
|
# 查看运行状态
|
|
|
|
get_status(client)
|
|
|
|
else:
|
|
|
|
# 错误类型
|
2023-12-07 15:27:38 +08:00
|
|
|
client.publish('error', payload='No Such Type', qos=0)
|
2023-12-07 09:27:37 +08:00
|
|
|
except Exception as e:
|
|
|
|
pass
|
2023-12-05 17:16:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
times = 120
|
|
|
|
|
|
|
|
|
|
|
|
def exec_shutdown():
|
|
|
|
pass
|
2023-12-02 10:53:31 +08:00
|
|
|
|
2023-12-07 18:58:36 +08:00
|
|
|
|
2023-12-07 18:54:45 +08:00
|
|
|
import os
|
2023-12-07 18:58:36 +08:00
|
|
|
|
2023-12-02 10:53:31 +08:00
|
|
|
if __name__ == '__main__':
|
2023-12-07 19:04:49 +08:00
|
|
|
|
2023-12-05 17:16:32 +08:00
|
|
|
while True:
|
|
|
|
try:
|
2023-12-07 18:50:18 +08:00
|
|
|
client = mqtt.Client(client_id='s')
|
2023-12-05 17:16:32 +08:00
|
|
|
client.username_pw_set("demo", "123456")
|
|
|
|
# Specify callback function
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.on_message = on_message
|
|
|
|
# Establish a connection
|
|
|
|
# ceshi-mqtt.lihaink.cn
|
2023-12-07 18:28:24 +08:00
|
|
|
client.connect('ceshi-mqtt.lihaink.cn', 1883)
|
2023-12-05 17:16:32 +08:00
|
|
|
# Publish a message
|
|
|
|
client.loop_forever()
|
|
|
|
except:
|
2023-12-07 09:20:44 +08:00
|
|
|
print("等待30秒连接")
|
2023-12-05 17:16:32 +08:00
|
|
|
time.sleep(30)
|
2023-12-05 18:38:15 +08:00
|
|
|
if times == 0:
|
2023-12-05 17:16:32 +08:00
|
|
|
# 重启机器
|
|
|
|
exec_shutdown()
|
2023-12-05 18:38:15 +08:00
|
|
|
times -= 1
|