AvconMiniAPI.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. import string
  5. import random
  6. import json
  7. from utils import *
  8. from utils.redis_util import *
  9. app_id = "820e2aa3903e46939abde098409319e1"
  10. secret= "92659e6e0dd3c550952df7c95a483d56"
  11. '''
  12. 融合通信Mini客户端对接
  13. U8 OpenAPI使用说明书
  14. '''
  15. API_ROOT = "http://19.152.196.106:12030/avcon-api"
  16. def get_nonce(length: int = 10) -> str:
  17. characters = string.ascii_letters + string.digits
  18. # 随机选择字符集的长度个字符
  19. random_string = ''.join(random.choice(characters) for _ in range(length))
  20. return random_string
  21. def get_sign_data(nonce: str, timestamp: str, uri: str, query: str = None, body: dict = None) -> str:
  22. data = nonce + str(timestamp) + str(uri)
  23. if query is not None:
  24. data = data + query
  25. if body is not None:
  26. data = data + json.dumps(body, ensure_ascii=False)
  27. sign_data = md5(data)
  28. print('data:', data)
  29. print('sign_data:', sign_data)
  30. return sign_data
  31. # 3.1 获得Token
  32. def get_auth_token():
  33. nonce = get_nonce()
  34. timestamp = str(unixstamp())
  35. uri = "/oauth/token"
  36. query = "app_id="+app_id+"&secret="+secret
  37. sign_data = get_sign_data(nonce, timestamp, uri, query)
  38. headers = {
  39. "Content-Type": 'application/json',
  40. "X-Nonce": nonce,
  41. "X-Timestamp": timestamp,
  42. "X-Signature": sign_data
  43. }
  44. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  45. print(response.text)
  46. if response.status_code == 200:
  47. result = response.json()
  48. if result['code'] == 0:
  49. data = result['data']
  50. return data['token']
  51. def get_redis_token():
  52. redis_key = "avcon_mini_api_token"
  53. token_val = redis_get(redis_key)
  54. if token_val is None:
  55. token_val = get_auth_token()
  56. if token_val is not None:
  57. redis_set_with_time(redis_key, token_val, 300)
  58. print('auth_token:', token_val)
  59. return token_val
  60. # 4.1 查询配置项
  61. def get_config(name: str):
  62. token = get_redis_token()
  63. nonce = get_nonce()
  64. timestamp = str(unixstamp())
  65. uri = "/open/api/v1/config/" + name
  66. sign_data = get_sign_data(nonce, timestamp, uri)
  67. headers = {
  68. "Content-Type": 'application/json',
  69. "Authorization": token,
  70. "X-Nonce": nonce,
  71. "X-Timestamp": timestamp,
  72. "X-Signature": sign_data
  73. }
  74. response = requests.get(API_ROOT + uri, headers=headers, timeout=15)
  75. if response.status_code == 200:
  76. result = response.json()
  77. if result['code'] == 0:
  78. data = result['data']
  79. return data
  80. else:
  81. print(response.text)
  82. # 5.1 查询区域
  83. def get_group(group_id: str = 'G1@mm.zw.yj'):
  84. token = get_redis_token()
  85. nonce = get_nonce()
  86. timestamp = str(unixstamp())
  87. uri = "/open/api/v1/monitor-room-group/" + group_id
  88. sign_data = get_sign_data(nonce, timestamp, uri)
  89. headers = {
  90. "Content-Type": 'application/json',
  91. "Authorization": token,
  92. "X-Nonce": nonce,
  93. "X-Timestamp": timestamp,
  94. "X-Signature": sign_data
  95. }
  96. response = requests.get(API_ROOT + uri, headers=headers, timeout=15)
  97. if response.status_code == 200:
  98. result = response.json()
  99. if result['code'] == 0:
  100. data = result['data']
  101. return data
  102. else:
  103. print(response.text)
  104. # 5.2 查询区域分页
  105. def get_group_page():
  106. token = get_redis_token()
  107. nonce = get_nonce()
  108. timestamp = str(unixstamp())
  109. uri = "/open/api/v1/monitor-room-group/page"
  110. query = "page_num=1&page_size=100"
  111. sign_data = get_sign_data(nonce, timestamp, uri, query)
  112. headers = {
  113. "Content-Type": 'application/json',
  114. "Authorization": token,
  115. "X-Nonce": nonce,
  116. "X-Timestamp": timestamp,
  117. "X-Signature": sign_data
  118. }
  119. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  120. if response.status_code == 200:
  121. result = response.json()
  122. if result['code'] == 0:
  123. data = result['data']
  124. return data
  125. else:
  126. print(response.text)
  127. # 5.3 查询区域的设备分页
  128. def get_group_device(group_id: str):
  129. token = get_redis_token()
  130. nonce = get_nonce()
  131. timestamp = str(unixstamp())
  132. uri = "/open/api/v1/monitor-room-group/" + group_id + "/device/page"
  133. query = "page_num=1&page_size=100"
  134. sign_data = get_sign_data(nonce, timestamp, uri, query)
  135. headers = {
  136. "Content-Type": 'application/json',
  137. "Authorization": token,
  138. "X-Nonce": nonce,
  139. "X-Timestamp": timestamp,
  140. "X-Signature": sign_data
  141. }
  142. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  143. if response.status_code == 200:
  144. result = response.json()
  145. if result['code'] == 0:
  146. data = result['data']
  147. return data
  148. else:
  149. print(response.text)
  150. # 6.1 查询设备
  151. def get_device_info(dev_id: str):
  152. token = get_redis_token()
  153. nonce = get_nonce()
  154. timestamp = str(unixstamp())
  155. uri = "/open/api/v1/device/" + dev_id
  156. sign_data = get_sign_data(nonce, timestamp, uri)
  157. headers = {
  158. "Content-Type": 'application/json',
  159. "Authorization": token,
  160. "X-Nonce": nonce,
  161. "X-Timestamp": timestamp,
  162. "X-Signature": sign_data
  163. }
  164. response = requests.get(API_ROOT + uri, headers=headers, timeout=15)
  165. if response.status_code == 200:
  166. result = response.json()
  167. if result['code'] == 0:
  168. data = result['data']
  169. return data
  170. else:
  171. print(response.text)
  172. # 6.2 查询设备分页
  173. def get_device_all():
  174. token = get_redis_token()
  175. nonce = get_nonce()
  176. timestamp = str(unixstamp())
  177. uri = "/open/api/v1/device/page"
  178. json_data = {
  179. "domain": "mm.zw.yj"
  180. }
  181. query = "json="+json.dumps(json_data, ensure_ascii=False) + "&page_num=1&page_size=1000"
  182. sign_data = get_sign_data(nonce, timestamp, uri, query)
  183. headers = {
  184. "Content-Type": 'application/json',
  185. "Authorization": token,
  186. "X-Nonce": nonce,
  187. "X-Timestamp": timestamp,
  188. "X-Signature": sign_data
  189. }
  190. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  191. if response.status_code == 200:
  192. result = response.json()
  193. if result['code'] == 0:
  194. data = result['data']
  195. return data
  196. else:
  197. print(response.text)
  198. # 6.3 查询设备的通道分页
  199. def get_device_channel(dev_id: str):
  200. token = get_redis_token()
  201. nonce = get_nonce()
  202. timestamp = str(unixstamp())
  203. uri = "/open/api/v1/device/" + dev_id + "/channel/page"
  204. json_data = {
  205. "domain": "mm.zw.yj"
  206. }
  207. query = "json="+json.dumps(json_data, ensure_ascii=False) + "&page_num=1&page_size=1000"
  208. sign_data = get_sign_data(nonce, timestamp, uri, query)
  209. headers = {
  210. "Content-Type": 'application/json',
  211. "Authorization": token,
  212. "X-Nonce": nonce,
  213. "X-Timestamp": timestamp,
  214. "X-Signature": sign_data
  215. }
  216. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  217. if response.status_code == 200:
  218. result = response.json()
  219. if result['code'] == 0:
  220. data = result['data']
  221. return data
  222. else:
  223. print(response.text)
  224. # 6.4 查询设备的通道GPS位置分页
  225. def get_device_channel_gps(dev_id: str):
  226. token = get_redis_token()
  227. nonce = get_nonce()
  228. timestamp = str(unixstamp())
  229. uri = "/open/api/v1/device/" + dev_id + "/channel-gps-point"
  230. sign_data = get_sign_data(nonce, timestamp, uri)
  231. headers = {
  232. "Content-Type": 'application/json',
  233. "Authorization": token,
  234. "X-Nonce": nonce,
  235. "X-Timestamp": timestamp,
  236. "X-Signature": sign_data
  237. }
  238. response = requests.get(API_ROOT + uri, headers=headers, timeout=15)
  239. if response.status_code == 200:
  240. result = response.json()
  241. if result['code'] == 0:
  242. data = result['data']
  243. return data
  244. else:
  245. print(response.text)
  246. else:
  247. print(response.text)
  248. # 7.1 查询通道
  249. # 7.2 查询通道分页
  250. def get_channel_all():
  251. token = get_redis_token()
  252. nonce = get_nonce()
  253. timestamp = str(unixstamp())
  254. uri = "/open/api/v1/device-channel/page"
  255. json_data = {
  256. "domain": "mm.zw.yj"
  257. }
  258. query = "json="+json.dumps(json_data, ensure_ascii=False) + "&page_num=1&page_size=1000"
  259. sign_data = get_sign_data(nonce, timestamp, uri, query)
  260. headers = {
  261. "Content-Type": 'application/json',
  262. "Authorization": token,
  263. "X-Nonce": nonce,
  264. "X-Timestamp": timestamp,
  265. "X-Signature": sign_data
  266. }
  267. response = requests.get(API_ROOT + uri + "?" + query, headers=headers, timeout=15)
  268. if response.status_code == 200:
  269. result = response.json()
  270. if result['code'] == 0:
  271. data = result['data']
  272. return data
  273. else:
  274. print(response.text)
  275. # 7.3 查询通道GPS位置