api.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # update: 2021-10-15
  2. """
  3. 配置流程
  4. 1、禁用vbox虚拟机的网卡。
  5. 2、pc端安装客户端程序,打开“智能广播扫描工具”,扫描设备,并修改设备ip。(服务ip不能和设备ip一样,随意就可以)
  6. {
  7. "sn": "iHciceH9A5",
  8. "type": "req",
  9. "name": "songs_queue_append",
  10. "params": {
  11. "tid": "134",
  12. "vol": 100,
  13. "urls": [
  14. {
  15. "name": "1",
  16. # 只支持mp3格式 不支持wav格式
  17. "uri": "http://websitecdn.gangganghao.com.cn/ls20/mp3/ei.mp3"
  18. },
  19. {
  20. "name": "2",
  21. "uri": "http://websitecdn.gangganghao.com.cn/ls20/mp3/jxy.mp3"
  22. }
  23. ]
  24. }
  25. }
  26. """
  27. import requests
  28. import time
  29. class Api(object):
  30. def __init__(self, service_url='http://192.168.30.89:8888'):
  31. """
  32. port: 端口号 8888 安装目录下 ls20_mgrserver/ls20_mgrserver.ini 文件中进行配置
  33. 192.168.1.241 ls20://02029F15B65A
  34. 192.168.1.242 ls20://0203B0F822FC
  35. 192.168.1.243 ls20://02015790B99B
  36. 192.168.1.244 ls20://02006723C3BA
  37. 192.168.1.245 ls20://020099728112
  38. """
  39. # self.api_service_url = service_url
  40. # --- 八局项目 第3现场设备 ---
  41. # self.api_service_url = 'http://192.168.15.193:8888'
  42. # self.file_service_url = 'http://192.168.15.195:9900'
  43. # --- 八局项目 第4现场设备 ---
  44. self.api_service_url = 'http://192.168.1.43:8888'
  45. self.file_service_url = 'http://192.168.1.242:9900'
  46. self.headers = dict()
  47. self.headers['content-type'] = 'application/json'
  48. def call_audio_make_sound_v2(self, file_name, url, sn):
  49. """
  50. url: http://sc.geemi.cn/smartSiteService/api/uploadLog/uploadLogCapture
  51. """
  52. data = {
  53. # "sn": sn,
  54. "sn": 'iHciceH9A5',
  55. "type": "req",
  56. "name": 'songs_queue_append',
  57. "params": {
  58. "tid": '1',
  59. "vol": 50,
  60. "urls": [
  61. {
  62. "name": "1",
  63. # "uri": f"http://127.0.0.1:5042/api?module=TEST&method=test011&mp3_name={file_name}",
  64. # "uri": "http://192.168.20.231:8802/api?mp3_name=2021-10-16-11-05-29-717030.mp3",
  65. # "uri": f"http://fra-component-trigger:5042/api?mp3_name={file_name}",
  66. # "uri": f"http://192.168.20.231:9900/{file_name}",
  67. # "uri": f"http://192.168.0.15:9900/{file_name}",
  68. # "uri": f"http://192.168.51.242:9900/{file_name}",
  69. # "uri": "http://192.168.51.242:9900/find-unknown-person.mp3",
  70. # "uri": "http://192.168.15.195:9900/find-unknown-person.mp3",
  71. "uri": f"{self.file_service_url}/find-unknown-person.mp3",
  72. }
  73. ]
  74. }
  75. }
  76. times = 0
  77. while times < 10:
  78. try:
  79. print('Api:call_audio_make_sound_v2:url:', self.api_service_url)
  80. print('Api:call_audio_make_sound_v2:uri:', data.get('params').get('urls')[0].get('uri'))
  81. response = requests.post(self.api_service_url, json=data, headers=self.headers)
  82. print('Api:call_audio_make_sound_v2:result:', response.status_code)
  83. return True
  84. except Exception as e:
  85. print(e)
  86. times += 1
  87. time.sleep(1)
  88. return False
  89. def call_audio_make_sound_v3(self, result_type):
  90. """
  91. url: http://sc.geemi.cn/smartSiteService/api/uploadLog/uploadLogCapture
  92. result_type: 结果类型 0 匹配到人员 1 人脸检测失败 2 提取特征失败 3 未匹配到人员
  93. """
  94. if result_type in [3]:
  95. # uri = "http://192.168.15.195:9900/a1.mp3" # 请登记 3
  96. uri = f"{self.file_service_url}/a1.mp3" # 请登记 3
  97. elif result_type in [1, 2]:
  98. # uri = "http://192.168.15.195:9900/a2.mp3" # 请重试 1 2
  99. uri = f"{self.file_service_url}/a2.mp3" # 请重试 1 2
  100. elif result_type in [0]:
  101. # uri = "http://192.168.15.195:9900/a3.mp3" # 欢迎 0
  102. uri = f"{self.file_service_url}/a3.mp3" # 欢迎 0
  103. else:
  104. return False
  105. data = {
  106. # "sn": sn,
  107. "sn": 'iHciceH9A5',
  108. "type": "req",
  109. "name": 'songs_queue_append',
  110. "params": {
  111. "tid": '1',
  112. # "vol": 50,
  113. "vol": 100,
  114. "urls": [
  115. {
  116. "name": "1",
  117. # "uri": f"http://127.0.0.1:5042/api?module=TEST&method=test011&mp3_name={file_name}",
  118. # "uri": "http://192.168.20.231:8802/api?mp3_name=2021-10-16-11-05-29-717030.mp3",
  119. # "uri": f"http://fra-component-trigger:5042/api?mp3_name={file_name}",
  120. # "uri": f"http://192.168.20.231:9900/{file_name}",
  121. # "uri": f"http://192.168.0.15:9900/{file_name}",
  122. # "uri": f"http://192.168.51.242:9900/{file_name}",
  123. # "uri": "http://192.168.51.242:9900/find-unknown-person.mp3",
  124. # "uri": "http://192.168.15.195:9900/a1.mp3",
  125. "uri": uri,
  126. }
  127. ]
  128. }
  129. }
  130. times = 0
  131. while times < 10:
  132. try:
  133. # print('Api:call_audio_make_sound_v3:url:', url)
  134. # print('Api:call_audio_make_sound_v3:uri:', data.get('params').get('urls')[0].get('uri'))
  135. response = requests.post(self.api_service_url, json=data, headers=self.headers)
  136. # print('Api:call_audio_make_sound_v3:result:', response.status_code)
  137. return True
  138. except Exception as e:
  139. print(e)
  140. times += 1
  141. time.sleep(1)
  142. return False
  143. def file_download(self):
  144. """
  145. doc: https://documenter.getpostman.com/view/691989/TzRVdR6Q#70870504-1cb6-439b-9cf4-f1f786d9d3a1
  146. """
  147. data = {
  148. # "sn": ["ls20://0201518F79D4"],
  149. "sn": ["iHciceH9A5"],
  150. "type": "req",
  151. "name": "file_download",
  152. "params": {
  153. "urls": [
  154. {
  155. "uri": "http://192.168.20.231:9900/2021-10-16-11-05-29-717030.mp3",
  156. "target": "/root/2021-10-16-11-05-29-717030.mp3"
  157. }
  158. ]
  159. }
  160. }
  161. print('Api:file_download:url:', self.api_service_url)
  162. response = requests.post(self.api_service_url, json=data, headers=self.headers)
  163. print('Api:file_download:result:', response.status_code)
  164. return response.status_code
  165. def songs_queue_append(self):
  166. """
  167. url: http://sc.geemi.cn/smartSiteService/api/uploadLog/uploadLogCapture
  168. """
  169. from requests.exceptions import ConnectionError, ReadTimeout
  170. import time
  171. time.sleep(0.01)
  172. # url = "http://192.168.20.189:8888"
  173. # url = "http://192.168.51.193:8888"
  174. # url = "http://192.168.15.193:8888"
  175. data = {
  176. # "sn": "iHciceH9A5", # 第3现场设备
  177. "sn": "ls20://02009EDCD48C", # 第4现场设备
  178. "type": "req",
  179. "name": "songs_queue_append",
  180. "params": {
  181. "tid": "134",
  182. "vol": 80,
  183. # "vol": 10, # 基本听不见
  184. "urls": [
  185. {
  186. "name": "1",
  187. "uri": f"{self.file_service_url}/a2.mp3",
  188. # --- test ---
  189. # "uri": "http://websitecdn.gangganghao.com.cn/ls20/mp3/ei.mp3",
  190. # "uri": "http://192.168.20.231:8802/api?mp3_name=2021-10-16-11-05-29-717030.mp3",
  191. # "uri": "http://192.168.20.231:9900/2021-10-16-11-05-29-717030.mp3",
  192. # "uri": "/root/2021-10-16-11-05-29-717030.mp3",
  193. # "uri": "http://192.168.0.15:9900/2021-10-16-11-05-29-717030.mp3",
  194. # "uri": "http://192.168.51.242:9900/find-unknown-person.mp3",
  195. # "uri": "http://192.168.15.195:9900/find-unknown-person.mp3",
  196. # "uri": "http://192.168.15.195:9900/a1.mp3",
  197. },
  198. # {
  199. # "name": "2",
  200. # "uri": "http://websitecdn.gangganghao.com.cn/ls20/mp3/jxy.mp3"
  201. # }
  202. ]
  203. }
  204. }
  205. run_at = time.time()
  206. times = 0
  207. while times < 10:
  208. try:
  209. print('Api:songs_queue_append:url:', self.api_service_url)
  210. # response = requests.post(url, json=data, headers=self.headers, timeout=1)
  211. response = requests.post(self.api_service_url, json=data, headers=self.headers)
  212. print('Api:songs_queue_append:result:', response.status_code)
  213. return f"{round(time.time() - run_at, 2)}s"
  214. # except ConnectionError as e:
  215. except Exception as e:
  216. print(e)
  217. times += 1
  218. time.sleep(1)
  219. return f"{round(time.time() - run_at, 2)}s"
  220. if __name__ == '__main__':
  221. # --- init ---
  222. api = Api()
  223. # --- test --- cd /home/server/projects/taiwuict/cscec-8bur-vms/supplement-python/apis/audio && python3 api.py
  224. # out = api.file_download()
  225. out = api.songs_queue_append()
  226. print('use time:', out)