update
This commit is contained in:
parent
99526ffa7c
commit
63cf55205d
22
MQTT.py
22
MQTT.py
|
@ -4,6 +4,10 @@ import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
from device import device_name
|
from device import device_name
|
||||||
from tool import *
|
from tool import *
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('/home/pi/lot_manager/conf/main/config.conf')
|
||||||
|
|
||||||
|
|
||||||
def valid(msg, client):
|
def valid(msg, client):
|
||||||
|
@ -20,17 +24,19 @@ def valid(msg, client):
|
||||||
|
|
||||||
|
|
||||||
class MQTTClient:
|
class MQTTClient:
|
||||||
def __init__(self, broker, port, topic):
|
def __init__(self, broker, port, topic, username, password):
|
||||||
self.broker = broker
|
self.broker = broker
|
||||||
self.port = port
|
self.port = port
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
self.client = mqtt.Client()
|
self.username = username
|
||||||
|
self.password = password
|
||||||
|
self.client = mqtt.Client(client_id=device_name)
|
||||||
self.client.on_connect = self.on_connect
|
self.client.on_connect = self.on_connect
|
||||||
self.client.on_message = self.on_message
|
self.client.on_message = self.on_message
|
||||||
|
|
||||||
def on_connect(self, client, userdata, flags, rc):
|
def on_connect(self, client, userdata, flags, rc):
|
||||||
self.client.subscribe(self.topic)
|
self.client.subscribe(self.topic)
|
||||||
client.publish('success', payload='成功订阅lot_mqtt' + str(time.time()), qos=0)
|
self.client.publish('success', payload='成功订阅lot_mqtt' + str(time.time()), qos=0)
|
||||||
|
|
||||||
def on_message(self, client, userdata, msg):
|
def on_message(self, client, userdata, msg):
|
||||||
if not valid(msg, client):
|
if not valid(msg, client):
|
||||||
|
@ -68,14 +74,20 @@ class MQTTClient:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
self.client.username_pw_set(self.username, self.password)
|
||||||
self.client.connect(self.broker, self.port)
|
self.client.connect(self.broker, self.port)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
MQTT = MQTTClient('192.168.1.27', 1883, 'lot_mqtt')
|
broker = config.get("broker", "host")
|
||||||
|
port = config.get("broker", "port")
|
||||||
|
topic = config.get("topic", "name")
|
||||||
|
username = config.get("security", "username")
|
||||||
|
password = config.get("security", "password")
|
||||||
|
MQTT = MQTTClient(broker, port, topic, username, password)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
MQTT.start()
|
MQTT.start()
|
||||||
MQTT.client.loop_forever()
|
MQTT.client.loop_forever()
|
||||||
except:
|
except Exception as e:
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
RTSP_URL="rtsp://admin:123456@192.168.0.123:554/mpeg4" # RTSP流的URL
|
RTSP_URL="rtsp://admin:123456@192.168.0.123:554/mpeg4" # RTSP流的URL
|
||||||
OUTPUT_DIR="/home/pi/mp4" # 录像文件保存的目录
|
OUTPUT_DIR="/home/pi/mp4" # 录像文件保存的目录
|
||||||
RECORD_DURATION=900 # 录像时长(秒)
|
RECORD_DURATION=900 # 录像时长(秒)
|
||||||
# 获取当前时间
|
# 获取当前时间
|
||||||
CURRENT_TIME=$(date +"%Y-%m-%d_%H-%M-%S")
|
CURRENT_TIME=$(date +"%Y-%m-%d_%H-%M-%S")
|
||||||
# 构建录像文件名
|
# 构建录像文件名
|
||||||
OUTPUT_FILE="${OUTPUT_DIR}/${CURRENT_TIME}.mp4"
|
OUTPUT_FILE="${OUTPUT_DIR}/${CURRENT_TIME}.mp4"
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
device_name=`cat /home/pi/device_name`
|
device_name=`cat /home/pi/device_name`
|
||||||
/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c copy -preset fast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://192.168.1.27:554/live/$device_name
|
/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c copy -preset fast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://47.108.186.87:554/live/$device_name
|
|
@ -0,0 +1,11 @@
|
||||||
|
[broker]
|
||||||
|
host=ceshi-mqtt.lihaink.cn
|
||||||
|
port=1883
|
||||||
|
[topic]
|
||||||
|
name=lot_mqtt
|
||||||
|
[security]
|
||||||
|
username=demo
|
||||||
|
password=123456
|
||||||
|
[record]
|
||||||
|
post_record_list_url=http://shop.lihaink.cn/api/index/file_list
|
||||||
|
post_record_url=https://shop.lihaink.cn/api/index/upload
|
|
@ -1,18 +1,23 @@
|
||||||
|
import configparser
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
from device import device_name
|
||||||
from tool import *
|
from tool import *
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('/home/pi/lot_manager/conf/main/config.conf')
|
||||||
times = 6
|
times = 6
|
||||||
|
|
||||||
|
|
||||||
class DataUploadClient:
|
class DataUploadClient:
|
||||||
def __init__(self, broker, port, topic):
|
def __init__(self, broker, port, topic, username, password):
|
||||||
self.broker = broker
|
self.broker = broker
|
||||||
self.port = port
|
self.port = port
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
self.client = mqtt.Client()
|
self.username = username
|
||||||
|
self.password = password
|
||||||
|
self.client = mqtt.Client(client_id=device_name)
|
||||||
self.client.on_connect = self.on_connect
|
self.client.on_connect = self.on_connect
|
||||||
self.client.on_disconnect = self.on_disconnect
|
self.client.on_disconnect = self.on_disconnect
|
||||||
|
|
||||||
|
@ -27,11 +32,17 @@ class DataUploadClient:
|
||||||
subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
|
subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
self.client.username_pw_set(self.username, self.password)
|
||||||
self.client.connect(self.broker, self.port)
|
self.client.connect(self.broker, self.port)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
MQTT = DataUploadClient('192.168.1.27', 1883, 'lot_mqtt')
|
broker = config.get("broker", "host")
|
||||||
|
port = config.get("broker", "port")
|
||||||
|
topic = config.get("topic", "name")
|
||||||
|
username = config.get("security", "username")
|
||||||
|
password = config.get("security", "password")
|
||||||
|
MQTT = DataUploadClient(broker, port, topic, username, password)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
MQTT.start()
|
MQTT.start()
|
||||||
|
|
|
@ -222,5 +222,7 @@ def test():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
# 正式环境
|
||||||
# local()
|
# local()
|
||||||
|
# 测试环境
|
||||||
test()
|
test()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import configparser
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
@ -11,7 +12,7 @@ from db.models.lot_data_model import LOT_DATA
|
||||||
from device import device_name
|
from device import device_name
|
||||||
|
|
||||||
|
|
||||||
def t():
|
def get_data():
|
||||||
temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
|
temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
|
||||||
co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
|
co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
|
||||||
pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
|
pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
|
||||||
|
@ -228,16 +229,23 @@ def p():
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
|
# 测试
|
||||||
threading.Thread(target=p).start()
|
threading.Thread(target=p).start()
|
||||||
|
# 正式环境
|
||||||
|
# threading.Thread(target=get_data).start()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('/home/pi/lot_manager/conf/main/config.conf')
|
||||||
|
broker = config.get("broker", "host")
|
||||||
|
port = config.get("broker", "port")
|
||||||
|
username = config.get("security", "username")
|
||||||
|
password = config.get("security", "password")
|
||||||
client = mqtt.Client(client_id=device_name)
|
client = mqtt.Client(client_id=device_name)
|
||||||
client.username_pw_set("demo", "123456")
|
client.username_pw_set(username, password)
|
||||||
# Specify callback function
|
# Specify callback function
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
# Establish a connection
|
# Establish a connection
|
||||||
# ceshi-mqtt.lihaink.cn
|
client.connect(broker, port)
|
||||||
client.connect('192.168.1.27', 1883)
|
|
||||||
# Publish a message
|
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
|
|
9
tool.py
9
tool.py
|
@ -1,3 +1,4 @@
|
||||||
|
import configparser
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -5,6 +6,8 @@ import subprocess
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
mp4_path = '/home/pi/mp4'
|
mp4_path = '/home/pi/mp4'
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('/home/pi/lot_manager/conf/main/config.conf')
|
||||||
|
|
||||||
|
|
||||||
def push_stream(client):
|
def push_stream(client):
|
||||||
|
@ -69,7 +72,8 @@ def get_list_record():
|
||||||
data = {
|
data = {
|
||||||
"data": os.listdir(mp4_path)
|
"data": os.listdir(mp4_path)
|
||||||
}
|
}
|
||||||
requests.post("http://shop.lihaink.cn/api/index/file_list", json=data)
|
post_record_list_url = config.get("record", "post_record_list_url")
|
||||||
|
requests.post(post_record_list_url, json=data)
|
||||||
|
|
||||||
|
|
||||||
def get_record(msg, client):
|
def get_record(msg, client):
|
||||||
|
@ -77,5 +81,6 @@ def get_record(msg, client):
|
||||||
if filename is None or filename == '':
|
if filename is None or filename == '':
|
||||||
client.publish('error', payload='没有该文件', qos=0)
|
client.publish('error', payload='没有该文件', qos=0)
|
||||||
return
|
return
|
||||||
|
post_record_url = config.get("record", "post_record_url")
|
||||||
files = {filename: open(os.path.join(mp4_path, filename), 'rb'), "Content-Type": "application/octet-stream"}
|
files = {filename: open(os.path.join(mp4_path, filename), 'rb'), "Content-Type": "application/octet-stream"}
|
||||||
requests.post("https://shop.lihaink.cn/api/index/upload", files=files)
|
requests.post(post_record_url, files=files)
|
||||||
|
|
Loading…
Reference in New Issue