sound_columns.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from hub import methods, Global
  2. import threading
  3. import time
  4. class SoundColumns(object):
  5. """音柱循环检查"""
  6. mdb = Global.get_mongodb_client()
  7. api = Global.get_audio_client()
  8. @classmethod
  9. def get_mp3_name_dict(cls):
  10. mp3_name_dict = dict()
  11. """
  12. FaceType: 人脸类型表
  13. FaceType.name: 类型名称
  14. FaceType.mp3_name: 音频名称
  15. """
  16. for item in Global.mdb.get_all('FaceType'):
  17. uuid = str(item.get('_id'))
  18. mp3_name_dict[uuid] = item.get('mp3_name')
  19. return mp3_name_dict
  20. @classmethod
  21. def get_column_service_url_and_sn(cls):
  22. """
  23. 获取音柱信息 todo 建议增加ip校验
  24. """
  25. unique_dict = {'name': 'AudioConfig'}
  26. item = cls.mdb.get_one('GlobalVariable', unique_dict)
  27. data = item.get('args', {})
  28. audio_sn = data.get('audio_sn')
  29. audio_url = f"http://{data.get('audio_ipv4')}:{data.get('audio_port')}"
  30. audio_vol = data.get('audio_vol')
  31. return audio_url, audio_sn, audio_vol
  32. @classmethod
  33. def get_host_ip(cls):
  34. """
  35. 获取本机ip
  36. """
  37. unique_dict = {'name': 'HostIpConfig'}
  38. item = cls.mdb.get_one('GlobalVariable', unique_dict)
  39. data = item.get('args', {})
  40. host_ip = data.get('ipv4')
  41. # --- check ---
  42. if not host_ip:
  43. ssh = Global.SSHClient('172.18.0.1', 22, 'server', 'server')
  44. out = ssh.run_command("ifconfig")
  45. for row in out.split('\n\n'):
  46. if not row.startswith('eth0'): # eth0 enp0s3
  47. continue
  48. for one in row.split('\n'):
  49. one = one.strip()
  50. if one[:4] != 'inet':
  51. continue
  52. if one[:5] == 'inet6':
  53. continue
  54. one = [i for i in one.split(' ') if i]
  55. ipv4, netmask = one[1], one[3]
  56. host_ip = ipv4
  57. return host_ip
  58. @classmethod
  59. def check_loop(cls):
  60. # --- define ---
  61. last_send_id = str()
  62. while True:
  63. # --- check --- 检查是否配置音柱
  64. # while True:
  65. # unique_dict = {'name': 'AudioConfig'}
  66. # item = cls.mdb.get_one('GlobalVariable', unique_dict)
  67. # data = item.get('args', {})
  68. # audio_ipv4 = data.get('audio_ipv4')
  69. # if audio_ipv4:
  70. # break
  71. # else:
  72. # time.sleep(60)
  73. try:
  74. # --- get send_data ---
  75. send_data = Global.rdb.get_one(key='send_data')
  76. # --- check ---
  77. if not send_data:
  78. continue
  79. # --- check ---
  80. send_id = send_data.get('send_id')
  81. if not send_id:
  82. continue
  83. # --- check ---
  84. if send_id == last_send_id:
  85. continue
  86. # --- check ---
  87. """
  88. send_list = [
  89. {
  90. base_face_uuid: 底库人脸id
  91. snap_face_image: 抓拍人脸
  92. base_face_image_path: 底库人脸路径
  93. face_similarity: 相似度
  94. }
  95. ]
  96. """
  97. send_list = send_data.get('send_list')
  98. if send_list is None or len(send_list) == 0:
  99. continue
  100. # --- debug ---
  101. # methods.debug_log(f"SoundColumns", f"m-122: run at {methods.now_string()} "
  102. # f"| send count is {len(send_list)} ")
  103. # --- update ---
  104. last_send_id = send_id
  105. # --- call --- todo 建议界面上添加个开关,是否启用音响
  106. """增加音柱配置写活"""
  107. audio_url, audio_sn, audio_vol = cls.get_column_service_url_and_sn()
  108. cls.api.api_service_url = audio_url
  109. # cls.api.sn = audio_sn
  110. cls.api.vol = audio_vol
  111. host_ip = cls.get_host_ip()
  112. cls.api.file_service_url = f'http://{host_ip}:9900'
  113. methods.debug_log(f"SoundColumns", f"m-96: file_service_url -> {cls.api.file_service_url}")
  114. for data in send_list:
  115. result_type = data.get('result_type')
  116. # result = cls.api.call_audio_make_sound_v3(result_type)
  117. result = cls.api.call_audio_make_sound_v4(result_type)
  118. methods.debug_log(f"SoundColumns", f"m-131: result is {result} | run at {methods.now_string()} |"
  119. f"send count is {len(send_list)}")
  120. except Exception as exception:
  121. methods.debug_log('SoundColumns', f"m-153: exception | {exception}")
  122. methods.debug_log('SoundColumns', f"m-153: wait 10 minutes try again!")
  123. time.sleep(600)
  124. continue
  125. @classmethod
  126. def run_background(cls, is_back_run=True):
  127. """"""
  128. p1 = threading.Thread(target=cls.check_loop)
  129. p1.start()
  130. if __name__ == '__main__':
  131. SoundColumns.run_background()