#!/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 import json 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 > 10: run_time = this_time # 获取设备的当前状态 device_real_status = self.update_device_status() # 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 device_real_status[device_name] == device_status: 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({"size": 15, "command": each_command}) except Exception as e: print(each_command + "write[ERROR]:" + str(e)) else: print("所有设备无需开启定时模式!") self.update_device_status() def auto_update_device_status(self): """自动更新水下云台灯的状态""" run_time = 0 while True: this_time = time.time() if this_time - run_time > 2: run_time = this_time self.update_device_status() def update_device_status(self): """获取水下云台水下灯的最新状态""" get_status_command_sql = "SELECT device_name, status_command, IO FROM auto_switch_device;" get_status_command = self._storage.execute_sql(get_status_command_sql) device_real_status = {} if len(get_status_command) > 0: for each in get_status_command: device_name = each["device_name"] status_command = each["status_command"] IO = each["IO"] subscript = 8 + (int(IO) - 1) * 2 # status_command = bytes.fromhex(status_command) try: res_status_command = Utility.available_connectors[device_name].send_command({"size": 15, "command": status_command}) if res_status_command: res_status = res_status_command.hex()[subscript:subscript + 2] if res_status == "01": light_status = 1 elif res_status == "00": light_status = 0 else: light_status = None device_real_status[device_name] = light_status update_sql = f"UPDATE auto_switch_device SET light_status={light_status} WHERE device_name=\'{device_name}\';" self._storage.execute_update_sql(update_sql) except Exception as e: print(f"{device_name} {status_command} write[ERROR]: {e}") return device_real_status