|
@@ -0,0 +1,313 @@
|
|
|
+"""
|
|
|
+@Date :2021/6/16/14:47
|
|
|
+@Author:zz
|
|
|
+@Desc :投喂数据统计
|
|
|
+"""
|
|
|
+
|
|
|
+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!')
|
|
|
+ # c445:旋转输料器1号运行频率
|
|
|
+ # c446:旋转输料器2号运行频率
|
|
|
+ # c447:旋转输料器3号运行频率
|
|
|
+ # c448:旋转输料器4号运行频率
|
|
|
+ # c577:工作模式(0:手动模式, 1:自动模式)
|
|
|
+ # c578: 控制模式(0:远程控制,1:本地控制)
|
|
|
+ # c579:系统运行状态(1:自动投喂运行 2:自动投喂停止中3:自动投喂停止)
|
|
|
+ # c580:设备状态(0:未运行 1:手动运行中2:自动运行运行中)
|
|
|
+ # c854:分配器故障
|
|
|
+ # c855:分配器已使能
|
|
|
+ list = ["c445", "c446", "c447", "c448", "c577", "c578", "c579", "c580"]
|
|
|
+
|
|
|
+ # 获取配置信息
|
|
|
+ while 1:
|
|
|
+ time.sleep(1)
|
|
|
+ self._error = 0
|
|
|
+ real_data_dict = self._storage.get_real_data(list)
|
|
|
+ feeding_connectors = Utility.available_connectors["touwei"]
|
|
|
+ try:
|
|
|
+ device_status = int(real_data_dict["c580"])
|
|
|
+ system_status = int(real_data_dict["c579"])
|
|
|
+ system_mode = int(real_data_dict["c577"])
|
|
|
+ system_control_mode = int(real_data_dict["c578"])
|
|
|
+
|
|
|
+ # 自动投饵逻辑
|
|
|
+ feeder_frequency = 0 # 供料器频率
|
|
|
+ tank_number = 1
|
|
|
+ # 获取供料器频率及料罐信息
|
|
|
+ if float(real_data_dict['c445']) > 1:
|
|
|
+ feeder_frequency = float(real_data_dict['c445'])
|
|
|
+ tank_number = 1
|
|
|
+ elif float(real_data_dict['c446']) > 1:
|
|
|
+ feeder_frequency = float(real_data_dict['c446'])
|
|
|
+ tank_number = 2
|
|
|
+ elif float(real_data_dict['c447']) > 1:
|
|
|
+ feeder_frequency = float(real_data_dict['c447'])
|
|
|
+ tank_number = 3
|
|
|
+ elif float(real_data_dict['c448']) > 1:
|
|
|
+ feeder_frequency = float(real_data_dict['c448'])
|
|
|
+ tank_number = 4
|
|
|
+ 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": "DB2.DBX14.1", "data_type": "BOOL", "data": 1}]) # 保护置1
|
|
|
+ else:
|
|
|
+ print("2")
|
|
|
+ feeding_connectors.send_command([{"addr_data": "DB2.DBX14.1", "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;"
|
|
|
+ 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']
|
|
|
+ # if feeding_port > 0:
|
|
|
+ if 1:
|
|
|
+ # print(feeding_port)
|
|
|
+ for task in automatic_feeding_configuration:
|
|
|
+ try:
|
|
|
+ time_begin = self.t2s(task['time_begin'])
|
|
|
+ time_end = int(task['time_end'])
|
|
|
+ feeding_port_select = int(task['feeding_port'])
|
|
|
+ feeding_line = int(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("停止")
|
|
|
+
|
|
|
+ # 获取选定路线对应的风机和供料器的地址
|
|
|
+ get_addr = self.feed_line_addr(feeding_line, feeding_port_select)
|
|
|
+ addr_GLQ = get_addr["addr_GLQ"] # 供料器的地址
|
|
|
+ addr_LCFJ = get_addr["addr_LCFJ"] # 罗茨风机的地址
|
|
|
+ addr_start_line = get_addr["addr_start_line"] # 线路的启动地址
|
|
|
+ addr_stop_line = get_addr["addr_stop_line"] # 线路的停止地址
|
|
|
+ addr_line4_port = get_addr["addr_line4_port"] # 线路4-x选择按钮的地址
|
|
|
+
|
|
|
+ print(time_begin, time_now, device_status, system_status, system_mode, system_control_mode, time_stage1, time_stage2, time_stage3)
|
|
|
+ if time_begin == time_now and system_mode == 1 and system_control_mode == 0:
|
|
|
+ # 开始投饵
|
|
|
+ print("开始投饵")
|
|
|
+ # if feeding_port_select > 0:
|
|
|
+ # feeding_port = feeding_port_select
|
|
|
+ # feeding_connectors.send_command([{"addr_data": "DB14.DBX36.0","data_type": "UINT8","data": feeding_line}]) # 设置投饵线路
|
|
|
+ if feeding_line == 4:
|
|
|
+ feeding_connectors.send_command([{"addr_data": "DB14.DBX36.0", "data_type": "BOOL", "data": 1}]) # 使能开启
|
|
|
+ time.sleep(2)
|
|
|
+ real_data_dict1 = self._storage.get_real_data(["c854", "c855"])
|
|
|
+ fpq_guzhang = int(real_data_dict1["c854"]) # 分配器故障
|
|
|
+ fpq_enable = int(real_data_dict1["c855"]) # 分配器已使能
|
|
|
+ if fpq_guzhang == 0 and fpq_enable == 1:
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_line4_port, "data_type": "BOOL", "data": 1}]) # 设置线路4的投饵口,置1
|
|
|
+ print("设置线路4的投饵口,置1", addr_line4_port)
|
|
|
+ time.sleep(1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_line4_port, "data_type": "BOOL", "data": 0}]) # 设置线路4的投饵口,置0
|
|
|
+ else:
|
|
|
+ continue
|
|
|
+ # else:
|
|
|
+ # continue
|
|
|
+
|
|
|
+ print("开始设置频率")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_GLQ, "data_type": "FLOAT32", "data": feeder_frequency_set_stage1}]) # 设置供料器频率
|
|
|
+ print("设置供料器频率", addr_GLQ, feeder_frequency_set_stage1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_LCFJ, "data_type": "FLOAT32", "data": 50}]) # 设置罗茨风机频率
|
|
|
+ print(" 设置罗茨风机频率", addr_LCFJ)
|
|
|
+
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_start_line, "data_type": "BOOL", "data": 1}]) # 启动置1
|
|
|
+ time.sleep(1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_start_line, "data_type": "BOOL", "data": 0}]) # 启动置0
|
|
|
+
|
|
|
+ if task['mode'] == "timing" and device_status == 2 and system_mode == 1 and system_control_mode == 0:
|
|
|
+ # 定时模式
|
|
|
+ if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60 and feeder_frequency_set_stage2 != int(feeder_frequency):
|
|
|
+ print("定时模式:开始变频1")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_GLQ, "data_type": "FLOAT32", "data": 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):
|
|
|
+ print("定时模式:开始变频2")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_GLQ, "data_type": "FLOAT32", "data": feeder_frequency_set_stage3}]) # 设置供料器频率
|
|
|
+ # 停止逻辑
|
|
|
+ if self._run_timing > time_end*60:
|
|
|
+ print("停止")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data": 1}]) # 停止置1
|
|
|
+ time.sleep(1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data": 0}]) # 停止置0
|
|
|
+ if feeding_line == 4:
|
|
|
+ feeding_connectors.send_command([{"addr_data": "DB14.DBX36.0", "data_type": "BOOL", "data": 0}]) # 使能关闭
|
|
|
+
|
|
|
+
|
|
|
+ elif task['mode'] == "ration" and device_status == 2 and system_mode == 1 and system_control_mode == 0:
|
|
|
+ # print("开始定量模式", self._feed_diameter, time_stage1, time_stage2, feeder_frequency_set_stage2, feeder_frequency)
|
|
|
+ # 定量模式
|
|
|
+ # 变频逻辑
|
|
|
+ if self._feed_diameter > time_stage1 and self._feed_diameter < time_stage2 and feeder_frequency_set_stage2 != int(feeder_frequency):
|
|
|
+ print("定量模式:开始变频1")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_GLQ, "data_type": "FLOAT32", "data": feeder_frequency_set_stage2}]) # 设置供料器频率
|
|
|
+ elif self._feed_diameter > time_stage2 and self._feed_diameter < time_stage3 and feeder_frequency_set_stage3 != int(feeder_frequency):
|
|
|
+ print("定量模式:开始变频2")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_GLQ, "data_type": "FLOAT32", "data": feeder_frequency_set_stage3}]) # 设置供料器频率
|
|
|
+ # 停止逻辑
|
|
|
+ if self._feed_diameter > feeding_amount_end:
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data": 1}]) # 停止置1
|
|
|
+ time.sleep(1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data": 0}]) # 停止置0
|
|
|
+ if feeding_line == 4:
|
|
|
+ feeding_connectors.send_command([{"addr_data": "DB14.DBX36.0", "data_type": "BOOL", "data": 0}]) # 使能关闭
|
|
|
+ else:
|
|
|
+ if system_status == 1:
|
|
|
+ print("停止")
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data":1}])
|
|
|
+ time.sleep(1)
|
|
|
+ feeding_connectors.send_command([{"addr_data": addr_stop_line, "data_type": "BOOL", "data":0}])
|
|
|
+ self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,自动停止投饵!')
|
|
|
+ else:
|
|
|
+ self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,无法执行自动投饵!')
|
|
|
+
|
|
|
+
|
|
|
+ def feed_line_addr(self, feeding_line, feeding_port):
|
|
|
+ """返回不同路线对应的风机和过滤器的地址"""
|
|
|
+ addr_P01 = "DB4.DBD0"
|
|
|
+ addr_P02 = "DB4.DBD4"
|
|
|
+ addr_P03 = "DB4.DBD8"
|
|
|
+ addr_P04 = "DB4.DBD12"
|
|
|
+ addr_GLQ01 = "DB4.DBD16"
|
|
|
+ addr_GLQ02 = "DB4.DBD20"
|
|
|
+ addr_GLQ03 = "DB4.DBD24"
|
|
|
+ addr_GLQ04 = "DB4.DBD28"
|
|
|
+ addr_start_line1 = "DB5.DBX12.2"
|
|
|
+ addr_start_line2 = "DB5.DBX12.3"
|
|
|
+ addr_start_line3 = "DB5.DBX12.4"
|
|
|
+ addr_start_line4 = "DB5.DBX12.5"
|
|
|
+ addr_stop_line1 = "DB5.DBX12.6"
|
|
|
+ addr_stop_line2 = "DB5.DBX12.7"
|
|
|
+ addr_stop_line3 = "DB5.DBX13.0"
|
|
|
+ addr_stop_line4 = "DB5.DBX13.1"
|
|
|
+ addr_line4_1 = "DB13.DBX0.4"
|
|
|
+ addr_line4_2 = "DB13.DBX0.5"
|
|
|
+ addr_line4_3 = "DB13.DBX0.6"
|
|
|
+ addr_line4_4 = "DB13.DBX0.7"
|
|
|
+
|
|
|
+ addr_line4_port = None
|
|
|
+ if feeding_line == 1:
|
|
|
+ addr_LCFJ = addr_P01
|
|
|
+ addr_GLQ = addr_GLQ01
|
|
|
+ addr_start_line = addr_start_line1
|
|
|
+ addr_stop_line = addr_stop_line1
|
|
|
+ elif feeding_line == 2:
|
|
|
+ addr_LCFJ = addr_P02
|
|
|
+ addr_GLQ = addr_GLQ02
|
|
|
+ addr_start_line = addr_start_line2
|
|
|
+ addr_stop_line = addr_stop_line2
|
|
|
+ elif feeding_line == 3:
|
|
|
+ addr_LCFJ = addr_P03
|
|
|
+ addr_GLQ = addr_GLQ03
|
|
|
+ addr_start_line = addr_start_line3
|
|
|
+ addr_stop_line = addr_stop_line3
|
|
|
+ elif feeding_line == 4:
|
|
|
+ addr_LCFJ = addr_P04
|
|
|
+ addr_GLQ = addr_GLQ04
|
|
|
+ addr_start_line = addr_start_line4
|
|
|
+ addr_stop_line = addr_stop_line4
|
|
|
+ if feeding_port == 1:
|
|
|
+ addr_line4_port = addr_line4_1
|
|
|
+ elif feeding_port == 2:
|
|
|
+ addr_line4_port = addr_line4_2
|
|
|
+ elif feeding_port == 3:
|
|
|
+ addr_line4_port = addr_line4_3
|
|
|
+ elif feeding_port == 4:
|
|
|
+ addr_line4_port = addr_line4_4
|
|
|
+
|
|
|
+ res_dict = {
|
|
|
+ "addr_LCFJ": addr_LCFJ,
|
|
|
+ "addr_GLQ": addr_GLQ,
|
|
|
+ "addr_start_line": addr_start_line,
|
|
|
+ "addr_stop_line": addr_stop_line,
|
|
|
+ "addr_line4_port": addr_line4_port
|
|
|
+ }
|
|
|
+ return res_dict
|