avcon.py 14 KB

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