ha.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. from flask import Blueprint, jsonify, request
  2. from hardware.ha_devices_module import (
  3. turn_on_entrance_lights, turn_off_entrance_lights,
  4. turn_on_exhibition_spotlight, turn_off_exhibition_spotlight,
  5. turn_on_exhibition_ceiling_lights, turn_off_exhibition_ceiling_lights, set_exhibition_ceiling_light_level,
  6. turn_on_exhibition_desktop_switch, turn_off_exhibition_desktop_switch,
  7. turn_on_exhibition_3d_fan, turn_off_exhibition_3d_fan,
  8. turn_on_exhibition_stand_light_strip, turn_off_exhibition_stand_light_strip,
  9. turn_on_all, turn_off_all
  10. )
  11. from utils.logger_config import logger
  12. from api.utils import login_required
  13. import threading
  14. ha_bp = Blueprint('ha', __name__)
  15. @ha_bp.route('/api/ha/entrance_lights/turn_on', methods=['POST'])
  16. @login_required
  17. def turn_on_entrance_lights_api():
  18. """打开一楼大门玄关顶灯"""
  19. try:
  20. def task():
  21. try:
  22. turn_on_entrance_lights()
  23. except Exception as e:
  24. logger.error(f"异步打开一楼大门玄关顶灯异常: {str(e)}")
  25. threading.Thread(target=task).start()
  26. return jsonify({
  27. "success": True,
  28. "message": "已发送打开一楼大门玄关顶灯指令 - 异步执行"
  29. })
  30. except Exception as e:
  31. logger.error(f"打开一楼大门玄关顶灯异常: {str(e)}")
  32. return jsonify({
  33. "success": False,
  34. "message": f"打开一楼大门玄关顶灯失败: {str(e)}"
  35. }), 500
  36. @ha_bp.route('/api/ha/entrance_lights/turn_off', methods=['POST'])
  37. @login_required
  38. def turn_off_entrance_lights_api():
  39. """关闭一楼大门玄关顶灯"""
  40. try:
  41. def task():
  42. try:
  43. turn_off_entrance_lights()
  44. except Exception as e:
  45. logger.error(f"异步关闭一楼大门玄关顶灯异常: {str(e)}")
  46. threading.Thread(target=task).start()
  47. return jsonify({
  48. "success": True,
  49. "message": "已发送关闭一楼大门玄关顶灯指令 - 异步执行"
  50. })
  51. except Exception as e:
  52. logger.error(f"关闭一楼大门玄关顶灯异常: {str(e)}")
  53. return jsonify({
  54. "success": False,
  55. "message": f"关闭一楼大门玄关顶灯失败: {str(e)}"
  56. }), 500
  57. @ha_bp.route('/api/ha/exhibition_spotlight/turn_on', methods=['POST'])
  58. @login_required
  59. def turn_on_exhibition_spotlight_api():
  60. """打开一楼大门玄关射灯"""
  61. try:
  62. def task():
  63. try:
  64. turn_on_exhibition_spotlight()
  65. except Exception as e:
  66. logger.error(f"异步打开一楼大门玄关射灯异常: {str(e)}")
  67. threading.Thread(target=task).start()
  68. return jsonify({
  69. "success": True,
  70. "message": "已发送打开一楼大门玄关射灯指令 - 异步执行"
  71. })
  72. except Exception as e:
  73. logger.error(f"打开一楼大门玄关射灯异常: {str(e)}")
  74. return jsonify({
  75. "success": False,
  76. "message": f"打开一楼大门玄关射灯失败: {str(e)}"
  77. }), 500
  78. @ha_bp.route('/api/ha/exhibition_spotlight/turn_off', methods=['POST'])
  79. @login_required
  80. def turn_off_exhibition_spotlight_api():
  81. """关闭一楼大门玄关射灯"""
  82. try:
  83. def task():
  84. try:
  85. turn_off_exhibition_spotlight()
  86. except Exception as e:
  87. logger.error(f"异步关闭一楼大门玄关射灯异常: {str(e)}")
  88. threading.Thread(target=task).start()
  89. return jsonify({
  90. "success": True,
  91. "message": "已发送关闭一楼大门玄关射灯指令 - 异步执行"
  92. })
  93. except Exception as e:
  94. logger.error(f"关闭一楼大门玄关射灯异常: {str(e)}")
  95. return jsonify({
  96. "success": False,
  97. "message": f"关闭一楼大门玄关射灯失败: {str(e)}"
  98. }), 500
  99. @ha_bp.route('/api/ha/exhibition_ceiling_lights/turn_on', methods=['POST'])
  100. @login_required
  101. def turn_on_exhibition_ceiling_lights_api():
  102. """打开一楼展厅顶灯"""
  103. try:
  104. def task():
  105. try:
  106. turn_on_exhibition_ceiling_lights()
  107. except Exception as e:
  108. logger.error(f"异步打开一楼展厅顶灯异常: {str(e)}")
  109. threading.Thread(target=task).start()
  110. return jsonify({
  111. "success": True,
  112. "message": "已发送打开一楼展厅顶灯指令 - 异步执行"
  113. })
  114. except Exception as e:
  115. logger.error(f"打开一楼展厅顶灯异常: {str(e)}")
  116. return jsonify({
  117. "success": False,
  118. "message": f"打开一楼展厅顶灯失败: {str(e)}"
  119. }), 500
  120. @ha_bp.route('/api/ha/exhibition_ceiling_lights/turn_off', methods=['POST'])
  121. @login_required
  122. def turn_off_exhibition_ceiling_lights_api():
  123. """关闭一楼展厅顶灯"""
  124. try:
  125. def task():
  126. try:
  127. turn_off_exhibition_ceiling_lights()
  128. except Exception as e:
  129. logger.error(f"异步关闭一楼展厅顶灯异常: {str(e)}")
  130. threading.Thread(target=task).start()
  131. return jsonify({
  132. "success": True,
  133. "message": "已发送关闭一楼展厅顶灯指令 - 异步执行"
  134. })
  135. except Exception as e:
  136. logger.error(f"关闭一楼展厅顶灯异常: {str(e)}")
  137. return jsonify({
  138. "success": False,
  139. "message": f"关闭一楼展厅顶灯失败: {str(e)}"
  140. }), 500
  141. @ha_bp.route('/api/ha/exhibition_ceiling_lights/set_level', methods=['POST'])
  142. @login_required
  143. def set_exhibition_ceiling_light_level_api():
  144. """设置一楼展厅顶灯等级 (0-3)"""
  145. try:
  146. data = request.get_json()
  147. if not data or 'level' not in data:
  148. return jsonify({"success": False, "message": "缺少参数 level"}), 400
  149. level = data['level']
  150. def task():
  151. try:
  152. set_exhibition_ceiling_light_level(level)
  153. except Exception as e:
  154. logger.error(f"异步设置一楼展厅顶灯等级异常: {str(e)}")
  155. threading.Thread(target=task).start()
  156. return jsonify({
  157. "success": True,
  158. "message": f"已发送设置一楼展厅顶灯等级指令 ({level}) - 异步执行"
  159. })
  160. except Exception as e:
  161. logger.error(f"设置一楼展厅顶灯等级异常: {str(e)}")
  162. return jsonify({
  163. "success": False,
  164. "message": f"设置一楼展厅顶灯等级失败: {str(e)}"
  165. }), 500
  166. @ha_bp.route('/api/ha/exhibition_desktop_switch/turn_on', methods=['POST'])
  167. @login_required
  168. def turn_on_exhibition_desktop_switch_api():
  169. """打开展厅桌面的灯座总开关"""
  170. try:
  171. def task():
  172. try:
  173. turn_on_exhibition_desktop_switch()
  174. except Exception as e:
  175. logger.error(f"异步打开展厅桌面的灯座总开关异常: {str(e)}")
  176. threading.Thread(target=task).start()
  177. return jsonify({
  178. "success": True,
  179. "message": "已发送打开展厅桌面的灯座总开关指令 - 异步执行"
  180. })
  181. except Exception as e:
  182. logger.error(f"打开展厅桌面的灯座总开关异常: {str(e)}")
  183. return jsonify({
  184. "success": False,
  185. "message": f"打开展厅桌面的灯座总开关失败: {str(e)}"
  186. }), 500
  187. @ha_bp.route('/api/ha/exhibition_desktop_switch/turn_off', methods=['POST'])
  188. @login_required
  189. def turn_off_exhibition_desktop_switch_api():
  190. """关闭展厅桌面的灯座总开关"""
  191. try:
  192. def task():
  193. try:
  194. turn_off_exhibition_desktop_switch()
  195. except Exception as e:
  196. logger.error(f"异步关闭展厅桌面的灯座总开关异常: {str(e)}")
  197. threading.Thread(target=task).start()
  198. return jsonify({
  199. "success": True,
  200. "message": "已发送关闭展厅桌面的灯座总开关指令 - 异步执行"
  201. })
  202. except Exception as e:
  203. logger.error(f"关闭展厅桌面的灯座总开关异常: {str(e)}")
  204. return jsonify({
  205. "success": False,
  206. "message": f"关闭展厅桌面的灯座总开关失败: {str(e)}"
  207. }), 500
  208. @ha_bp.route('/api/ha/exhibition_3d_fan/turn_on', methods=['POST'])
  209. @login_required
  210. def turn_on_exhibition_3d_fan_api():
  211. """打开展厅桌面3D风扇投影"""
  212. try:
  213. def task():
  214. try:
  215. turn_on_exhibition_3d_fan()
  216. except Exception as e:
  217. logger.error(f"异步打开展厅桌面3D风扇投影异常: {str(e)}")
  218. threading.Thread(target=task).start()
  219. return jsonify({
  220. "success": True,
  221. "message": "已发送打开展厅桌面3D风扇投影指令 - 异步执行"
  222. })
  223. except Exception as e:
  224. logger.error(f"打开展厅桌面3D风扇投影异常: {str(e)}")
  225. return jsonify({
  226. "success": False,
  227. "message": f"打开展厅桌面3D风扇投影失败: {str(e)}"
  228. }), 500
  229. @ha_bp.route('/api/ha/exhibition_3d_fan/turn_off', methods=['POST'])
  230. @login_required
  231. def turn_off_exhibition_3d_fan_api():
  232. """关闭展厅桌面3D风扇投影"""
  233. try:
  234. def task():
  235. try:
  236. turn_off_exhibition_3d_fan()
  237. except Exception as e:
  238. logger.error(f"异步关闭展厅桌面3D风扇投影异常: {str(e)}")
  239. threading.Thread(target=task).start()
  240. return jsonify({
  241. "success": True,
  242. "message": "已发送关闭展厅桌面3D风扇投影指令 - 异步执行"
  243. })
  244. except Exception as e:
  245. logger.error(f"关闭展厅桌面3D风扇投影异常: {str(e)}")
  246. return jsonify({
  247. "success": False,
  248. "message": f"关闭展厅桌面3D风扇投影失败: {str(e)}"
  249. }), 500
  250. @ha_bp.route('/api/ha/exhibition_stand_light_strip/turn_on', methods=['POST'])
  251. @login_required
  252. def turn_on_exhibition_stand_light_strip_api():
  253. """打开展台桌子灯带"""
  254. try:
  255. def task():
  256. try:
  257. turn_on_exhibition_stand_light_strip()
  258. except Exception as e:
  259. logger.error(f"异步打开展台桌子灯带异常: {str(e)}")
  260. threading.Thread(target=task).start()
  261. return jsonify({
  262. "success": True,
  263. "message": "已发送打开展台桌子灯带指令 - 异步执行"
  264. })
  265. except Exception as e:
  266. logger.error(f"打开展台桌子灯带异常: {str(e)}")
  267. return jsonify({
  268. "success": False,
  269. "message": f"打开展台桌子灯带失败: {str(e)}"
  270. }), 500
  271. @ha_bp.route('/api/ha/exhibition_stand_light_strip/turn_off', methods=['POST'])
  272. @login_required
  273. def turn_off_exhibition_stand_light_strip_api():
  274. """关闭展台桌子灯带"""
  275. try:
  276. def task():
  277. try:
  278. turn_off_exhibition_stand_light_strip()
  279. except Exception as e:
  280. logger.error(f"异步关闭展台桌子灯带异常: {str(e)}")
  281. threading.Thread(target=task).start()
  282. return jsonify({
  283. "success": True,
  284. "message": "已发送关闭展台桌子灯带指令 - 异步执行"
  285. })
  286. except Exception as e:
  287. logger.error(f"关闭展台桌子灯带异常: {str(e)}")
  288. return jsonify({
  289. "success": False,
  290. "message": f"关闭展台桌子灯带失败: {str(e)}"
  291. }), 500
  292. @ha_bp.route('/api/ha/turn_on_all', methods=['POST'])
  293. @login_required
  294. def ha_turn_on_all_api():
  295. """打开所有HA设备"""
  296. try:
  297. def task():
  298. try:
  299. turn_on_all()
  300. except Exception as e:
  301. logger.error(f"异步打开所有HA设备异常: {str(e)}")
  302. threading.Thread(target=task).start()
  303. return jsonify({
  304. "success": True,
  305. "message": "已发送打开所有HA设备指令 - 异步执行"
  306. })
  307. except Exception as e:
  308. logger.error(f"打开所有HA设备异常: {str(e)}")
  309. return jsonify({
  310. "success": False,
  311. "message": f"打开所有HA设备失败: {str(e)}"
  312. }), 500
  313. @ha_bp.route('/api/ha/turn_off_all', methods=['POST'])
  314. @login_required
  315. def ha_turn_off_all_api():
  316. """关闭所有HA设备"""
  317. try:
  318. def task():
  319. try:
  320. turn_off_all()
  321. except Exception as e:
  322. logger.error(f"异步关闭所有HA设备异常: {str(e)}")
  323. threading.Thread(target=task).start()
  324. return jsonify({
  325. "success": True,
  326. "message": "已发送关闭所有HA设备指令 - 异步执行"
  327. })
  328. except Exception as e:
  329. logger.error(f"关闭所有HA设备异常: {str(e)}")
  330. return jsonify({
  331. "success": False,
  332. "message": f"关闭所有HA设备失败: {str(e)}"
  333. }), 500