from flask import Blueprint, jsonify, request from hardware.ha_devices_module import ( turn_on_entrance_lights, turn_off_entrance_lights, turn_on_exhibition_spotlight, turn_off_exhibition_spotlight, turn_on_exhibition_ceiling_lights, turn_off_exhibition_ceiling_lights, set_exhibition_ceiling_light_level, turn_on_exhibition_desktop_switch, turn_off_exhibition_desktop_switch, turn_on_exhibition_3d_fan, turn_off_exhibition_3d_fan, turn_on_exhibition_stand_light_strip, turn_off_exhibition_stand_light_strip, turn_on_all, turn_off_all, ha_device_controller ) from utils.logger_config import logger from api.utils import login_required import threading import concurrent.futures ha_bp = Blueprint('ha', __name__) @ha_bp.route('/api/ha/entrance_lights/turn_on', methods=['POST']) @login_required def turn_on_entrance_lights_api(): """打开一楼大门玄关顶灯""" try: def task(): try: turn_on_entrance_lights() except Exception as e: logger.error(f"异步打开一楼大门玄关顶灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开一楼大门玄关顶灯指令 - 异步执行" }) except Exception as e: logger.error(f"打开一楼大门玄关顶灯异常: {str(e)}") return jsonify({ "success": False, "message": f"打开一楼大门玄关顶灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/entrance_lights/turn_off', methods=['POST']) @login_required def turn_off_entrance_lights_api(): """关闭一楼大门玄关顶灯""" try: def task(): try: turn_off_entrance_lights() except Exception as e: logger.error(f"异步关闭一楼大门玄关顶灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭一楼大门玄关顶灯指令 - 异步执行" }) except Exception as e: logger.error(f"关闭一楼大门玄关顶灯异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭一楼大门玄关顶灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_spotlight/turn_on', methods=['POST']) @login_required def turn_on_exhibition_spotlight_api(): """打开一楼大门玄关射灯""" try: def task(): try: turn_on_exhibition_spotlight() except Exception as e: logger.error(f"异步打开一楼大门玄关射灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开一楼大门玄关射灯指令 - 异步执行" }) except Exception as e: logger.error(f"打开一楼大门玄关射灯异常: {str(e)}") return jsonify({ "success": False, "message": f"打开一楼大门玄关射灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_spotlight/turn_off', methods=['POST']) @login_required def turn_off_exhibition_spotlight_api(): """关闭一楼大门玄关射灯""" try: def task(): try: turn_off_exhibition_spotlight() except Exception as e: logger.error(f"异步关闭一楼大门玄关射灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭一楼大门玄关射灯指令 - 异步执行" }) except Exception as e: logger.error(f"关闭一楼大门玄关射灯异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭一楼大门玄关射灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_ceiling_lights/turn_on', methods=['POST']) @login_required def turn_on_exhibition_ceiling_lights_api(): """打开一楼展厅顶灯""" try: def task(): try: turn_on_exhibition_ceiling_lights() except Exception as e: logger.error(f"异步打开一楼展厅顶灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开一楼展厅顶灯指令 - 异步执行" }) except Exception as e: logger.error(f"打开一楼展厅顶灯异常: {str(e)}") return jsonify({ "success": False, "message": f"打开一楼展厅顶灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_ceiling_lights/turn_off', methods=['POST']) @login_required def turn_off_exhibition_ceiling_lights_api(): """关闭一楼展厅顶灯""" try: def task(): try: turn_off_exhibition_ceiling_lights() except Exception as e: logger.error(f"异步关闭一楼展厅顶灯异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭一楼展厅顶灯指令 - 异步执行" }) except Exception as e: logger.error(f"关闭一楼展厅顶灯异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭一楼展厅顶灯失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_ceiling_lights/set_level', methods=['POST']) @login_required def set_exhibition_ceiling_light_level_api(): """设置一楼展厅顶灯等级 (0-3)""" try: data = request.get_json() if not data or 'level' not in data: return jsonify({"success": False, "message": "缺少参数 level"}), 400 level = data['level'] def task(): try: set_exhibition_ceiling_light_level(level) except Exception as e: logger.error(f"异步设置一楼展厅顶灯等级异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": f"已发送设置一楼展厅顶灯等级指令 ({level}) - 异步执行" }) except Exception as e: logger.error(f"设置一楼展厅顶灯等级异常: {str(e)}") return jsonify({ "success": False, "message": f"设置一楼展厅顶灯等级失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_desktop_switch/turn_on', methods=['POST']) @login_required def turn_on_exhibition_desktop_switch_api(): """打开展厅桌面的灯座总开关""" try: def task(): try: turn_on_exhibition_desktop_switch() except Exception as e: logger.error(f"异步打开展厅桌面的灯座总开关异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开展厅桌面的灯座总开关指令 - 异步执行" }) except Exception as e: logger.error(f"打开展厅桌面的灯座总开关异常: {str(e)}") return jsonify({ "success": False, "message": f"打开展厅桌面的灯座总开关失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_desktop_switch/turn_off', methods=['POST']) @login_required def turn_off_exhibition_desktop_switch_api(): """关闭展厅桌面的灯座总开关""" try: def task(): try: turn_off_exhibition_desktop_switch() except Exception as e: logger.error(f"异步关闭展厅桌面的灯座总开关异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭展厅桌面的灯座总开关指令 - 异步执行" }) except Exception as e: logger.error(f"关闭展厅桌面的灯座总开关异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭展厅桌面的灯座总开关失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_3d_fan/turn_on', methods=['POST']) @login_required def turn_on_exhibition_3d_fan_api(): """打开展厅桌面3D风扇投影""" try: def task(): try: turn_on_exhibition_3d_fan() except Exception as e: logger.error(f"异步打开展厅桌面3D风扇投影异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开展厅桌面3D风扇投影指令 - 异步执行" }) except Exception as e: logger.error(f"打开展厅桌面3D风扇投影异常: {str(e)}") return jsonify({ "success": False, "message": f"打开展厅桌面3D风扇投影失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_3d_fan/turn_off', methods=['POST']) @login_required def turn_off_exhibition_3d_fan_api(): """关闭展厅桌面3D风扇投影""" try: def task(): try: turn_off_exhibition_3d_fan() except Exception as e: logger.error(f"异步关闭展厅桌面3D风扇投影异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭展厅桌面3D风扇投影指令 - 异步执行" }) except Exception as e: logger.error(f"关闭展厅桌面3D风扇投影异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭展厅桌面3D风扇投影失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_stand_light_strip/turn_on', methods=['POST']) @login_required def turn_on_exhibition_stand_light_strip_api(): """打开展台桌子灯带""" try: def task(): try: turn_on_exhibition_stand_light_strip() except Exception as e: logger.error(f"异步打开展台桌子灯带异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开展台桌子灯带指令 - 异步执行" }) except Exception as e: logger.error(f"打开展台桌子灯带异常: {str(e)}") return jsonify({ "success": False, "message": f"打开展台桌子灯带失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/exhibition_stand_light_strip/turn_off', methods=['POST']) @login_required def turn_off_exhibition_stand_light_strip_api(): """关闭展台桌子灯带""" try: def task(): try: turn_off_exhibition_stand_light_strip() except Exception as e: logger.error(f"异步关闭展台桌子灯带异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭展台桌子灯带指令 - 异步执行" }) except Exception as e: logger.error(f"关闭展台桌子灯带异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭展台桌子灯带失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/turn_on_all', methods=['POST']) @login_required def ha_turn_on_all_api(): """打开所有HA设备""" try: def task(): try: turn_on_all() except Exception as e: logger.error(f"异步打开所有HA设备异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送打开所有HA设备指令 - 异步执行" }) except Exception as e: logger.error(f"打开所有HA设备异常: {str(e)}") return jsonify({ "success": False, "message": f"打开所有HA设备失败: {str(e)}" }), 500 @ha_bp.route('/api/ha/turn_off_all', methods=['POST']) @login_required def ha_turn_off_all_api(): """关闭所有HA设备""" try: def task(): try: turn_off_all() except Exception as e: logger.error(f"异步关闭所有HA设备异常: {str(e)}") threading.Thread(target=task).start() return jsonify({ "success": True, "message": "已发送关闭所有HA设备指令 - 异步执行" }) except Exception as e: logger.error(f"关闭所有HA设备异常: {str(e)}") return jsonify({ "success": False, "message": f"关闭所有HA设备失败: {str(e)}" }), 500 from application.self_check_service import check_ha_status @ha_bp.route('/api/ha/self_check', methods=['POST']) @login_required def self_check_api(): """ HA设备自检 检查所有配置的HA设备是否可用(通过调用 HA API 获取状态) """ try: results = check_ha_status() # 检查是否有错误返回 if results and "error" in results[0]: return jsonify({ "success": False, "message": results[0]["error"], "data": [] }), 500 return jsonify({ "success": True, "message": "HA设备自检完成", "data": results }) except Exception as e: logger.error(f"HA设备自检异常: {str(e)}") return jsonify({ "success": False, "message": f"HA设备自检失败: {str(e)}" }), 500