2023-12-29 17:59:05 +08:00
|
|
|
import asyncio
|
2023-12-21 09:13:42 +08:00
|
|
|
import json
|
2024-01-02 11:39:09 +08:00
|
|
|
|
2023-12-21 09:13:42 +08:00
|
|
|
|
|
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2023-12-28 09:37:37 +08:00
|
|
|
|
2023-12-21 09:13:42 +08:00
|
|
|
class MQTTClient:
|
|
|
|
def __init__(self, broker, port, topic, username, password):
|
|
|
|
self.broker = broker
|
|
|
|
self.port = port
|
|
|
|
self.topic = topic
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
# 千万不要指定client_id 不然死翘翘
|
|
|
|
self.client = mqtt.Client()
|
|
|
|
self.client.username_pw_set(self.username, self.password)
|
|
|
|
|
2024-01-02 11:37:17 +08:00
|
|
|
def push(self):
|
2023-12-29 09:56:44 +08:00
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "open_led"}, ensure_ascii=False),
|
|
|
|
qos=0)
|
2023-12-28 09:37:37 +08:00
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "push_stream"}, ensure_ascii=False),
|
2023-12-21 09:13:42 +08:00
|
|
|
qos=0)
|
|
|
|
|
2024-01-02 11:37:17 +08:00
|
|
|
def close(self):
|
2023-12-28 09:37:37 +08:00
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "close_stream"}, ensure_ascii=False),
|
2023-12-21 09:13:42 +08:00
|
|
|
qos=0)
|
2023-12-29 09:56:44 +08:00
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "close_led"}, ensure_ascii=False),
|
|
|
|
qos=0)
|
2023-12-21 09:13:42 +08:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.client.connect(self.broker, self.port)
|
|
|
|
|
|
|
|
|
2023-12-29 17:59:05 +08:00
|
|
|
user_sched = {}
|
|
|
|
|
|
|
|
|
|
|
|
async def close(username, device):
|
|
|
|
# 倒计时600秒
|
2024-01-02 12:01:00 +08:00
|
|
|
await asyncio.sleep(40)
|
2023-12-29 17:59:05 +08:00
|
|
|
user_sched.pop(username)
|
2024-01-02 12:01:24 +08:00
|
|
|
print(username + "结束推流")
|
2023-12-29 17:59:05 +08:00
|
|
|
close_stream(username, device)
|
|
|
|
|
|
|
|
|
|
|
|
async def task_start(username, device):
|
2024-01-02 12:01:24 +08:00
|
|
|
print(username + "开始推流")
|
2023-12-29 17:59:05 +08:00
|
|
|
push_stream(username, device)
|
|
|
|
task = asyncio.create_task(close(username, device))
|
|
|
|
user_sched[username] = task
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/video/{username}/{device}")
|
|
|
|
async def startup(username, device):
|
|
|
|
try:
|
|
|
|
if username not in user_sched:
|
|
|
|
await task_start(username, device)
|
|
|
|
else:
|
|
|
|
t = user_sched[username]
|
|
|
|
# 取消重来
|
|
|
|
t.cancel()
|
2024-01-02 12:01:00 +08:00
|
|
|
await task_start(username, device)
|
2023-12-29 17:59:05 +08:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-12-28 09:37:37 +08:00
|
|
|
def push_stream(username, device):
|
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
2023-12-21 09:13:42 +08:00
|
|
|
try:
|
|
|
|
MQTT.start()
|
|
|
|
MQTT.push()
|
|
|
|
except Exception as e:
|
2023-12-21 09:14:35 +08:00
|
|
|
# print(e)
|
2023-12-21 09:13:42 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-12-28 09:37:37 +08:00
|
|
|
def close_stream(username, device):
|
2023-12-21 09:13:42 +08:00
|
|
|
try:
|
2023-12-28 09:37:37 +08:00
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
2023-12-21 09:13:42 +08:00
|
|
|
MQTT.start()
|
|
|
|
MQTT.close()
|
|
|
|
except Exception as e:
|
2023-12-21 09:14:35 +08:00
|
|
|
# print(e)
|
2023-12-21 09:13:42 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-12-28 09:37:37 +08:00
|
|
|
broker = 'mqtt.lihaink.cn'
|
|
|
|
port = 1883
|
2024-01-02 12:01:00 +08:00
|
|
|
uvicorn.run(app, host="127.0.0.1", port=8001)
|