This commit is contained in:
xyj 2023-12-11 14:11:12 +08:00
parent 99526ffa7c
commit 63cf55205d
8 changed files with 67 additions and 18 deletions

22
MQTT.py
View File

@ -4,6 +4,10 @@ import paho.mqtt.client as mqtt
from device import device_name
from tool import *
import configparser
config = configparser.ConfigParser()
config.read('/home/pi/lot_manager/conf/main/config.conf')
def valid(msg, client):
@ -20,17 +24,19 @@ def valid(msg, client):
class MQTTClient:
def __init__(self, broker, port, topic):
def __init__(self, broker, port, topic, username, password):
self.broker = broker
self.port = port
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_message = self.on_message
def on_connect(self, client, userdata, flags, rc):
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):
if not valid(msg, client):
@ -68,14 +74,20 @@ class MQTTClient:
pass
def start(self):
self.client.username_pw_set(self.username, self.password)
self.client.connect(self.broker, self.port)
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:
try:
MQTT.start()
MQTT.client.loop_forever()
except:
except Exception as e:
time.sleep(30)

View File

@ -1,3 +1,3 @@
#!/bin/bash
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

11
conf/main/config.conf Normal file
View File

@ -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

View File

@ -1,18 +1,23 @@
import configparser
import time
import paho.mqtt.client as mqtt
from device import device_name
from tool import *
config = configparser.ConfigParser()
config.read('/home/pi/lot_manager/conf/main/config.conf')
times = 6
class DataUploadClient:
def __init__(self, broker, port, topic):
def __init__(self, broker, port, topic, username, password):
self.broker = broker
self.port = port
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_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)
def start(self):
self.client.username_pw_set(self.username, self.password)
self.client.connect(self.broker, self.port)
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:
try:
MQTT.start()

View File

@ -222,5 +222,7 @@ def test():
if __name__ == '__main__':
# 正式环境
# local()
# 测试环境
test()

View File

@ -1,3 +1,4 @@
import configparser
import json
import os
import threading
@ -11,7 +12,7 @@ from db.models.lot_data_model import LOT_DATA
from device import device_name
def t():
def get_data():
temp_send = '06 03 01 F4 00 02 85 B2 ' # 温湿度查询指令
co2_send = '06 03 01 F7 00 02 75 B2 ' # 二氧化碳查询指令
pressure_send = '06 03 01 F9 00 02 14 71 ' # 气压查询指令
@ -228,16 +229,23 @@ def p():
def on_connect(client, userdata, flags, rc):
if rc == 0:
# 测试
threading.Thread(target=p).start()
# 正式环境
# threading.Thread(target=get_data).start()
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.username_pw_set("demo", "123456")
client.username_pw_set(username, password)
# Specify callback function
client.on_connect = on_connect
# Establish a connection
# ceshi-mqtt.lihaink.cn
client.connect('192.168.1.27', 1883)
# Publish a message
client.connect(broker, port)
client.loop_forever()

View File

@ -1,3 +1,4 @@
import configparser
import json
import os
import subprocess
@ -5,6 +6,8 @@ import subprocess
import requests
mp4_path = '/home/pi/mp4'
config = configparser.ConfigParser()
config.read('/home/pi/lot_manager/conf/main/config.conf')
def push_stream(client):
@ -69,7 +72,8 @@ def get_list_record():
data = {
"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):
@ -77,5 +81,6 @@ def get_record(msg, client):
if filename is None or filename == '':
client.publish('error', payload='没有该文件', qos=0)
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"}
requests.post("https://shop.lihaink.cn/api/index/upload", files=files)
requests.post(post_record_url, files=files)