led.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from flask import Blueprint, jsonify, request
  2. from application.wled_thread import start_exhibit_led_effect, stop_led_effect, is_effect_running
  3. from utils.logger_config import logger
  4. from api.utils import login_required, ping_ip
  5. import yaml
  6. import os
  7. led_bp = Blueprint('led', __name__)
  8. @led_bp.route('/api/led/status', methods=['GET'])
  9. @login_required
  10. def get_led_status():
  11. """获取LED状态"""
  12. try:
  13. is_running = is_effect_running()
  14. return jsonify({
  15. "success": True,
  16. "data": {
  17. "is_running": is_running,
  18. "message": "灯效正在运行" if is_running else "灯效已停止"
  19. }
  20. })
  21. except Exception as e:
  22. return jsonify({
  23. "success": False,
  24. "message": f"获取状态失败: {str(e)}"
  25. }), 500
  26. @led_bp.route('/api/led/start', methods=['POST'])
  27. @login_required
  28. def start_led_effect():
  29. """启动展品LED灯效控制"""
  30. try:
  31. data = request.get_json()
  32. if not data or 'exhibit_id' not in data:
  33. return jsonify({
  34. "success": False,
  35. "message": "缺少展品ID参数"
  36. }), 400
  37. exhibit_id = data['exhibit_id']
  38. if not isinstance(exhibit_id, int) or exhibit_id < 0:
  39. return jsonify({
  40. "success": False,
  41. "message": "展品ID必须是大于等于0的整数"
  42. }), 400
  43. success = start_exhibit_led_effect(exhibit_id)
  44. if success:
  45. return jsonify({
  46. "success": True,
  47. "message": f"展品 {exhibit_id} 的灯效已启动",
  48. "data": {
  49. "exhibit_id": exhibit_id,
  50. "is_running": True
  51. }
  52. })
  53. else:
  54. return jsonify({
  55. "success": False,
  56. "message": f"启动展品 {exhibit_id} 的灯效失败"
  57. }), 500
  58. except Exception as e:
  59. return jsonify({
  60. "success": False,
  61. "message": f"启动灯效失败: {str(e)}"
  62. }), 500
  63. @led_bp.route('/api/led/stop', methods=['POST'])
  64. @login_required
  65. def stop_led_effect_api():
  66. """停止当前LED灯效"""
  67. try:
  68. success = stop_led_effect()
  69. if success:
  70. return jsonify({
  71. "success": True,
  72. "message": "灯效已停止",
  73. "data": {
  74. "is_running": False
  75. }
  76. })
  77. else:
  78. return jsonify({
  79. "success": False,
  80. "message": "停止灯效失败"
  81. }), 500
  82. except Exception as e:
  83. return jsonify({
  84. "success": False,
  85. "message": f"停止灯效失败: {str(e)}"
  86. }), 500
  87. from application.self_check_service import check_led_status
  88. @led_bp.route('/api/led/self_check', methods=['POST'])
  89. @login_required
  90. def self_check_api():
  91. """LED控制器自检"""
  92. try:
  93. results = check_led_status()
  94. # 检查是否有错误返回
  95. if results and "error" in results[0]:
  96. return jsonify({
  97. "success": False,
  98. "message": results[0]["error"],
  99. "data": []
  100. }), 500
  101. return jsonify({
  102. "success": True,
  103. "message": "LED自检完成",
  104. "data": results
  105. })
  106. except Exception as e:
  107. logger.error(f"LED自检异常: {str(e)}")
  108. return jsonify({
  109. "success": False,
  110. "message": f"LED自检失败: {str(e)}"
  111. }), 500