123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- """
- @Date :2021/6/16/14:47
- @Author:zz
- @Desc :投喂数据统计 喷洒口1
- """
- import time
- import json
- import threading
- from event_storage import EventStorage
- from utility import Utility
- from log import OutPutLog
- from apis.get_feeding_advise import GetFeedingAdvise
- class AutoFeeding:
- def __init__(self):
- self._storage = EventStorage()
- self._log = OutPutLog()
- self._run_time = None
- self._run_timing = 0
- self._feed_diameter = 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('[AutoFeeding] - AutoFeeding module is running!')
- # c737工作模式,1:自动模式(指投饵设备)
- # 1号供料器频率:c653,2号供料器频率:c661,
- # 罗茨风机 --- P01运行状态:c667,P02运行状态:c681
- # c740 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
- # c742 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
- # 线路1被选中:c755, 线路2被选中:c756, 线路3被选中:c757, 线路4被选中:c758
- 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)
- feeding_connectors = Utility.available_connectors["touwei"]
- try:
- 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
- # 自动投饵逻辑
- feeder_frequency = 0
- tank_number = 1
- # 获取供料器频率及料罐信息
- if float(real_data_dict['c653']) > 1:
- feeder_frequency = float(real_data_dict['c653'])
- tank_number = 1
- 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'])
- # bait_name = tank_number_info['name']
- # feed_weight = int(tank_number_info['weight'])
- # shelf_life = tank_number_info['shelf_life']
- 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
- print("累计运行时间"+str(self._run_timing))
- except Exception as e:
- self._error = 1
- print("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
- self._log.error("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
- # 通讯保护逻辑
- if int(time.time()) % 10 == 0:
- if int(time.time() / 10) % 2 == 0:
- print("1")
- feeding_connectors.send_command([{"addr_data": "DB1.DBX0.7", "data_type": "BOOL", "data": 1}]) # 保护置1
- else:
- print("2")
- feeding_connectors.send_command([{"addr_data": "DB1.DBX0.7", "data_type": "BOOL", "data": 0}]) # 保护置0
- if self._error == 0 and task_mode == 1:
- # 获取投喂配置参数
- sql_get_automatic_feeding_configuration = "SELECT * FROM automatic_feeding_configuration WHERE activation = 1 AND system = 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 = 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(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("准备自动投饵")
- # # 调试代码
- # 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 == 1 and p_all_status == 0:
- if len(feeding_line_list) > 0 and len(feeding_line_list) < 3:
- # 线路重置
- self.send_usual_command("DB14.DBX0.0", "BOOL", 0) # 线路1置0
- self.send_usual_command("DB14.DBX0.1", "BOOL", 0) # 线路2置0
- self.send_usual_command("DB14.DBX0.2", "BOOL", 0) # 线路3置0
- self.send_usual_command("DB14.DBX0.3", "BOOL", 0) # 线路4置0
- set_status = 1 # 假设线路设置成功
- if int(real_data_dict["c755"]) + int(real_data_dict["c756"]) + int(real_data_dict["c757"]) + int(real_data_dict["c758"]) != 0:
- set_status = 0
- # 线路设置
- line_addr_list = ["DB14.DBX0.0", "DB14.DBX0.1", "DB14.DBX0.2", "DB14.DBX0.3"]
- for i in feeding_line_list:
- line_addr = line_addr_list[i-1]
- self.send_usual_command(line_addr, "BOOL", 1) # 线路选择
- if int(real_data_dict["c" + str(i+754)]) == 0:
- set_status = 0
- # if 设置成功:
- if set_status == 1:
- if time_begin == time_now and system_mode == 1:
- if 1 in feeding_line_list and system1_status == 0 and p01_status == 0:
- self.send_usual_command("DB1.DBD4", "FLOAT32", 50) # 设置风机频率
- self.send_usual_command("DB1.DBD8", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
- self.send_auto_start_stop_command("DB1.DBX0.3") # 启动1
- if 2 in feeding_line_list and system1_status == 0 and p02_status == 0:
- self.send_usual_command("DB1.DBD12", "FLOAT32", 50) # 设置风机频率
- self.send_usual_command("DB1.DBD8", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
- self.send_auto_start_stop_command("DB1.DBX0.3") # 启动1
- 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 system_status > 0 and system_mode == 1:
- # 变频逻辑
- if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60 and feeder_frequency_set_stage2 != int(feeder_frequency):
- self.send_glq_command(system1_status, system2_status, 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_glq_command(system1_status, system2_status, feeder_frequency_set_stage3)
-
- # 停止逻辑
- if self._run_timing > time_end*60:
- if system1_status == 1:
- print("停止1")
- self.send_auto_start_stop_command("DB1.DBX0.4")
- if system1_status == 2:
- print("停止2")
- self.send_auto_start_stop_command("DB1.DBX0.6")
- elif task['mode'] == "ration" and system_status > 0 and system_mode == 1:
- # 变频逻辑
- if self._feed_diameter > time_stage1 and self._feed_diameter < time_stage2 and feeder_frequency_set_stage2 != int(feeder_frequency):
- self.send_glq_command(system1_status, system2_status, 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_glq_command(system1_status, system2_status, feeder_frequency_set_stage3)
-
- # 停止逻辑
- if self._feed_diameter > feeding_amount_end:
- if system1_status == 1:
- print("停止1")
- self.send_auto_start_stop_command("DB1.DBX0.4")
- if system1_status == 2:
- print("停止2")
- self.send_auto_start_stop_command("DB1.DBX0.6")
- else:
- if system1_status == 1:
- print("停止1")
- self.send_auto_start_stop_command("DB1.DBX0.4")
- if system1_status == 2:
- print("停止2")
- self.send_auto_start_stop_command("DB1.DBX0.6")
- self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,自动停止投饵!')
- else:
- self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,无法执行自动投饵!')
- def send_auto_start_stop_command(self, addr):
- feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 1}])
- time.sleep(1)
- feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 0}])
- def send_usual_command(self, addr, type, data):
- feeding_connectors.send_command([{"addr_data": addr, "data_type": type, "data": data}])
- def send_glq_command(self, system1_status, system2_status, feeder_frequency):
- if system1_status == 1:
- print("开始变频1")
- feeding_connectors.send_command([{"addr_data": "DB1.DBD8", "data_type": "FLOAT32", "data": feeder_frequency}]) # 设置频率
- if system2_status == 1:
- print("开始变频2")
- feeding_connectors.send_command([{"addr_data": "DB1.DBD16", "data_type": "FLOAT32", "data": feeder_frequency}]) # 设置频率
|