|
|
|
import os
|
|
|
|
from . import api
|
|
|
|
from web.common.utils import login_required
|
|
|
|
from quart import jsonify, request,send_file
|
|
|
|
from core.DBManager import mDBM
|
|
|
|
|
|
|
|
@api.route('/warn/search_warn',methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
async def warn_search(): #查询报警
|
|
|
|
#获取查询参数
|
|
|
|
json_data = await request.get_json()
|
|
|
|
s_count = json_data.get('s_count','')
|
|
|
|
e_count = json_data.get('e_count','')
|
|
|
|
model_name = json_data.get('model_name','')
|
|
|
|
channel_name = json_data.get('channel_name','')
|
|
|
|
start_time = json_data.get('start_time','')
|
|
|
|
end_time = json_data.get('end_time','')
|
|
|
|
|
|
|
|
# 动态拼接 SQL 语句
|
|
|
|
sql = "SELECT t1.*,t2.channel_name FROM warn t1 LEFT JOIN channel t2 ON t1.channel_id = t2.element_id WHERE 1=1"
|
|
|
|
|
|
|
|
if model_name:
|
|
|
|
sql += f" AND t1.model_name = {model_name}"
|
|
|
|
if channel_name:
|
|
|
|
sql += f" AND t2.channel_name = {channel_name}"
|
|
|
|
if start_time and end_time:
|
|
|
|
sql += f" AND t1.creat_time BETWEEN {start_time} AND {end_time}"
|
|
|
|
|
|
|
|
# 增加倒序排列和分页
|
|
|
|
sql += f" ORDER BY t1.creat_time DESC LIMIT {e_count} OFFSET {s_count}"
|
|
|
|
|
|
|
|
# 使用SQLAlchemy执行查询
|
|
|
|
try:
|
|
|
|
print(sql)
|
|
|
|
data = mDBM.do_select(sql)
|
|
|
|
# 将数据转换为JSON格式返回给前端
|
|
|
|
warn_list = [{"ID": warn[0], "model_name": warn[1], "video_path": warn[2], "img_path": warn[3],
|
|
|
|
"creat_time": warn[4], "channel_name": warn[6]} for warn in data]
|
|
|
|
return jsonify(warn_list)
|
|
|
|
except Exception as e:
|
|
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
@api.route('/warn/warn_img')
|
|
|
|
@login_required
|
|
|
|
async def warn_img():
|
|
|
|
# 获取图片路径参数
|
|
|
|
image_path = request.args.get('path')
|
|
|
|
if image_path and os.path.exists(image_path):
|
|
|
|
# 返回图片文件
|
|
|
|
return await send_file(image_path)
|
|
|
|
else:
|
|
|
|
# 图片不存在时,返回 404
|
|
|
|
return 'Image not found', 404
|
|
|
|
|
|
|
|
@api.route('/warn/warn_video')
|
|
|
|
@login_required
|
|
|
|
async def warn_video():
|
|
|
|
# 获取视频路径参数
|
|
|
|
video_path = request.args.get('path')
|
|
|
|
if video_path and os.path.exists(video_path):
|
|
|
|
# 返回视频文件流
|
|
|
|
return await send_file(video_path, as_attachment=True)
|
|
|
|
else:
|
|
|
|
# 视频文件不存在时,返回 404
|
|
|
|
return 'Video not found', 404
|
|
|
|
|
|
|
|
@api.route('/warn/warn_del',methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
async def warn_del():
|
|
|
|
# 获取请求体中的报警ID数组
|
|
|
|
data = await request.get_json()
|
|
|
|
alarm_ids = data.get('alarmIds')
|
|
|
|
|
|
|
|
if not alarm_ids:
|
|
|
|
return jsonify({'success': False, 'message': '没有提供报警ID'}), 400
|
|
|
|
|
|
|
|
try:
|
|
|
|
# 根据报警ID进行删除数据的操作,这里是假设数据库删除操作
|
|
|
|
# 例如:delete_from_database(alarm_ids)
|
|
|
|
|
|
|
|
# 模拟删除成功
|
|
|
|
success = True
|
|
|
|
|
|
|
|
if success:
|
|
|
|
return jsonify({'success': True, 'message': '删除成功'})
|
|
|
|
else:
|
|
|
|
return jsonify({'success': False, 'message': '删除失败'})
|
|
|
|
except Exception as e:
|
|
|
|
return jsonify({'success': False, 'message': f'发生异常: {str(e)}'}), 500
|