| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import concurrent.futures
- import yaml
- import os
- from api.utils import ping_ip
- from hardware.mitvs_module import mitv_controller
- from hardware.ha_devices_module import ha_device_controller
- from hardware.door_module import load_config as load_door_config
- from utils.logger_config import logger
- def check_mitv_status():
- """检查电视(Kodi)状态"""
- try:
- servers = mitv_controller.kodi_servers
- results = []
-
- with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
- future_to_server = {
- executor.submit(ping_ip, server.get('ip')): server
- for server in servers
- if server.get('ip')
- }
-
- for future in concurrent.futures.as_completed(future_to_server):
- server = future_to_server[future]
- try:
- is_online = future.result()
- results.append({
- "id": server.get('id'),
- "ip": server.get('ip'),
- "name": server.get('name', f"TV-{server.get('id')}"),
- "is_online": is_online
- })
- except Exception as e:
- logger.error(f"检查电视状态异常 (ID: {server.get('id')}): {e}")
- results.append({
- "id": server.get('id'),
- "ip": server.get('ip'),
- "is_online": False,
- "error": str(e)
- })
-
- results.sort(key=lambda x: x.get('id', 0))
- return results
- except Exception as e:
- logger.error(f"电视自检异常: {str(e)}")
- return [{"error": str(e)}]
- def check_door_status():
- """检查门禁状态"""
- try:
- config = load_door_config()
- if not config:
- return [{"error": "无法加载门禁配置"}]
-
- ip = config.get('ip')
- if ip:
- is_online = ping_ip(ip)
- return [{
- "id": 0,
- "ip": ip,
- "name": "DoorController",
- "is_online": is_online
- }]
- else:
- return [{"error": "门禁配置中缺少IP地址"}]
- except Exception as e:
- logger.error(f"门禁自检异常: {str(e)}")
- return [{"error": str(e)}]
- def check_led_status():
- """检查LED控制器状态"""
- try:
- config_path = 'led_config.yaml'
- if not os.path.exists(config_path):
- return [{"error": "LED配置文件不存在"}]
- with open(config_path, 'r', encoding='utf-8') as f:
- config = yaml.safe_load(f)
-
- if not config:
- return [{"error": "LED配置为空"}]
-
- ip = config.get('wled_ip')
-
- if ip:
- is_online = ping_ip(ip)
- return [{
- "id": 0,
- "ip": ip,
- "name": "WLEDController",
- "is_online": is_online
- }]
- else:
- return [{"error": "LED配置中缺少IP地址"}]
- except Exception as e:
- logger.error(f"LED自检异常: {str(e)}")
- return [{"error": str(e)}]
- def check_ha_status():
- """检查HA设备状态"""
- try:
- devices = ha_device_controller.devices
- results = []
-
- with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
- future_to_device = {}
- for key, device_info in devices.items():
- entity_id = device_info.get('entity_id')
- if entity_id:
- future = executor.submit(ha_device_controller._get_device_state, entity_id)
- future_to_device[future] = (key, device_info)
-
- for future in concurrent.futures.as_completed(future_to_device):
- key, device_info = future_to_device[future]
- try:
- state = future.result()
- is_online = state is not None
-
- results.append({
- "id": key,
- "name": device_info.get('name', key),
- "entity_id": device_info.get('entity_id'),
- "is_online": is_online,
- "state": state if is_online else "unknown"
- })
- except Exception as e:
- logger.error(f"检查HA设备状态异常 ({key}): {e}")
- results.append({
- "id": key,
- "name": device_info.get('name', key),
- "entity_id": device_info.get('entity_id'),
- "is_online": False,
- "error": str(e)
- })
- results.sort(key=lambda x: x.get('id', ''))
- return results
- except Exception as e:
- logger.error(f"HA设备自检异常: {str(e)}")
- return [{"error": str(e)}]
- def check_pc_status():
- """检查展厅PC状态"""
- try:
- ip = "192.168.189.250"
- is_online = ping_ip(ip)
- return [{
- "id": 0,
- "ip": ip,
- "name": "Exhibition-PC",
- "is_online": is_online
- }]
- except Exception as e:
- logger.error(f"PC自检异常: {str(e)}")
- return [{"error": str(e)}]
- def run_all_checks():
- """运行所有自检并返回汇总结果"""
- return {
- "kodi": check_mitv_status(),
- "door": check_door_status(),
- "led": check_led_status(),
- "ha": check_ha_status(),
- "pc": check_pc_status()
- }
|