auto_switch_device.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class AutoSwitchDevice:
  15. def __init__(self):
  16. self._storage = EventStorage()
  17. self._log = OutPutLog()
  18. def run(self):
  19. self._log.info("[AutoSwitchDevice] - AutoSwitchDevice module is running!")
  20. run_time = 0
  21. while True:
  22. this_time = time.time()
  23. if this_time - run_time > 5:
  24. run_time = this_time
  25. self.update_yuntaideng_status()
  26. def update_yuntaideng_status(self):
  27. # 获取设备的当前状态
  28. device_real_status = self.get_real_status() # {'shuixiayuntai01': '1', 'shuixiayuntai02': '1', 'litipomian': '1'}
  29. print("-------------------------------", device_real_status)
  30. get_auto_switch_device = "SELECT device_name,open_time,close_time,open_command,close_command FROM auto_switch_device WHERE status=1;"
  31. auto_switch_device = self._storage.execute_sql(get_auto_switch_device)
  32. if len(auto_switch_device) > 0:
  33. now_time = datetime.now().strftime("%H:%M:%S")
  34. for each_device in auto_switch_device:
  35. device_name = each_device["device_name"]
  36. open_time = datetime.strptime(str(each_device["open_time"]), '%H:%M:%S').strftime("%H:%M:%S")
  37. close_time = datetime.strptime(str(each_device["close_time"]), '%H:%M:%S').strftime("%H:%M:%S")
  38. device_status = 0 # 默认设备是关闭状态
  39. # 判断当前时间设备应该设置的状态
  40. if open_time > close_time:
  41. if close_time < now_time < open_time:
  42. pass
  43. else:
  44. device_status = 1
  45. else:
  46. if open_time < now_time < close_time:
  47. device_status = 1
  48. else:
  49. pass
  50. device_command = None
  51. # 获取设备应该设置的状态的指令
  52. if device_status == 0:
  53. device_command = each_device["close_command"]
  54. elif device_status == 1:
  55. device_command = each_device["open_command"]
  56. # 与当前实际状态比较
  57. if device_name in device_real_status.keys() and str(device_status) == device_real_status[device_name]:
  58. device_command = None
  59. # 发送命令
  60. if device_command:
  61. device_command = device_command.split(",")
  62. # print("***********************",device_name, device_command)
  63. for each_command in device_command:
  64. # print(device_name, device_command)
  65. # command = bytes.fromhex(device_command).decode()
  66. command = bytes.fromhex(each_command)
  67. try:
  68. Utility.available_connectors[device_name].send_command(["update_status",command])
  69. except Exception as e:
  70. print(each_command + "write[ERROR]:" + str(e))
  71. else:
  72. print("所有设备无需开启定时模式!")
  73. def get_real_status(self):
  74. """
  75. 获取水下云台灯的实时状态
  76. return:{'shuixiayuntai01': '1', 'shuixiayuntai02': None, 'shuixiayuntai03': None,...}
  77. """
  78. juyudeng_point_sql = "SELECT serial_number,io_point_name FROM `data_point_tbl` WHERE `io_point_name` LIKE 'shuixiayuntai%_status' OR `io_point_name` LIKE 'litipomian%_status';"
  79. juyudeng_point = self._storage.execute_sql(juyudeng_point_sql)
  80. read_real_list = []
  81. point_name_dict = {}
  82. for each in juyudeng_point:
  83. serial_number = "c"+str(each["serial_number"])
  84. read_real_list.append(serial_number)
  85. juyudeng_name = each["io_point_name"].replace("_status", "")
  86. point_name_dict[serial_number] = juyudeng_name
  87. real_data_dict = self._storage.get_real_data(read_real_list)
  88. light_real_status = {}
  89. for k,v in real_data_dict.items():
  90. light_real_status[point_name_dict[k]] = v
  91. return light_real_status