AvconH5API.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. from utils.redis_util import *
  5. app_id = "f41ddc53aee74905834ccfc93fe91787"
  6. secret= "d70322a3cc40d1a345539800c417fcf2"
  7. '''
  8. 融合通信H5对接
  9. '''
  10. API_ROOT = "http://19.152.196.223:5050/rpy"
  11. # 1.1获取Token
  12. '''
  13. {
  14. "code": 0,
  15. "msg": "Success.",
  16. "data": {
  17. "app_id": "5cdf02e49d434b3ac4c272a39d3211ca",
  18. "secret": "f3e2182a13690b75ff87c5273063201",
  19. "token": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJhdmNvbiIsInN1YiI6IjczNjkzYmI5ZGYwYTRiYjZiMWMyNTMxMWM0NjAwM2Q0IiwiaWF0IjoxNjM5MjA0MTgzLCJ1c2VyX3R5cGUiOiJvYXV0aCIsInJvbGVzIjpbIlJPTEVfb2F1dGgiXX0MS5JX9NJ7dQomZ4-udFukGMQsFFTMyYTKZ9wS0VgjTYM4A2MvpDZeGB4Ish3GOcH4Z_Sn2TAijyZYrJtLGAmww"}
  20. }
  21. '''
  22. def get_token():
  23. params = {
  24. "app_id": app_id,
  25. "secret": secret
  26. }
  27. api_url = API_ROOT + "/token"
  28. print('param', params)
  29. response = requests.get(url=api_url, params=params, timeout=5)
  30. print(response.text)
  31. if response.status_code == 200:
  32. result = response.json()
  33. if result['code'] == 0:
  34. data = result['data']
  35. return data['token'].removeprefix("Bearer ")
  36. def get_redis_token():
  37. redis_key = "avcon_h5_api_token"
  38. token_val = redis_get(redis_key)
  39. if token_val is None:
  40. token_val = get_token()
  41. if token_val is not None:
  42. redis_set_with_time(redis_key, token_val, 300)
  43. # print('token:', token_val)
  44. return token_val
  45. # 2.1获取区域
  46. '''
  47. {
  48. "code": 0,
  49. "msg": "Success.",
  50. "data": {
  51. count:1
  52. region:[{
  53. "region_id": "YS@hp",
  54. "region_name": "演示图像资源"
  55. }]
  56. }
  57. }
  58. '''
  59. def get_region():
  60. token = get_redis_token()
  61. headers = {
  62. 'Content-Type': 'application/json;charset=UTF-8',
  63. "Authorization": "Bearer " + token
  64. }
  65. api_url = API_ROOT + "/region"
  66. response = requests.get(url=api_url, headers=headers, timeout=5)
  67. if response.status_code == 200:
  68. result = response.json()
  69. if result['code'] == 0:
  70. data = result['data']
  71. return data['region']
  72. else:
  73. print(response.text)
  74. # 2.2获取区域组节点
  75. '''
  76. {
  77. "code": 0,
  78. "msg": "Success.",
  79. "data": {
  80. "count": 4,
  81. "group": [
  82. {
  83. "group_id": "Gc@hp",
  84. "group_name": "监控设备",
  85. "domain": "hp",
  86. "parent_id": "S1@hp",
  87. "level_id": 1,
  88. "order_id": 9999,
  89. "child_count": 1
  90. },
  91. ....略....
  92. ]
  93. }
  94. }
  95. '''
  96. def get_group(region_id: str, parent_id: str = ''):
  97. token = get_redis_token()
  98. headers = {
  99. 'Content-Type': 'application/json;charset=UTF-8',
  100. "Authorization": "Bearer " + token
  101. }
  102. params = {
  103. "region_id": region_id
  104. }
  105. if parent_id != '':
  106. params = {
  107. "parent_id": parent_id
  108. }
  109. api_url = API_ROOT + "/group"
  110. response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
  111. if response.status_code == 200:
  112. result = response.json()
  113. if result['code'] == 0:
  114. data = result['data']
  115. return data['group']
  116. else:
  117. print(response.text)
  118. # 2.3根据名称搜索区域下所有的节点
  119. '''
  120. {
  121. "code": 0,
  122. "msg": "Success.",
  123. "data": [
  124. {
  125. "group_id": "G1@xf",
  126. "group_name": "消防指挥域",
  127. "domain": "xf",
  128. "parent_id": "G0@xf",
  129. "level_id": 1,
  130. "order_id": 9999,
  131. "child_count": 0, //区域组数
  132. "total_number": 0, //设备数
  133. },
  134. {
  135. "group_id": "G2@xf",
  136. "group_name": "消防指挥域",
  137. "domain": "xf",
  138. "parent_id": "G1@xf",
  139. "level_id": 2,
  140. "order_id": 9999,
  141. "child_count": 2, //区域组数
  142. "total_number": 1, //设备数
  143. "child_group": [
  144. {
  145. "group_id": "G110@xf",
  146. "group_name": "指挥中心",
  147. "domain": "xf",
  148. "parent_id": "G2@xf",
  149. "level_id": 2,
  150. "order_id": 9999,
  151. "child_count": 0,
  152. "total_number": 1, //设备数
  153. "child_device": [
  154. {
  155. "dev_id": "cy0001@xf",
  156. "dev_name": "sx",
  157. "domain": "xf",
  158. "group_id": "G110@xf",
  159. "channel_num": 1,
  160. "status": 0,
  161. "dev_type": "001",
  162. "child_channel": [
  163. {
  164. "channel_id": "cy0001@xf_00",
  165. "channel_name": "cy0001@xf_00",
  166. "channel_no": 0,
  167. "dev_id": "cy0001@xf",
  168. "status": 1,
  169. "lat": 28.28,
  170. "lng": 116.38
  171. }
  172. ]
  173. }
  174. ]
  175. },
  176. {
  177. "group_id": "G110@xf",
  178. "group_name": "电视墙",
  179. "domain": "xf",
  180. "parent_id": "G110@xf",
  181. "level_id": 2,
  182. "order_id": 9999,
  183. "child_count": 0,
  184. "total_number": 0
  185. }
  186. ],
  187. "child_device": [
  188. {
  189. "dev_id": "sx@xf",
  190. "dev_name": "sx",
  191. "domain": "xf",
  192. "group_id": "G110@xf",
  193. "channel_num": 1,
  194. "status": 0,
  195. "dev_type": "001",
  196. "child_channel": [
  197. {
  198. "channel_id": "sx@xf_00",
  199. "channel_name": "sx@xf_00",
  200. "channel_no": 0,
  201. "dev_id": "sx@xf",
  202. "status": 0
  203. }
  204. ]
  205. }
  206. ]
  207. }
  208. ]
  209. }
  210. '''
  211. def get_search_region(region_name: str):
  212. token = get_redis_token()
  213. headers = {
  214. 'Content-Type': 'application/json;charset=UTF-8',
  215. "Authorization": "Bearer " + token
  216. }
  217. params = {
  218. "region_name": region_name
  219. }
  220. api_url = API_ROOT + "/search/region"
  221. response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
  222. if response.status_code == 200:
  223. result = response.json()
  224. if result['code'] == 0:
  225. data = result['data']
  226. return data
  227. else:
  228. print(response.text)
  229. # 2.4搜索范围内的通道信息及直播流地址
  230. '''
  231. {
  232. "code": 0,
  233. "msg": "Success.",
  234. "data": {
  235. "count": 2,
  236. "channel": [
  237. {
  238. "channel_id": "hk01@hp_00",
  239. "channel_name": "学校校门外监控",
  240. "channel_no": 0,
  241. "dev_id": "hk01@hp",
  242. "status": 1,
  243. "lat": 28.631629,
  244. "lng": 115.877328,
  245. "live_url": "http://192.168.0.1:1935/live/hk01@hp_00.flv"
  246. },
  247. {
  248. "channel_id": "hk02@hp_00",
  249. "channel_name": "学校校门外监控2",
  250. "channel_no": 0,
  251. "dev_id": "hk02@hp",
  252. "status": 1,
  253. "lat": 28.631529,
  254. "lng": 115.874328,
  255. "live_url": "http://192.168.0.1:1935/live/hk02@hp_00.flv"
  256. }
  257. ]
  258. }
  259. }
  260. '''
  261. def get_search_live_location(center_lat: float, center_lng: float, point_lat: float, point_lng: float):
  262. token = get_redis_token()
  263. headers = {
  264. 'Content-Type': 'application/json;charset=UTF-8',
  265. "Authorization": "Bearer " + token
  266. }
  267. params = {
  268. "center_lat": center_lat,
  269. "center_lng": center_lng,
  270. "point_lat": point_lat,
  271. "point_lng": point_lng
  272. }
  273. api_url = API_ROOT + "/search/live-location"
  274. response = requests.get(url=api_url, headers=headers, params=params, timeout=5)
  275. if response.status_code == 200:
  276. result = response.json()
  277. if result['code'] == 0:
  278. data = result['data']
  279. return data['channel']
  280. else:
  281. print(response.text)
  282. # 3.1获取设备
  283. '''
  284. {
  285. "code": 0,
  286. "msg": "Success.",
  287. "data": {
  288. "count": 10,
  289. "device": [
  290. {
  291. "dev_id": "hk01@hp",
  292. "dev_name": "海康设备",
  293. "domain": "hp",
  294. "group_id": "Gc@hp",
  295. "channel_num": 1,
  296. "status": 1,
  297. "dev_type": "135"
  298. },
  299. {
  300. "dev_id": "jkwg01@hp",
  301. "dev_name": "监控网关",
  302. "domain": "hp",
  303. "group_id": "Gc@hp",
  304. "channel_num": 0,
  305. "status": 0,
  306. "dev_type": "085"
  307. }
  308. ....略
  309. ]
  310. }
  311. }
  312. '''
  313. def get_group_device(group_id: str, dev_type: dict = None, status: int = -1):
  314. token = get_redis_token()
  315. headers = {
  316. 'Content-Type': 'application/json;charset=UTF-8',
  317. "Authorization": "Bearer " + token
  318. }
  319. params = {}
  320. if status != -1:
  321. params['status'] = status
  322. if dev_type != None:
  323. params['dev_type'] = "[" + ",".join(dev_type) + "]"
  324. api_url = API_ROOT + "/group/" + group_id + "/device"
  325. print('get_group_device:', api_url)
  326. response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
  327. if response.status_code == 200:
  328. result = response.json()
  329. if result['code'] == 0:
  330. data = result['data']
  331. return data['device']
  332. else:
  333. print(response.text)
  334. # 4.1 获取通道
  335. '''
  336. {
  337. "code": 0,
  338. "msg": "Success.",
  339. "data": {
  340. "count": 1,
  341. "channel": [
  342. {
  343. "channel_id": "hk01@hp_00",
  344. "channel_name": "学校校门外监控",
  345. "channel_no": 0,
  346. "dev_id": "hk01@hp",
  347. "status": 1,
  348. "lat": 28.631629,
  349. "lng": 115.877328
  350. }
  351. ]
  352. }
  353. }
  354. '''
  355. def get_device_channel(device_id: str, status: int = -1):
  356. token = get_redis_token()
  357. headers = {
  358. 'Content-Type': 'application/json;charset=UTF-8',
  359. "Authorization": "Bearer " + token
  360. }
  361. params = {}
  362. if status != -1:
  363. params['status'] = status
  364. api_url = API_ROOT + "/device/" + device_id + "/channel"
  365. response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
  366. if response.status_code == 200:
  367. result = response.json()
  368. if result['code'] == 0:
  369. data = result['data']
  370. return data['channel']
  371. else:
  372. print(response.text)
  373. # 4.2 获取所有通道
  374. '''
  375. {
  376. "code": 0,
  377. "msg": "Success.",
  378. "data": {
  379. "count": 80,
  380. "channel": [
  381. {
  382. "channel_id": "hk01@hp_00",
  383. "channel_name": "学校校门外监控",
  384. "channel_no": 0,
  385. "dev_id": "hk01@hp",
  386. "status": 1,
  387. "lat": 28.631629,
  388. "lng": 115.877328
  389. },
  390. {
  391. "channel_id": "hk02@hp_00",
  392. "channel_name": "学校校门外监控2",
  393. "channel_no": 0,
  394. "dev_id": "hk02@hp",
  395. "status": 1,
  396. "lat": 28.631529,
  397. "lng": 115.874328
  398. }
  399. ...略
  400. ]
  401. }
  402. }
  403. '''
  404. def get_channel_all(status: int = -1, gpsonly: int = -1):
  405. token = get_redis_token()
  406. headers = {
  407. 'Content-Type': 'application/json;charset=UTF-8',
  408. "Authorization": "Bearer " + token
  409. }
  410. params = {}
  411. if status != -1:
  412. params['status'] = status
  413. if gpsonly != -1:
  414. params['gpsonly'] = gpsonly
  415. api_url = API_ROOT + "/channel/all"
  416. response = requests.get(url=api_url, headers=headers, params=params, timeout=15)
  417. if response.status_code == 200:
  418. result = response.json()
  419. if result['code'] == 0:
  420. data = result['data']
  421. return data['channel']
  422. else:
  423. print(response.text)
  424. # 5.1 获取直播流
  425. '''
  426. {
  427. "code": 0,
  428. "msg": "success.",
  429. "data": {
  430. "live_url": "http://192.168.0.1:1935/live/hk01@hp_00.flv"
  431. }
  432. }
  433. '''
  434. def get_live_streaming(channel_id: str):
  435. token = get_redis_token()
  436. headers = {
  437. 'Content-Type': 'application/json;charset=UTF-8',
  438. "Authorization": "Bearer " + token
  439. }
  440. api_url = API_ROOT + "/live/streaming/" + channel_id
  441. response = requests.get(url=api_url, headers=headers, timeout=15)
  442. if response.status_code == 200:
  443. result = response.json()
  444. if result['code'] == 0:
  445. data = result['data']
  446. return data['live_url']
  447. else:
  448. print(response.text)
  449. # 5.2云台控制
  450. # 没用
  451. # 5.3获取通道直播页面完整地址
  452. '''
  453. {
  454. "code":0,
  455. "msg":"Success",
  456. "data":{
  457. "play_url":"http://192.168.0.88:5050/liveplay.html?liveurl=ws://192.168.0.88:9001/live/gbdh01@xf_00.flv"
  458. }
  459. }
  460. '''
  461. def get_live_playing(channel_id: str):
  462. token = get_redis_token()
  463. headers = {
  464. 'Content-Type': 'application/json;charset=UTF-8',
  465. "Authorization": "Bearer " + token
  466. }
  467. api_url = API_ROOT + "/live/playing/" + channel_id
  468. response = requests.get(url=api_url, headers=headers, timeout=15)
  469. if response.status_code == 200:
  470. result = response.json()
  471. if result['code'] == 0:
  472. data = result['data']
  473. return data['play_url']
  474. else:
  475. print(response.text)
  476. if __name__ == '__main__':
  477. get_region()