auto_switch_device.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 AutoSwitchDevice:
  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 > 10:
  25. run_time = this_time
  26. # 获取设备的当前状态
  27. device_real_status = self.update_device_status()
  28. # print("-------------------------------", device_real_status)
  29. get_auto_switch_device = "SELECT device_name,open_time,close_time,open_command,close_command FROM auto_switch_device WHERE status=1;"
  30. auto_switch_device = self._storage.execute_sql(get_auto_switch_device)
  31. if len(auto_switch_device) > 0:
  32. now_time = datetime.now().strftime("%H:%M:%S")
  33. for each_device in auto_switch_device:
  34. device_name = each_device["device_name"]
  35. open_time = datetime.strptime(str(each_device["open_time"]), '%H:%M:%S').strftime("%H:%M:%S")
  36. close_time = datetime.strptime(str(each_device["close_time"]), '%H:%M:%S').strftime("%H:%M:%S")
  37. device_status = 0 # 默认设备是关闭状态
  38. # 判断当前时间设备应该设置的状态
  39. if open_time > close_time:
  40. if close_time < now_time < open_time:
  41. pass
  42. else:
  43. device_status = 1
  44. else:
  45. if open_time < now_time < close_time:
  46. device_status = 1
  47. else:
  48. pass
  49. device_command = None
  50. # 获取设备应该设置的状态的指令
  51. if device_status == 0:
  52. device_command = each_device["close_command"]
  53. elif device_status == 1:
  54. device_command = each_device["open_command"]
  55. # 与当前实际状态比较
  56. if device_name in device_real_status.keys() and device_real_status[device_name] == device_status:
  57. device_command = None
  58. # 发送命令
  59. if device_command:
  60. device_command = device_command.split(",")
  61. # print("***********************",device_name, device_command)
  62. for each_command in device_command:
  63. # print(device_name, device_command)
  64. # command = bytes.fromhex(device_command).decode()
  65. # command = bytes.fromhex(each_command)
  66. try:
  67. Utility.available_connectors[device_name].send_command({"size": 15, "command": each_command})
  68. except Exception as e:
  69. print(each_command + "write[ERROR]:" + str(e))
  70. else:
  71. print("所有设备无需开启定时模式!")
  72. self.update_device_status()
  73. def auto_update_device_status(self):
  74. """自动更新水下云台灯的状态"""
  75. run_time = 0
  76. while True:
  77. this_time = time.time()
  78. if this_time - run_time > 2:
  79. run_time = this_time
  80. self.update_device_status()
  81. def update_device_status(self):
  82. """获取水下云台水下灯的最新状态"""
  83. get_status_command_sql = "SELECT device_name, status_command, IO FROM auto_switch_device;"
  84. get_status_command = self._storage.execute_sql(get_status_command_sql)
  85. device_real_status = {}
  86. if len(get_status_command) > 0:
  87. for each in get_status_command:
  88. device_name = each["device_name"]
  89. status_command = each["status_command"]
  90. IO = each["IO"]
  91. subscript = 8 + (int(IO) - 1) * 2
  92. # status_command = bytes.fromhex(status_command)
  93. try:
  94. res_status_command = Utility.available_connectors[device_name].send_command({"size": 15, "command": status_command})
  95. if res_status_command:
  96. res_status = res_status_command.hex()[subscript:subscript + 2]
  97. if res_status == "01":
  98. light_status = 1
  99. elif res_status == "00":
  100. light_status = 0
  101. else:
  102. light_status = None
  103. device_real_status[device_name] = light_status
  104. update_sql = f"UPDATE auto_switch_device SET light_status={light_status} WHERE device_name=\'{device_name}\';"
  105. self._storage.execute_update_sql(update_sql)
  106. except Exception as e:
  107. print(f"{device_name} {status_command} write[ERROR]: {e}")
  108. return device_real_status