self_check_service.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import concurrent.futures
  2. import yaml
  3. import os
  4. from api.utils import ping_ip
  5. from hardware.mitvs_module import mitv_controller
  6. from hardware.ha_devices_module import ha_device_controller
  7. from hardware.door_module import load_config as load_door_config
  8. from utils.logger_config import logger
  9. def check_mitv_status():
  10. """检查电视(Kodi)状态"""
  11. try:
  12. servers = mitv_controller.kodi_servers
  13. results = []
  14. with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
  15. future_to_server = {
  16. executor.submit(ping_ip, server.get('ip')): server
  17. for server in servers
  18. if server.get('ip')
  19. }
  20. for future in concurrent.futures.as_completed(future_to_server):
  21. server = future_to_server[future]
  22. try:
  23. is_online = future.result()
  24. results.append({
  25. "id": server.get('id'),
  26. "ip": server.get('ip'),
  27. "name": server.get('name', f"TV-{server.get('id')}"),
  28. "is_online": is_online
  29. })
  30. except Exception as e:
  31. logger.error(f"检查电视状态异常 (ID: {server.get('id')}): {e}")
  32. results.append({
  33. "id": server.get('id'),
  34. "ip": server.get('ip'),
  35. "is_online": False,
  36. "error": str(e)
  37. })
  38. results.sort(key=lambda x: x.get('id', 0))
  39. return results
  40. except Exception as e:
  41. logger.error(f"电视自检异常: {str(e)}")
  42. return [{"error": str(e)}]
  43. def check_door_status():
  44. """检查门禁状态"""
  45. try:
  46. config = load_door_config()
  47. if not config:
  48. return [{"error": "无法加载门禁配置"}]
  49. ip = config.get('ip')
  50. if ip:
  51. is_online = ping_ip(ip)
  52. return [{
  53. "id": 0,
  54. "ip": ip,
  55. "name": "DoorController",
  56. "is_online": is_online
  57. }]
  58. else:
  59. return [{"error": "门禁配置中缺少IP地址"}]
  60. except Exception as e:
  61. logger.error(f"门禁自检异常: {str(e)}")
  62. return [{"error": str(e)}]
  63. def check_led_status():
  64. """检查LED控制器状态"""
  65. try:
  66. config_path = 'led_config.yaml'
  67. if not os.path.exists(config_path):
  68. return [{"error": "LED配置文件不存在"}]
  69. with open(config_path, 'r', encoding='utf-8') as f:
  70. config = yaml.safe_load(f)
  71. if not config:
  72. return [{"error": "LED配置为空"}]
  73. ip = config.get('wled_ip')
  74. if ip:
  75. is_online = ping_ip(ip)
  76. return [{
  77. "id": 0,
  78. "ip": ip,
  79. "name": "WLEDController",
  80. "is_online": is_online
  81. }]
  82. else:
  83. return [{"error": "LED配置中缺少IP地址"}]
  84. except Exception as e:
  85. logger.error(f"LED自检异常: {str(e)}")
  86. return [{"error": str(e)}]
  87. def check_ha_status():
  88. """检查HA设备状态"""
  89. try:
  90. devices = ha_device_controller.devices
  91. results = []
  92. with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
  93. future_to_device = {}
  94. for key, device_info in devices.items():
  95. entity_id = device_info.get('entity_id')
  96. if entity_id:
  97. future = executor.submit(ha_device_controller._get_device_state, entity_id)
  98. future_to_device[future] = (key, device_info)
  99. for future in concurrent.futures.as_completed(future_to_device):
  100. key, device_info = future_to_device[future]
  101. try:
  102. state = future.result()
  103. is_online = state is not None
  104. results.append({
  105. "id": key,
  106. "name": device_info.get('name', key),
  107. "entity_id": device_info.get('entity_id'),
  108. "is_online": is_online,
  109. "state": state if is_online else "unknown"
  110. })
  111. except Exception as e:
  112. logger.error(f"检查HA设备状态异常 ({key}): {e}")
  113. results.append({
  114. "id": key,
  115. "name": device_info.get('name', key),
  116. "entity_id": device_info.get('entity_id'),
  117. "is_online": False,
  118. "error": str(e)
  119. })
  120. results.sort(key=lambda x: x.get('id', ''))
  121. return results
  122. except Exception as e:
  123. logger.error(f"HA设备自检异常: {str(e)}")
  124. return [{"error": str(e)}]
  125. def check_pc_status():
  126. """检查展厅PC状态"""
  127. try:
  128. ip = "192.168.189.250"
  129. is_online = ping_ip(ip)
  130. return [{
  131. "id": 0,
  132. "ip": ip,
  133. "name": "Exhibition-PC",
  134. "is_online": is_online
  135. }]
  136. except Exception as e:
  137. logger.error(f"PC自检异常: {str(e)}")
  138. return [{"error": str(e)}]
  139. def run_all_checks():
  140. """运行所有自检并返回汇总结果"""
  141. return {
  142. "kodi": check_mitv_status(),
  143. "door": check_door_status(),
  144. "led": check_led_status(),
  145. "ha": check_ha_status(),
  146. "pc": check_pc_status()
  147. }