| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- 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
|