avcon.py 13 KB

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