auto_switch_yuntaideng.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @CreateTime: 2021/10/13 10:58
  5. @Author: lxc
  6. @LastEditTime:
  7. @Desctiption: 水下云台灯
  8. """
  9. from event_storage import EventStorage
  10. from log import OutPutLog
  11. import time
  12. from datetime import datetime
  13. from utility import Utility
  14. import json
  15. class AutoSwitchYuntaideng:
  16. def __init__(self):
  17. self._storage = EventStorage()
  18. self._log = OutPutLog()
  19. def run(self):
  20. self._log.info("[AutoSwitchDevice] - AutoSwitchDevice module is running!")
  21. run_time = 0
  22. while True:
  23. this_time = time.time()
  24. if this_time - run_time > 60:
  25. run_time = this_time
  26. self.update_yuntaideng_status()
  27. def get_real_status(self):
  28. """
  29. 获取水下云台灯的实时状态
  30. return:{'shuixiayuntai01': '1', 'shuixiayuntai02': None, 'shuixiayuntai03': None,...}
  31. """
  32. juyudeng_point_sql = "SELECT serial_number,io_point_name FROM `data_point_tbl` WHERE `io_point_name` LIKE 'shuixiayuntai%_status';"
  33. juyudeng_point = self._storage.execute_sql(juyudeng_point_sql)
  34. read_real_list = []
  35. point_name_dict = {}
  36. for each in juyudeng_point:
  37. serial_number = "c"+str(each["serial_number"])
  38. read_real_list.append(serial_number)
  39. juyudeng_name = each["io_point_name"].replace("_status", "")
  40. point_name_dict[serial_number] = juyudeng_name
  41. real_data_dict = self._storage.get_real_data(read_real_list)
  42. light_real_status = {}
  43. for k,v in real_data_dict.items():
  44. light_real_status[point_name_dict[k]] = v
  45. return light_real_status
  46. def update_yuntaideng_status(self):
  47. """判断水下云台灯的状态是否需要修改"""
  48. # # 获取水下云台灯的实时状态
  49. # all_light_real_status = self.get_real_status() # {'shuixiayuntai01': '0', 'shuixiayuntai02': None, 'shuixiayuntai03': None, 'shuixiayuntai04': None}
  50. # #print(all_light_real_status)
  51. # 判断水下云台灯的状态是否需要修改
  52. yuntaideng_info_sql = "SELECT device_name,control_mode,definite_time,commands FROM `shuixiayuntaideng_control`;"
  53. get_yuntaideng_info = self._storage.execute_sql(yuntaideng_info_sql)
  54. for yuntaideng_info in get_yuntaideng_info:
  55. # {'device_name': 'shuixiayuntai01', 'control_mode': 0, 'definite_time': '[["06:00:00,07:59:59"]]\t', 'commands': '[{"open_command": "AA 88 01 00 55", "close_command": "AA 88 01 01 55"}]'}
  56. control_mode = yuntaideng_info["control_mode"] # 0:手动模式,1:定时模式
  57. if control_mode == 0:
  58. '''手动模式:前端发送控制指令'''
  59. pass
  60. elif control_mode == 1:
  61. '''定时模式'''
  62. light_status = 0 # 默认水下云台灯处于关闭状态
  63. now_time = datetime.now().strftime("%H:%M:%S")
  64. definite_time = json.loads(yuntaideng_info["definite_time"])
  65. # 依次判断每个时间段
  66. for time_list in definite_time:
  67. each_time = time_list.split(",")
  68. time1 = each_time[0]
  69. time2 = each_time[1]
  70. if time1 < time2:
  71. if time1 <= now_time <= time2:
  72. light_status = 1
  73. else:
  74. if time2 <= now_time <= time1:
  75. light_status = 1
  76. # 判断是否需要修改当前状态
  77. light_name = yuntaideng_info['device_name']
  78. # light_real_status = all_light_real_status[light_name]
  79. #
  80. # if light_real_status is None or int(light_status) != int(light_real_status):
  81. commands = json.loads(yuntaideng_info["commands"])
  82. for each_command in commands:
  83. if int(light_status) == 0:
  84. # 关灯
  85. command = each_command["close_command"]
  86. elif int(light_status) == 1:
  87. # 开灯
  88. command = each_command["open_command"]
  89. # print("--------------",light_name, light_real_status, light_status, command)
  90. try:
  91. Utility.available_connectors[light_name].send_command(command)
  92. except Exception as e:
  93. print("----------------", light_name, e)
  94. self._log.error(e)