AvconH5API.py 13 KB

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