123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #!/usr/bin/env python
- # encoding: utf-8
- """
- @CreateTime: 2021/10/13 10:58
- @Author: lxc
- @LastEditTime:
- @Desctiption: 实现设备定时开关功能,实时更新设备当前状态
- """
- from event_storage import EventStorage
- from log import OutPutLog
- import time
- from datetime import datetime
- from utility import Utility
- class AutoSwitchDevice:
- def __init__(self):
- self._storage = EventStorage()
- self._log = OutPutLog()
- def run(self):
- self._log.info("[AutoSwitchDevice] - AutoSwitchDevice module is running!")
- run_time = 0
- while True:
- this_time = time.time()
- if this_time - run_time > 5:
- run_time = this_time
- self.update_yuntaideng_status()
- def update_yuntaideng_status(self):
- # 获取设备的当前状态
- device_real_status = self.get_real_status() # {'shuixiayuntai01': '1', 'shuixiayuntai02': '1', 'litipomian': '1'}
- print("-------------------------------", device_real_status)
-
- get_auto_switch_device = "SELECT device_name,open_time,close_time,open_command,close_command FROM auto_switch_device WHERE status=1;"
- auto_switch_device = self._storage.execute_sql(get_auto_switch_device)
- if len(auto_switch_device) > 0:
- now_time = datetime.now().strftime("%H:%M:%S")
- for each_device in auto_switch_device:
- device_name = each_device["device_name"]
- open_time = datetime.strptime(str(each_device["open_time"]), '%H:%M:%S').strftime("%H:%M:%S")
- close_time = datetime.strptime(str(each_device["close_time"]), '%H:%M:%S').strftime("%H:%M:%S")
-
- device_status = 0 # 默认设备是关闭状态
- # 判断当前时间设备应该设置的状态
- if open_time > close_time:
- if close_time < now_time < open_time:
- pass
- else:
- device_status = 1
- else:
- if open_time < now_time < close_time:
- device_status = 1
- else:
- pass
-
- device_command = None
- # 获取设备应该设置的状态的指令
- if device_status == 0:
- device_command = each_device["close_command"]
- elif device_status == 1:
- device_command = each_device["open_command"]
-
- # 与当前实际状态比较
- if device_name in device_real_status.keys() and str(device_status) == device_real_status[device_name]:
- device_command = None
-
- # 发送命令
- if device_command:
- device_command = device_command.split(",")
- # print("***********************",device_name, device_command)
- for each_command in device_command:
-
- # print(device_name, device_command)
- # command = bytes.fromhex(device_command).decode()
- command = bytes.fromhex(each_command)
- try:
- Utility.available_connectors[device_name].send_command(["update_status",command])
- except Exception as e:
- print(each_command + "write[ERROR]:" + str(e))
- else:
- print("所有设备无需开启定时模式!")
- def get_real_status(self):
- """
- 获取水下云台灯的实时状态
- return:{'shuixiayuntai01': '1', 'shuixiayuntai02': None, 'shuixiayuntai03': None,...}
- """
- 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';"
- juyudeng_point = self._storage.execute_sql(juyudeng_point_sql)
- read_real_list = []
- point_name_dict = {}
- for each in juyudeng_point:
- serial_number = "c"+str(each["serial_number"])
- read_real_list.append(serial_number)
- juyudeng_name = each["io_point_name"].replace("_status", "")
- point_name_dict[serial_number] = juyudeng_name
- real_data_dict = self._storage.get_real_data(read_real_list)
-
- light_real_status = {}
- for k,v in real_data_dict.items():
- light_real_status[point_name_dict[k]] = v
- return light_real_status
|