123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- """
- @Date :2021/6/16/14:47
- @Author:zz
- @Desc :投喂数据统计 喷洒口1
- """
- import time
- import json
- import threading
- from datetime import datetime
- from event_storage import EventStorage
- from utility import Utility
- from log import OutPutLog
- from apis.get_feeding_advise import GetFeedingAdvise
- class AutoFeedingLine2:
- def __init__(self):
- self._storage = EventStorage()
- self._log = OutPutLog()
- self._run_time = None
- self.feeding_connectors = None
- self._run_timing = 0
- self._feed_diameter = 0
- self._line_set_status = 0
- self._error = 0
- def t2s(self, t):
- h, m = t.strip().split(":")
- return int(h) * 3600 + int(m) * 60
- # 历史存储主函数
- def run(self):
- self._log.info(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - AutoFeeding module is running!")
- # c737 工作模式,1:自动模式(指投饵设备)
- # c653: 1号供料器频率
- # c661: 2号供料器频率
- # c667: 罗茨风机 --- P01运行状态
- # c681: 罗茨风机 --- P02运行状态
- # c740: 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
- # c742: 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
- # c743: 线路1被选中
- # c744: 线路2被选中
- # c745: 线路3被选中
- # c746: 线路4被选中
- list = ["c737", "c653", "c661", "c740", "c742","c667", "c681", "c743", "c744", "c745", "c746"]
- # 获取配置信息
- while 1:
- time.sleep(1)
- self._error = 0
- real_data_dict = self._storage.get_real_data(list)
- self.feeding_connectors = Utility.available_connectors["touwei"]
- try:
- for k, v in real_data_dict.items():
- if v is None:
- real_data_dict[k] = -9999
- system1_status = int(real_data_dict["c740"])
- system2_status = int(real_data_dict["c742"])
- system_status = system1_status + system2_status
- system_mode = int(real_data_dict["c737"])
- p01_status = int(real_data_dict["c667"])
- p02_status = int(real_data_dict["c681"])
- p_all_status = p01_status + p02_status
- line_status = {}
- line_status[1] = int(real_data_dict["c743"])
- line_status[2] = int(real_data_dict["c744"])
- line_status[3] = int(real_data_dict["c745"])
- line_status[4] = int(real_data_dict["c746"])
- # 自动投饵逻辑
- feeder_frequency = 0
- tank_number = 2
- # 获取供料器频率及料罐信息
- if float(real_data_dict['c661']) > 1:
- feeder_frequency = float(real_data_dict['c661'])
- sql_get_task_info = "SELECT * FROM touer_set;"
- task_info = self._storage.execute_sql(sql_get_task_info)
- task_info = task_info[0]
- task_mode = int(task_info['feed_mode'])
- tank_number_info = eval(task_info["tank" + str(tank_number)])
- feed_diameter = int(tank_number_info['diameter'])
- if feeder_frequency > 1:
- if self._run_time == None:
- self._run_time = time.time()
- feeding_speed = 1440 / 67 * feeder_frequency / 50 * (8 - 0.5 * feed_diameter) * 1 / 60
- now_time = time.time()
- self._run_timing = self._run_timing + (now_time - self._run_time)
- self._feed_diameter = self._feed_diameter + feeding_speed * (now_time - self._run_time)
- self._run_time = now_time
- else:
- self._run_time = None
- self._run_timing = 0
- self._feed_diameter = 0
- if self._run_timing > 0:
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - 累计运行时间: {str(self._run_timing)}")
- except Exception as e:
- self._error = 1
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - get tank_number_info in AutoFeeding [ERROR]: {e}")
- self._log.error("[AutoFeedingLine2] - get tank_number_info in AutoFeeding [ERROR]:" + str(e))
- if self._error == 0 and task_mode == 1:
- # 获取投喂配置参数
- sql_get_automatic_feeding_configuration = "SELECT * FROM automatic_feeding_configuration WHERE activation = 1;"
- automatic_feeding_configuration = self._storage.execute_sql(sql_get_automatic_feeding_configuration)
- # print(self._error)
- # 自动投饵逻辑
- if len(automatic_feeding_configuration) > 0:
- weekday = int(time.strftime('%w', time.localtime(time.time())))
- # feedingAdvise = GetFeedingAdvise()
- # feeding_port = feedingAdvise.operation(None)['port']
- feeding_port = 1 #设备原因,临时跳过智慧投喂判断
- if feeding_port > 0:
- for task in automatic_feeding_configuration:
- try:
- time_begin = self.t2s(task['time_begin'])
- time_end = int(task['time_end'])
- feeding_line_list = eval(task['feeding_line'])
- task_info = eval(task['phase_frequency'])
- feeder_frequency_set_stage1 = int(task_info['stage1']['frequency'])
- feeder_frequency_set_stage2 = int(task_info['stage2']['frequency'])
- feeder_frequency_set_stage3 = int(task_info['stage3']['frequency'])
- time_stage1 = int(task_info['stage1']['time'])
- time_stage2 = int(task_info['stage2']['time'])
- time_stage3 = int(task_info['stage3']['time'])
- feeding_amount_end = float(task['feeding_amount'])
- weekday_task = eval(task['date_bait'])
- except Exception as e:
- self._error = 1
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - {e}")
- self._log.error("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
- if self._error == 0:
- # 判断当日是否进行投饵
- if weekday_task.count(weekday) > 0:
- # 判断是否到达开启时间,系统未运行,模式为投喂模式
- time_now = time.strftime('%H:%M', time.localtime(time.time()))
- time_now = self.t2s(time_now)
- # 自动投饵操作
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - 准备自动投饵")
- # # 调试代码
- # if time_begin == time_now:
- # # 开始投饵
- # print("开始投饵")
- #
- # if task['mode'] == "timing":
- # # 变频逻辑
- # if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60:
- # print("开始变频1")
- # elif self._run_timing > time_stage2*60 and self._run_timing < time_stage3*60:
- # print("开始变频2")
- # # 停止逻辑
- # if time_now == time_end:
- # print("停止")
- if time_begin > (time_now - 120) and system_status == 0 and system_mode == 0 and p_all_status == 0:
- self._line_set_status = 1
- for i in line_status:
- if i in feeding_line_list:
- if line_status[i] != 1:
- self._line_set_status = 0
- else:
- if line_status[i] != 0:
- self._line_set_status = 0
- # if 设置成功:
- if self._line_set_status == 1:
- if time_begin == time_now and system_mode == 0:
- if 3 in feeding_line_list and system2_status == 0 and p01_status == 0:
- self.send_usual_command("DB1.DBD4", "FLOAT32", 50) # 设置风机频率
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
- self.send_auto_start_stop_command("DB1.DBX0.5") # 启动2
- if 4 in feeding_line_list and system2_status == 0 and p02_status == 0:
- self.send_usual_command("DB1.DBD12", "FLOAT32", 50) # 设置风机频率
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
- self.send_auto_start_stop_command("DB1.DBX0.5") # 启动2
- if task['mode'] == "timing" and system2_status == 2 and system_mode == 0:
- # 变频逻辑
- if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60 and feeder_frequency_set_stage2 != int(feeder_frequency):
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage2)
- elif self._run_timing > time_stage2*60 and self._run_timing < time_stage3*60 and feeder_frequency_set_stage3 != int(feeder_frequency):
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage3)
-
- # 停止逻辑
- if self._run_timing > time_end*60:
- if system2_status == 2:
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - 停止1")
- self.send_auto_start_stop_command("DB1.DBX0.6")
- elif task['mode'] == "ration" and system2_status == 2 and system_mode == 0:
- # 变频逻辑
- if self._feed_diameter > time_stage1 and self._feed_diameter < time_stage2 and feeder_frequency_set_stage2 != int(feeder_frequency):
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage2)
- elif self._feed_diameter > time_stage2 and self._feed_diameter < time_stage3 and feeder_frequency_set_stage3 != int(feeder_frequency):
- self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage3)
-
- # 停止逻辑
- if self._feed_diameter > feeding_amount_end:
- if system2_status == 2:
- print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [AutoFeedingLine2] - 停止2")
- self.send_auto_start_stop_command("DB1.DBX0.6")
- def send_auto_start_stop_command(self, addr):
- if self.feeding_connectors != None:
- self.feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 1}])
- time.sleep(1)
- self.feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 0}])
- def send_usual_command(self, addr, type, data):
- if self.feeding_connectors != None:
- self.feeding_connectors.send_command([{"addr_data": addr, "data_type": type, "data": data}])
|