avcon.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends,Query,HTTPException
  4. from database import get_db
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy import case,or_
  7. from sqlalchemy import text
  8. from utils import *
  9. from utils.ry_system_util import *
  10. from utils.video_util import *
  11. from common.security import valid_access_token
  12. from fastapi.responses import JSONResponse
  13. import traceback
  14. import base64
  15. from datetime import datetime
  16. from common.db import db_user, db_czrz
  17. from common import AvconH5API, AvconMiniAPI
  18. from models import *
  19. '''
  20. 融合通信相关接口
  21. '''
  22. router = APIRouter()
  23. @router.get('/get_video_list')
  24. async def get_video_list(
  25. db: Session = Depends(get_db),
  26. user_id=Depends(valid_access_token)
  27. ):
  28. return AvconH5API.get_channel_all()
  29. @router.get('/get_mini_video_list')
  30. async def get_mini_video_list(
  31. db: Session = Depends(get_db)
  32. ):
  33. # domain = AvconMiniAPI.get_config("DOMAIN")
  34. # rsa_public_key = AvconMiniAPI.get_config("RSA_PUBLIC_KEY")
  35. # return domain + " / " + rsa_public_key
  36. # return AvconMiniAPI.get_group_page()
  37. # return AvconMiniAPI.get_group_device("G6@mm.zw.yj")
  38. # return AvconMiniAPI.get_device_info("19.152.196.150@mm.zw.yj")
  39. # return AvconMiniAPI.get_device_all()
  40. # return AvconMiniAPI.get_device_channel("19.152.196.150@mm.zw.yj")
  41. # return AvconMiniAPI.get_device_channel_gps("19.152.196.150@mm.zw.yj")
  42. return AvconMiniAPI.get_channel_all()
  43. @router.post("/get_start_mini_param")
  44. async def get_start_mini_param(
  45. request: Request,
  46. body = Depends(remove_xss_json),
  47. db: Session = Depends(get_db),
  48. user_id = Depends(valid_access_token)
  49. ):
  50. userid = get_req_param_optional(body, "userid")
  51. if userid == "":
  52. user_info = db.query(AvconUser).filter(AvconUser.user_id).first()
  53. if user_info is None:
  54. return {
  55. "code": 500,
  56. "msg": "当前账号对应的融合设备为空,请联系管理员配置。"
  57. }
  58. userid = user_info.dev_id
  59. password = get_req_param_optional(body, "password")
  60. if password == "":
  61. password = "123"
  62. windowpos = get_req_param(body, "windowpos")
  63. x = windowpos['x']
  64. y = windowpos['y']
  65. width = windowpos['width']
  66. height = windowpos['height']
  67. top = windowpos['top']
  68. # 进入会议后需要广播的人员列表
  69. members = get_req_param(body, "members")
  70. params = {
  71. "mode": "mini",
  72. "cmd": "enterroom",
  73. "serverip": "19.152.196.106",
  74. "userid": userid,
  75. "password": password,
  76. "windowpos": {"x": x,"y": y,"width": width,"height": height,"top": top},
  77. "members": members
  78. }
  79. logger.info("发起融合通信mini客户端: {}", params)
  80. json_str = json.dumps(params, ensure_ascii=False)
  81. base64_str = base64.b64encode(json_str.encode('utf-8')).decode('utf-8')
  82. try:
  83. user_info = db_user.get_user_info(db, user_id)
  84. db_czrz.log_username(db, user_id, user_info.user_name, user_info.nick_name, "应急一张图", "发起会议", request.client.host)
  85. except:
  86. traceback.print_exc()
  87. return {
  88. "code": 0,
  89. "msg": "success",
  90. "data": "" + "avcon6://" + base64_str
  91. }
  92. @router.post("/get_start_mini_with_no_param")
  93. async def get_start_mini_param(
  94. body = Depends(remove_xss_json),
  95. db: Session = Depends(get_db),
  96. user_id = Depends(valid_access_token)
  97. ):
  98. userid = get_req_param_optional(body, "userid")
  99. if userid == "":
  100. user_info = db.query(AvconUser).filter(AvconUser.user_id).first()
  101. if user_info is None:
  102. return {
  103. "code": 500,
  104. "msg": "当前账号对应的融合设备为空,请联系管理员配置。"
  105. }
  106. userid = user_info.dev_id
  107. password = get_req_param_optional(body, "password")
  108. if password == "":
  109. password = "123"
  110. roomcode = get_req_param_optional(body, "roomcode")
  111. windowpos = get_req_param(body, "windowpos")
  112. x = windowpos['x']
  113. y = windowpos['y']
  114. width = windowpos['width']
  115. height = windowpos['height']
  116. top = windowpos['top']
  117. params = {
  118. "mode": "mini",
  119. "cmd": "enterroom",
  120. "serverip": "19.152.196.106",
  121. "userid": userid,
  122. "password": password,
  123. "roomcode": roomcode,
  124. "windowpos": {"x": x,"y": y,"width": width,"height": height,"top": top},
  125. }
  126. logger.info("发起融合通信mini客户端入会: {}", params)
  127. json_str = json.dumps(params, ensure_ascii=False)
  128. base64_str = base64.b64encode(json_str.encode('utf-8')).decode('utf-8')
  129. return {
  130. "code": 0,
  131. "msg": "success",
  132. "data": "" + "avcon6://" + base64_str
  133. }
  134. # 视频会商
  135. @router.get("/get_device_tree")
  136. async def get_device_tree(
  137. body = Depends(remove_xss_json),
  138. db: Session = Depends(get_db),
  139. user_id = Depends(valid_access_token)
  140. ):
  141. try:
  142. result = []
  143. data = AvconH5API.get_search_region("指挥终端")
  144. if data is not None:
  145. result1 = []
  146. fomat_device_group_result(data[0], result1)
  147. result.append({
  148. "id": data[0]["group_id"],
  149. "label": data[0]["group_name"],
  150. "children": result1
  151. })
  152. data = AvconH5API.get_search_region("H323会议视频")
  153. if data is not None:
  154. result1 = []
  155. fomat_device_group_result(data[0], result1)
  156. result.append({
  157. "id": data[0]["group_id"],
  158. "label": data[0]["group_name"],
  159. "children": result1
  160. })
  161. data = AvconH5API.get_search_region("手机App")
  162. if data is not None:
  163. result1 = []
  164. fomat_device_group_result(data[0], result1)
  165. result.append({
  166. "id": data[0]["group_id"],
  167. "label": data[0]["group_name"],
  168. "children": result1
  169. })
  170. return {
  171. "code": 0,
  172. "msg": "success",
  173. "data": result
  174. }
  175. except Exception as e:
  176. traceback.print_exc()
  177. return {
  178. "code": 0,
  179. "msg": "无法获取视频终端",
  180. "data": []
  181. }
  182. #
  183. #return {
  184. # "code": 500,
  185. # "msg": "无法获取视频终端"
  186. #}
  187. def fomat_device_group_result(data: dict, result: dict) -> dict:
  188. if "child_group" in data:
  189. for n in data["child_group"]:
  190. new_item = {
  191. "id": n['group_id'],
  192. "label": n['group_name'],
  193. "order_id": n['order_id']
  194. }
  195. result1 = []
  196. fomat_device_result(n, result1)
  197. new_item["children"] = result1
  198. result.append(new_item)
  199. @router.get("/get_device_list/{group_id}")
  200. async def get_device_list(
  201. group_id: str,
  202. body = Depends(remove_xss_json),
  203. db: Session = Depends(get_db),
  204. user_id = Depends(valid_access_token)
  205. ):
  206. try:
  207. result = []
  208. data = AvconH5API.get_group_device(group_id=group_id)
  209. for n in data:
  210. new_item = {
  211. "id": n['dev_id'],
  212. "name": n['dev_name'],
  213. "dept": get_avcon_type_text(n['dev_type']),
  214. "mobile": n['dev_id']
  215. }
  216. result.append(new_item)
  217. return {
  218. "code": 0,
  219. "msg": "success",
  220. "data": result
  221. }
  222. except Exception as e:
  223. traceback.print_exc()
  224. return {
  225. "code": 200,
  226. "msg": "无法获取视频终端",
  227. "data": []
  228. }
  229. def get_avcon_type_text(type: str) -> str:
  230. if type == '001':
  231. return '指挥视频终端'
  232. elif type == '060':
  233. return '营区监控'
  234. elif type == '085':
  235. return '监控网关服务器'
  236. elif type == '100':
  237. return 'H.323终端'
  238. return "未知终端"
  239. # 无人机
  240. @router.get("/get_drone_tree")
  241. async def get_drone_tree(
  242. equipment: str = Query('', description='设备类型'),
  243. body = Depends(remove_xss_json),
  244. db: Session = Depends(get_db),
  245. user_id = Depends(valid_access_token)
  246. ):
  247. try:
  248. result = []
  249. equipment_type = "无人机视频"
  250. print('equipment:', equipment)
  251. data = AvconH5API.get_search_region(equipment_type)
  252. if data is not None:
  253. result1 = []
  254. fomat_device_result(data[0], result1)
  255. result.append({
  256. "id": data[0]["group_id"],
  257. "label": data[0]["group_name"],
  258. "children": result1
  259. })
  260. result = [
  261. {
  262. "label": '无人机设备',
  263. "children": [
  264. {
  265. "label": '茂名市应急管理局',
  266. "children": [
  267. {
  268. "id": "1",
  269. "label": '应急无人机(例子1)',
  270. "url": "http://19.152.196.223:5050/liveplay.html?liveurl=ws://10.100.100.103:9001/live/mmjk0002@mm.zw.yj_00.flv&channelid=mmjk0002@mm.zw.yj_00"
  271. }
  272. ]
  273. },
  274. {
  275. "label": '电白区应急管理局',
  276. "children": [
  277. {
  278. "id": "2",
  279. "label": '应急无人机(例子2)',
  280. "url": "http://19.152.196.223:5050/liveplay.html?liveurl=ws://10.100.100.103:9001/live/mmjk0002@mm.zw.yj_00.flv&channelid=mmjk0002@mm.zw.yj_00"
  281. }
  282. ]
  283. },
  284. {
  285. "label": '茂南区应急管理局',
  286. "children": [
  287. {
  288. "id": "3",
  289. "label": '应急无人机(例子3)',
  290. "url": "http://19.152.196.223:5050/liveplay.html?liveurl=ws://10.100.100.103:9001/live/mmjk0002@mm.zw.yj_00.flv&channelid=mmjk0002@mm.zw.yj_00"
  291. }
  292. ]
  293. }
  294. ]
  295. }
  296. ]
  297. return {
  298. "code": 0,
  299. "msg": "success",
  300. "data": result
  301. }
  302. except Exception as e:
  303. traceback.print_exc()
  304. return {
  305. "code": 200,
  306. "msg": "无法获取视频终端",
  307. "data": []
  308. }
  309. def fomat_device_result(data: dict, result: dict) -> dict:
  310. if "child_group" in data:
  311. for n in data["child_group"]:
  312. new_item = {
  313. "id": n['group_id'],
  314. "label": n['group_name'],
  315. "order_id": n['order_id']
  316. }
  317. result1 = []
  318. fomat_device_result(n, result1)
  319. new_item["children"] = result1
  320. result.append(new_item)
  321. if "child_device" in data:
  322. for n in data["child_device"]:
  323. new_item = {
  324. "id": n['dev_id'],
  325. "label": n['dev_name'],
  326. "dev_type": n['dev_type'],
  327. # "order_id": n['order_id'],
  328. "isLeaf": True
  329. }
  330. result.append(new_item)
  331. # 手机工作台
  332. @router.post("/get_mobile_workstation_info")
  333. async def get_mobile_workstation_info(
  334. db: Session = Depends(get_db),
  335. body = Depends(remove_xss_json),
  336. user_id = Depends(valid_access_token)
  337. ):
  338. try:
  339. current = body['current']
  340. size = body['size']
  341. keywords = body['query']['keywords']
  342. result = []
  343. total = 0
  344. data = AvconH5API.get_search_region("手机App")
  345. if data is not None:
  346. for n in data[0]['child_device']:
  347. dev_id = n['dev_id']
  348. dev_name = n['dev_name']
  349. if keywords == "" or dev_name.find(keywords) != -1:
  350. result.append({
  351. "dev_id": dev_id,
  352. "dev_name": dev_name
  353. })
  354. total = len(result)
  355. result = paginate(result, size, current)
  356. return {
  357. "code": 0,
  358. "msg": "success",
  359. "data": result,
  360. "total": total
  361. }
  362. except Exception as e:
  363. traceback.print_exc()
  364. return {
  365. "code": 200,
  366. "msg": "无法获取视频终端",
  367. "data": [],
  368. "total": 0
  369. }
  370. # 移动指挥车
  371. @router.post("/get_mobile_command_vehicle_list_info")
  372. async def get_mobile_command_vehicle_list_info(
  373. db: Session = Depends(get_db),
  374. body = Depends(remove_xss_json),
  375. user_id = Depends(valid_access_token)
  376. ):
  377. try:
  378. current = body['current']
  379. size = body['size']
  380. keywords = body['query']['keywords']
  381. result = []
  382. total = 0
  383. data = AvconH5API.get_search_region("指挥终端")
  384. if data is not None:
  385. for n in data[0]['child_device']:
  386. dev_id = n['dev_id']
  387. dev_name = n['dev_name']
  388. if keywords == "" or dev_name.find(keywords) != -1:
  389. result.append({
  390. "dev_id": dev_id,
  391. "dev_name": dev_name
  392. })
  393. total = len(result)
  394. result = paginate(result, size, current)
  395. return {
  396. "code": 0,
  397. "msg": "success",
  398. "data": result,
  399. "total": total
  400. }
  401. except Exception as e:
  402. traceback.print_exc()
  403. return {
  404. "code": 200,
  405. "msg": "无法获取视频终端",
  406. "data": [],
  407. "total": 0
  408. }
  409. def paginate(array, page_size, page_number):
  410. start_index = (page_number - 1) * page_size
  411. end_index = start_index + page_size
  412. return array[start_index:end_index]