led.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  5. led_bp = Blueprint('led', __name__)
  6. @led_bp.route('/api/led/status', methods=['GET'])
  7. @login_required
  8. def get_led_status():
  9. """获取LED状态"""
  10. try:
  11. is_running = is_effect_running()
  12. return jsonify({
  13. "success": True,
  14. "data": {
  15. "is_running": is_running,
  16. "message": "灯效正在运行" if is_running else "灯效已停止"
  17. }
  18. })
  19. except Exception as e:
  20. return jsonify({
  21. "success": False,
  22. "message": f"获取状态失败: {str(e)}"
  23. }), 500
  24. @led_bp.route('/api/led/start', methods=['POST'])
  25. @login_required
  26. def start_led_effect():
  27. """启动展品LED灯效控制"""
  28. try:
  29. data = request.get_json()
  30. if not data or 'exhibit_id' not in data:
  31. return jsonify({
  32. "success": False,
  33. "message": "缺少展品ID参数"
  34. }), 400
  35. exhibit_id = data['exhibit_id']
  36. if not isinstance(exhibit_id, int) or exhibit_id < 0:
  37. return jsonify({
  38. "success": False,
  39. "message": "展品ID必须是大于等于0的整数"
  40. }), 400
  41. success = start_exhibit_led_effect(exhibit_id)
  42. if success:
  43. return jsonify({
  44. "success": True,
  45. "message": f"展品 {exhibit_id} 的灯效已启动",
  46. "data": {
  47. "exhibit_id": exhibit_id,
  48. "is_running": True
  49. }
  50. })
  51. else:
  52. return jsonify({
  53. "success": False,
  54. "message": f"启动展品 {exhibit_id} 的灯效失败"
  55. }), 500
  56. except Exception as e:
  57. return jsonify({
  58. "success": False,
  59. "message": f"启动灯效失败: {str(e)}"
  60. }), 500
  61. @led_bp.route('/api/led/stop', methods=['POST'])
  62. @login_required
  63. def stop_led_effect_api():
  64. """停止当前LED灯效"""
  65. try:
  66. success = stop_led_effect()
  67. if success:
  68. return jsonify({
  69. "success": True,
  70. "message": "灯效已停止",
  71. "data": {
  72. "is_running": False
  73. }
  74. })
  75. else:
  76. return jsonify({
  77. "success": False,
  78. "message": "停止灯效失败"
  79. }), 500
  80. except Exception as e:
  81. return jsonify({
  82. "success": False,
  83. "message": f"停止灯效失败: {str(e)}"
  84. }), 500