123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #!/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 AutoSwitchYuntaideng:
- 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 > 60:
- run_time = this_time
- self.update_yuntaideng_status()
- 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';"
- 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
- def update_yuntaideng_status(self):
- """判断水下云台灯的状态是否需要修改"""
- # # 获取水下云台灯的实时状态
- # all_light_real_status = self.get_real_status() # {'shuixiayuntai01': '0', 'shuixiayuntai02': None, 'shuixiayuntai03': None, 'shuixiayuntai04': None}
- # #print(all_light_real_status)
- # 判断水下云台灯的状态是否需要修改
- yuntaideng_info_sql = "SELECT device_name,control_mode,definite_time,commands FROM `shuixiayuntaideng_control`;"
- get_yuntaideng_info = self._storage.execute_sql(yuntaideng_info_sql)
- for yuntaideng_info in get_yuntaideng_info:
- # {'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"}]'}
- control_mode = yuntaideng_info["control_mode"] # 0:手动模式,1:定时模式
- if control_mode == 0:
- '''手动模式:前端发送控制指令'''
- pass
- elif control_mode == 1:
- '''定时模式'''
- light_status = 0 # 默认水下云台灯处于关闭状态
- now_time = datetime.now().strftime("%H:%M:%S")
- definite_time = json.loads(yuntaideng_info["definite_time"])
- # 依次判断每个时间段
- for time_list in definite_time:
- each_time = time_list.split(",")
- time1 = each_time[0]
- time2 = each_time[1]
- if time1 < time2:
- if time1 <= now_time <= time2:
- light_status = 1
- else:
- if time2 <= now_time <= time1:
- light_status = 1
- # 判断是否需要修改当前状态
- light_name = yuntaideng_info['device_name']
- # light_real_status = all_light_real_status[light_name]
- #
- # if light_real_status is None or int(light_status) != int(light_real_status):
- commands = json.loads(yuntaideng_info["commands"])
- for each_command in commands:
- if int(light_status) == 0:
- # 关灯
- command = each_command["close_command"]
- elif int(light_status) == 1:
- # 开灯
- command = each_command["open_command"]
- # print("--------------",light_name, light_real_status, light_status, command)
- try:
- Utility.available_connectors[light_name].send_command(command)
- except Exception as e:
- print("----------------", light_name, e)
- self._log.error(e)
|