2024-01-11 13:09:45 +08:00
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI, WebSocket
|
2024-01-09 15:49:48 +08:00
|
|
|
|
|
|
|
|
|
import requests
|
2024-01-03 18:28:27 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
2024-01-11 13:09:45 +08:00
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
2024-01-03 18:28:27 +08:00
|
|
|
|
|
|
|
|
|
class BaseResponse(BaseModel):
|
|
|
|
|
code: int = 200
|
|
|
|
|
msg: str = "success"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def push(self):
|
|
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "open_led"}, ensure_ascii=False),
|
|
|
|
|
qos=0)
|
|
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "push_stream"}, ensure_ascii=False),
|
|
|
|
|
qos=0)
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "close_stream"}, ensure_ascii=False),
|
|
|
|
|
qos=0)
|
|
|
|
|
self.client.publish(self.topic, payload=json.dumps({"msg": "close_led"}, ensure_ascii=False),
|
|
|
|
|
qos=0)
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self.client.connect(self.broker, self.port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def close(username, device):
|
|
|
|
|
# 倒计时600秒
|
2024-01-10 15:18:55 +08:00
|
|
|
|
await asyncio.sleep(30)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
print(username + "结束推流")
|
|
|
|
|
user_sched.pop(username)
|
|
|
|
|
close_stream(username, device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def task_start(username, device):
|
|
|
|
|
print(username + "创建结束推流定时任务")
|
2024-01-05 10:01:04 +08:00
|
|
|
|
task = asyncio.create_task(close(username, device))
|
2024-01-03 18:28:27 +08:00
|
|
|
|
user_sched[username] = task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def start(username, device):
|
|
|
|
|
try:
|
|
|
|
|
# 如果定时任务不存在,那么直接进行推流。当用户退出界面时,创建任务
|
|
|
|
|
if username not in user_sched:
|
2024-01-09 15:49:48 +08:00
|
|
|
|
res = requests.get(rtsp_MediaInfo + device)
|
|
|
|
|
if res.status_code != 200:
|
|
|
|
|
return BaseResponse(code=500, msg="ZLMediakit Server Error!").json()
|
|
|
|
|
# 转换为json
|
|
|
|
|
res = res.json()
|
|
|
|
|
if 'code' in res and res['code'] == 0:
|
2024-01-11 13:09:45 +08:00
|
|
|
|
return BaseResponse(code=200, msg=username + ",Do not repeat push stream").json()
|
2024-01-09 15:49:48 +08:00
|
|
|
|
# 开始推流
|
|
|
|
|
get_times = 50
|
|
|
|
|
# 启动推流程序
|
2024-01-11 13:09:45 +08:00
|
|
|
|
print(username + "开始推流")
|
2024-01-03 18:28:27 +08:00
|
|
|
|
push_stream(username, device)
|
2024-01-09 15:49:48 +08:00
|
|
|
|
# 查看请求 ,ZLMediakit是否已经生成了mp4文件,生成了就给前端返回
|
|
|
|
|
while True:
|
|
|
|
|
res = requests.get(rtsp_MediaInfo + device)
|
|
|
|
|
if res.status_code != 200:
|
|
|
|
|
return BaseResponse(code=200, msg="ZLMediakit Server Error!").json()
|
|
|
|
|
# 转换为json
|
|
|
|
|
res = res.json()
|
|
|
|
|
if 'code' in res and res['code'] == 0:
|
|
|
|
|
# code == 0 说明已经启动成功
|
|
|
|
|
break
|
|
|
|
|
# 如果都没有code,那就出错了
|
|
|
|
|
if 'code' not in res:
|
|
|
|
|
return BaseResponse(code=500, msg="ZLMediakit Server Error!").json()
|
|
|
|
|
get_times -= 1
|
|
|
|
|
if get_times == 0:
|
|
|
|
|
# 请求次数达到50次就退了吧
|
2024-01-11 13:09:45 +08:00
|
|
|
|
return BaseResponse(code=500, msg="ZLMediakit Server Error!").json()
|
2024-01-09 15:49:48 +08:00
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
return BaseResponse(code=200, msg="success").json()
|
2024-01-03 18:28:27 +08:00
|
|
|
|
# 如果定时任务存在,那么取消上次的任务。当用户退出界面时,创建任务
|
|
|
|
|
else:
|
|
|
|
|
t = user_sched[username]
|
|
|
|
|
# 取消任务
|
|
|
|
|
if t is not None:
|
|
|
|
|
print(username + "取消定时任务")
|
|
|
|
|
t.cancel()
|
2024-01-09 15:49:48 +08:00
|
|
|
|
return BaseResponse(code=200, msg="success").json()
|
|
|
|
|
else:
|
|
|
|
|
BaseResponse(code=500, msg="Internal Error").json()
|
2024-01-03 18:28:27 +08:00
|
|
|
|
except Exception as e:
|
2024-01-04 18:32:36 +08:00
|
|
|
|
return BaseResponse(code=500, msg=str(e)).json()
|
2024-01-03 18:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def stop(username, device):
|
|
|
|
|
try:
|
|
|
|
|
await task_start(username, device)
|
2024-01-04 18:32:36 +08:00
|
|
|
|
return BaseResponse(code=200, msg="success").json()
|
2024-01-03 18:28:27 +08:00
|
|
|
|
except Exception as e:
|
2024-01-04 18:32:36 +08:00
|
|
|
|
return BaseResponse(code=500, msg=str(e)).json()
|
2024-01-03 18:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def push_stream(username, device):
|
|
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
|
|
|
|
MQTT.start()
|
|
|
|
|
MQTT.push()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def close_stream(username, device):
|
|
|
|
|
MQTT = MQTTClient(broker, port, device, username, username)
|
|
|
|
|
MQTT.start()
|
|
|
|
|
MQTT.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 定义函数A
|
2024-01-11 13:09:45 +08:00
|
|
|
|
async def function_A(data):
|
2024-01-04 18:32:36 +08:00
|
|
|
|
data = json.loads(data)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
username = data['username']
|
|
|
|
|
device = data['device']
|
2024-01-04 18:32:36 +08:00
|
|
|
|
return await start(username, device)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
|
2024-01-03 19:10:56 +08:00
|
|
|
|
|
2024-01-03 18:28:27 +08:00
|
|
|
|
# 定义函数B
|
2024-01-11 13:09:45 +08:00
|
|
|
|
async def function_B(data):
|
2024-01-04 18:32:36 +08:00
|
|
|
|
data = json.loads(data)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
username = data['username']
|
|
|
|
|
device = data['device']
|
2024-01-04 18:32:36 +08:00
|
|
|
|
await stop(username, device)
|
|
|
|
|
|
|
|
|
|
|
2024-01-06 17:46:53 +08:00
|
|
|
|
class Scene:
|
|
|
|
|
def __init__(self, user):
|
|
|
|
|
self.user = user
|
|
|
|
|
self.active = False
|
|
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
|
self.active = True
|
|
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
|
self.active = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppScene(Scene):
|
|
|
|
|
def __init__(self, user):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.name = "app"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebScene(Scene):
|
|
|
|
|
def __init__(self, user):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.name = "web"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScreenScene(Scene):
|
|
|
|
|
def __init__(self, user):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.name = "screen"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SceneManager:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.users = {}
|
|
|
|
|
|
|
|
|
|
def add_user(self, user):
|
|
|
|
|
# 上锁
|
|
|
|
|
if user not in self.users:
|
|
|
|
|
self.users[user] = {"app": AppScene(user), "web": WebScene(user), "screen": ScreenScene(user)}
|
|
|
|
|
|
|
|
|
|
def get_scene(self, user, scene_type):
|
|
|
|
|
return self.users[user].get(scene_type)
|
|
|
|
|
|
|
|
|
|
|
2024-01-11 13:09:45 +08:00
|
|
|
|
async def on_connect(ws: WebSocket):
|
|
|
|
|
await ws.send_text(json.dumps({"code": 201, "msg": "socket已连接,等待接收消息"}, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def on_disconnect(the_user_scene, user_app_scene, user_web_scene, user_screen_scene, message):
|
|
|
|
|
if the_user_scene is not None:
|
|
|
|
|
the_user_scene.turn_off()
|
|
|
|
|
# 查看该用户的所有场景是否在线,只有都不在线就关闭推流
|
|
|
|
|
if user_app_scene is not None and user_app_scene is not None and user_app_scene is not None:
|
|
|
|
|
if user_app_scene.active is False and user_web_scene.active is False and user_screen_scene.active is False:
|
|
|
|
|
print("关闭场景" + the_user_scene.name)
|
|
|
|
|
await function_B(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.websocket("/")
|
|
|
|
|
async def wsk(ws: WebSocket):
|
2024-01-06 16:29:19 +08:00
|
|
|
|
the_user_scene = None
|
2024-01-06 17:46:53 +08:00
|
|
|
|
user_app_scene = None
|
|
|
|
|
user_web_scene = None
|
|
|
|
|
user_screen_scene = None
|
2024-01-11 13:09:45 +08:00
|
|
|
|
username = ""
|
|
|
|
|
await ws.accept()
|
|
|
|
|
await on_connect(ws)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
try:
|
2024-01-11 13:09:45 +08:00
|
|
|
|
while True:
|
|
|
|
|
message = await ws.receive_text()
|
|
|
|
|
await ws.send_text(json.dumps({"code": 202, "msg": "socket已收到消息,等待处理"}, ensure_ascii=False))
|
|
|
|
|
user_msg[ws] = message
|
2024-01-06 15:15:52 +08:00
|
|
|
|
data = json.loads(message)
|
2024-01-06 16:29:19 +08:00
|
|
|
|
username = data['username']
|
2024-01-06 17:46:53 +08:00
|
|
|
|
scene_name = data[SCENE_NAME]
|
|
|
|
|
scene_manager.add_user(username)
|
|
|
|
|
the_user_scene = scene_manager.get_scene(username, scene_name)
|
|
|
|
|
if the_user_scene.active is True:
|
2024-01-11 13:09:45 +08:00
|
|
|
|
await ws.send_text(
|
2024-01-06 17:46:53 +08:00
|
|
|
|
json.dumps({"code": 200,
|
2024-01-11 13:09:45 +08:00
|
|
|
|
"msg": the_user_scene.user + "的" + the_user_scene.name + "该场景已经开启了,不要再请求我了,请等待一会!"},
|
2024-01-06 16:29:19 +08:00
|
|
|
|
ensure_ascii=False))
|
|
|
|
|
continue
|
2024-01-06 17:46:53 +08:00
|
|
|
|
user_app_scene = scene_manager.get_scene(username, "app")
|
|
|
|
|
user_web_scene = scene_manager.get_scene(username, "web")
|
|
|
|
|
user_screen_scene = scene_manager.get_scene(username, "screen")
|
|
|
|
|
if user_app_scene.active is True or user_web_scene.active is True or user_screen_scene.active is True:
|
|
|
|
|
the_user_scene.turn_on()
|
2024-01-11 13:09:45 +08:00
|
|
|
|
await ws.send_text(
|
|
|
|
|
json.dumps({"code": 200, "msg": "该用户已经有其他场景开启了,请等待一会!不需要再推流了!"},
|
|
|
|
|
ensure_ascii=False))
|
2024-01-06 16:29:19 +08:00
|
|
|
|
continue
|
|
|
|
|
# 如果该用户的场景没有开启,那么进行开启
|
2024-01-11 13:09:45 +08:00
|
|
|
|
print(username + "开启场景" + the_user_scene.name)
|
2024-01-06 17:46:53 +08:00
|
|
|
|
the_user_scene.turn_on()
|
2024-01-11 13:09:45 +08:00
|
|
|
|
response = await function_A(message)
|
2024-01-09 15:49:48 +08:00
|
|
|
|
# 当接收到消息时调用函数A
|
2024-01-11 13:09:45 +08:00
|
|
|
|
await ws.send_text(response)
|
2024-01-03 18:28:27 +08:00
|
|
|
|
except Exception as e:
|
2024-01-11 13:09:45 +08:00
|
|
|
|
print(f"{username}的{the_user_scene.name}场景的连接已断开,异常消息或代码:{e}")
|
|
|
|
|
await on_disconnect(the_user_scene, user_app_scene, user_web_scene, user_screen_scene, user_msg[ws])
|
2024-01-03 18:28:27 +08:00
|
|
|
|
|
2024-01-06 15:15:52 +08:00
|
|
|
|
if __name__ == '__main__':
|
2024-01-06 17:46:53 +08:00
|
|
|
|
broker = 'mqtt.lihaink.cn'
|
|
|
|
|
port = 1883
|
2024-01-09 15:49:48 +08:00
|
|
|
|
rtsp_MediaInfo = "http://rtsp.lihaink.cn/index/api/getMediaInfo?secret=YwDtp2llj80HA19JhMXL4Po99nsMAyNT&schema=rtsp&vhost=__defaultVhost__&app=live&stream="
|
2024-01-06 17:46:53 +08:00
|
|
|
|
SCENE_NAME = "scene"
|
|
|
|
|
user_sched = {}
|
|
|
|
|
user_msg = {}
|
|
|
|
|
user_scene = []
|
|
|
|
|
scene_manager = SceneManager()
|
2024-01-11 14:30:34 +08:00
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8765)
|