update
This commit is contained in:
parent
c69ee4e528
commit
f93efc9f60
104
MQTT.py
104
MQTT.py
|
@ -1,56 +1,70 @@
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
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
|
||||||
|
|
||||||
|
|
||||||
class MQTT:
|
def on_connect(client, userdata, flags, rc):
|
||||||
def on_connect(self, client, userdata, flags, rc):
|
if rc == 0:
|
||||||
if rc == 0:
|
client.subscribe('lot_mqtt')
|
||||||
client.subscribe('lot_mqtt')
|
|
||||||
def on_connect_fail(self):
|
|
||||||
pass
|
|
||||||
# Message receiving callback
|
|
||||||
def on_message(self, client, userdata, msg):
|
|
||||||
data = json.loads(msg.payload.decode('utf-8'))["msg"]
|
|
||||||
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, self.client)
|
|
||||||
elif data == "status":
|
|
||||||
# 查看运行状态
|
|
||||||
get_status(self.client)
|
|
||||||
else:
|
|
||||||
# 错误类型
|
|
||||||
client.publish('success', payload='No Such Type', qos=0)
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.client = mqtt.Client(client_id='device1')
|
# Message receiving callback
|
||||||
self.client.username_pw_set("demo", "123456")
|
def on_message(client, userdata, msg):
|
||||||
# Specify callback function
|
data = json.loads(msg.payload.decode('utf-8'))["msg"]
|
||||||
self.client.on_connect = self.on_connect
|
if data == "push_stream":
|
||||||
self.client.on_message = self.on_message
|
# 启动推流视频
|
||||||
# Establish a connection
|
push_stream(client)
|
||||||
# ceshi-mqtt.lihaink.cn
|
elif data == "close_stream":
|
||||||
self.client.connect('192.168.1.27', 1883)
|
# 关闭推流视频
|
||||||
# Publish a message
|
close_stream(client)
|
||||||
self.client.loop_forever(retry_first_connection=True)
|
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:
|
||||||
|
# 错误类型
|
||||||
|
client.publish('success', payload='No Such Type', qos=0)
|
||||||
|
|
||||||
|
|
||||||
|
times = 120
|
||||||
|
|
||||||
|
|
||||||
|
def exec_shutdown():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
mq = MQTT()
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
client = mqtt.Client(client_id='device1')
|
||||||
|
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
|
||||||
|
client.connect('192.168.1.27', 1883)
|
||||||
|
# Publish a message
|
||||||
|
client.loop_forever()
|
||||||
|
except:
|
||||||
|
print("等待30秒重新连接客户端")
|
||||||
|
time.sleep(30)
|
||||||
|
if times != 0:
|
||||||
|
times -= 1
|
||||||
|
# 重启机器
|
||||||
|
exec_shutdown()
|
||||||
|
|
38
ceshi2.py
38
ceshi2.py
|
@ -1,38 +0,0 @@
|
||||||
import subprocess
|
|
||||||
import time
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
|
||||||
global times
|
|
||||||
if rc == 0:
|
|
||||||
print("连接成功,执行数据推送和本地存储")
|
|
||||||
subprocess.Popen(["supervisorctl stop sensor_to_local && supervisorctl start sensor_to_server"])
|
|
||||||
times = 3
|
|
||||||
|
|
||||||
|
|
||||||
def on_connect_fail():
|
|
||||||
print("失败,执行本地存储")
|
|
||||||
subprocess.Popen(["supervisorctl stop sensor_to_server && supervisorctl start sensor_to_local"])
|
|
||||||
|
|
||||||
|
|
||||||
times = 3
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
client = mqtt.Client()
|
|
||||||
client.username_pw_set("demo", "123456")
|
|
||||||
# Specify callback function
|
|
||||||
client.on_connect = on_connect
|
|
||||||
client.on_connect_fail = on_connect_fail
|
|
||||||
# Establish a connection
|
|
||||||
client.connect('127.0.0.1', 1883)
|
|
||||||
# Publish a message
|
|
||||||
client.loop_forever()
|
|
||||||
except Exception as e:
|
|
||||||
print("等待5秒重新连接客户端")
|
|
||||||
time.sleep(5)
|
|
||||||
if times != 0:
|
|
||||||
times -= 1
|
|
||||||
on_connect_fail()
|
|
Loading…
Reference in New Issue