diff --git a/MQTT.py b/MQTT.py index 684467f..539b108 100644 --- a/MQTT.py +++ b/MQTT.py @@ -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) diff --git a/bash/record.sh b/bash/record.sh index e435000..732f273 100755 --- a/bash/record.sh +++ b/bash/record.sh @@ -3,7 +3,7 @@ RTSP_URL="rtsp://admin:123456@192.168.0.123:554/mpeg4" # RTSP流的URL OUTPUT_DIR="/home/pi/mp4" # 录像文件保存的目录 RECORD_DURATION=900 # 录像时长(秒) - # 获取当前时间 +# 获取当前时间 CURRENT_TIME=$(date +"%Y-%m-%d_%H-%M-%S") # 构建录像文件名 OUTPUT_FILE="${OUTPUT_DIR}/${CURRENT_TIME}.mp4" diff --git a/bash/stream.sh b/bash/stream.sh index ab233a7..5c14dd9 100644 --- a/bash/stream.sh +++ b/bash/stream.sh @@ -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 \ No newline at end of file +/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 \ No newline at end of file diff --git a/conf/main/config.conf b/conf/main/config.conf new file mode 100644 index 0000000..4a00ca8 --- /dev/null +++ b/conf/main/config.conf @@ -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 diff --git a/data_upload.py b/data_upload.py index 78e8360..2aa7127 100644 --- a/data_upload.py +++ b/data_upload.py @@ -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() diff --git a/sensor_to_local.py b/sensor_to_local.py index 56cafe6..67b804b 100755 --- a/sensor_to_local.py +++ b/sensor_to_local.py @@ -222,5 +222,7 @@ def test(): if __name__ == '__main__': + # 正式环境 # local() + # 测试环境 test() diff --git a/sensor_to_server.py b/sensor_to_server.py index fc7026a..0ff78ce 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -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() diff --git a/tool.py b/tool.py index 01b06ec..97fa736 100755 --- a/tool.py +++ b/tool.py @@ -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)