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
|
2024-01-03 14:01:50 +08:00
|
|
|
from pydantic import BaseModel
|
2024-01-03 18:28:27 +08:00
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
2024-01-03 14:01:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseResponse(BaseModel):
|
|
|
|
code: int = 200
|
2024-01-03 14:06:26 +08:00
|
|
|
msg: str = "success"
|
2024-01-03 14:01:50 +08:00
|
|
|
|
2023-12-21 09:13:42 +08:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2024-01-03 18:28:27 +08:00
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
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-03 18:28:27 +08:00
|
|
|
await asyncio.sleep(35)
|
|
|
|
print(username + "结束推流")
|
2024-01-03 15:07:43 +08:00
|
|
|
user_sched.pop(username)
|
2023-12-29 17:59:05 +08:00
|
|
|
close_stream(username, device)
|
|
|
|
|
|
|
|
|
|
|
|
async def task_start(username, device):
|
2024-01-03 18:28:27 +08:00
|
|
|
print(username + "创建结束推流定时任务")
|
2023-12-29 17:59:05 +08:00
|
|
|
task = asyncio.create_task(close(username, device))
|
|
|
|
user_sched[username] = task
|
|
|
|
|
|
|
|
|
2024-01-03 15:07:43 +08:00
|
|
|
@app.get("/start/video/{username}/{device}")
|
|
|
|
async def start(username, device):
|
2023-12-29 17:59:05 +08:00
|
|
|
try:
|
2024-01-03 15:07:43 +08:00
|
|
|
# 如果定时任务不存在,那么直接进行推流。当用户退出界面时,创建任务
|
2023-12-29 17:59:05 +08:00
|
|
|
if username not in user_sched:
|
2024-01-03 18:28:27 +08:00
|
|
|
print(username + "开始推流")
|
2024-01-03 15:07:43 +08:00
|
|
|
push_stream(username, device)
|
|
|
|
# 如果定时任务存在,那么取消上次的任务。当用户退出界面时,创建任务
|
2023-12-29 17:59:05 +08:00
|
|
|
else:
|
|
|
|
t = user_sched[username]
|
2024-01-03 15:07:43 +08:00
|
|
|
# 取消任务
|
|
|
|
if t is not None:
|
2024-01-03 18:28:27 +08:00
|
|
|
print(username + "取消定时任务")
|
2024-01-03 15:07:43 +08:00
|
|
|
t.cancel()
|
|
|
|
return BaseResponse(code=200, msg="success")
|
|
|
|
except Exception as e:
|
|
|
|
return BaseResponse(code=500, msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/stop/video/{username}/{device}")
|
|
|
|
async def stop(username, device):
|
|
|
|
try:
|
|
|
|
await task_start(username, device)
|
2024-01-03 14:01:50 +08:00
|
|
|
return BaseResponse(code=200, msg="success")
|
|
|
|
except Exception as e:
|
2024-01-03 14:25:39 +08:00
|
|
|
return BaseResponse(code=500, msg=str(e))
|
2023-12-29 17:59:05 +08:00
|
|
|
|
|
|
|
|
2023-12-28 09:37:37 +08:00
|
|
|
def push_stream(username, device):
|
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
2024-01-03 14:05:55 +08:00
|
|
|
MQTT.start()
|
|
|
|
MQTT.push()
|
2023-12-21 09:13:42 +08:00
|
|
|
|
|
|
|
|
2023-12-28 09:37:37 +08:00
|
|
|
def close_stream(username, device):
|
2024-01-03 14:05:55 +08:00
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
|
|
|
MQTT.start()
|
|
|
|
MQTT.close()
|
2023-12-21 09:13:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-12-28 09:37:37 +08:00
|
|
|
broker = 'mqtt.lihaink.cn'
|
|
|
|
port = 1883
|
2024-01-03 18:28:27 +08:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|