auto_feeding.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. @Date :2021/6/16/14:47
  3. @Author:zz
  4. @Desc :投喂数据统计 喷洒口1
  5. """
  6. import time
  7. import json
  8. import threading
  9. from event_storage import EventStorage
  10. from utility import Utility
  11. from log import OutPutLog
  12. from apis.get_feeding_advise import GetFeedingAdvise
  13. class AutoFeeding:
  14. def __init__(self):
  15. self._storage = EventStorage()
  16. self._log = OutPutLog()
  17. self._run_time = None
  18. self._run_timing = 0
  19. self._feed_diameter = 0
  20. self._error = 0
  21. def t2s(self, t):
  22. h, m = t.strip().split(":")
  23. return int(h) * 3600 + int(m) * 60
  24. # 历史存储主函数
  25. def run(self):
  26. self._log.info('[AutoFeeding] - AutoFeeding module is running!')
  27. # c737工作模式,1:自动模式(指投饵设备)
  28. # 1号供料器频率:c653,2号供料器频率:c661,
  29. # 罗茨风机 --- P01运行状态:c667,P02运行状态:c681
  30. # c740 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
  31. # c742 喷洒口1设备状态: 0:未运行 1:手动投喂中2:自动投喂中3:冲洗或烘干模式
  32. # 线路1被选中:c755, 线路2被选中:c756, 线路3被选中:c757, 线路4被选中:c758
  33. list = ["c737", "c653", "c661", "c740", "c742","c667", "c681", "c743", "c744", "c745", "c746"]
  34. # 获取配置信息
  35. while 1:
  36. time.sleep(1)
  37. self._error = 0
  38. real_data_dict = self._storage.get_real_data(list)
  39. feeding_connectors = Utility.available_connectors["touwei"]
  40. try:
  41. system1_status = int(real_data_dict["c740"])
  42. system2_status = int(real_data_dict["c742"])
  43. system_status = system1_status + system2_status
  44. system_mode = int(real_data_dict["c737"])
  45. p01_status = int(real_data_dict["c667"])
  46. p02_status = int(real_data_dict["c681"])
  47. p_all_status = p01_status + p02_status
  48. # 自动投饵逻辑
  49. feeder_frequency = 0
  50. tank_number = 1
  51. # 获取供料器频率及料罐信息
  52. if float(real_data_dict['c653']) > 1:
  53. feeder_frequency = float(real_data_dict['c653'])
  54. tank_number = 1
  55. sql_get_task_info = "SELECT * FROM touer_set;"
  56. task_info = self._storage.execute_sql(sql_get_task_info)
  57. task_info = task_info[0]
  58. task_mode = int(task_info['feed_mode'])
  59. # bait_name = tank_number_info['name']
  60. # feed_weight = int(tank_number_info['weight'])
  61. # shelf_life = tank_number_info['shelf_life']
  62. tank_number_info = eval(task_info["tank" + str(tank_number)])
  63. feed_diameter = int(tank_number_info['diameter'])
  64. if feeder_frequency > 1:
  65. if self._run_time == None:
  66. self._run_time = time.time()
  67. feeding_speed = 1440 / 67 * feeder_frequency / 50 * (8 - 0.5 * feed_diameter) * 1 / 60
  68. now_time = time.time()
  69. self._run_timing = self._run_timing + (now_time - self._run_time)
  70. self._feed_diameter = self._feed_diameter + feeding_speed * (now_time - self._run_time)
  71. self._run_time = now_time
  72. else:
  73. self._run_time = None
  74. self._run_timing = 0
  75. self._feed_diameter = 0
  76. print("累计运行时间"+str(self._run_timing))
  77. except Exception as e:
  78. self._error = 1
  79. print("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
  80. self._log.error("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
  81. # 通讯保护逻辑
  82. if int(time.time()) % 10 == 0:
  83. if int(time.time() / 10) % 2 == 0:
  84. print("1")
  85. feeding_connectors.send_command([{"addr_data": "DB1.DBX0.7", "data_type": "BOOL", "data": 1}]) # 保护置1
  86. else:
  87. print("2")
  88. feeding_connectors.send_command([{"addr_data": "DB1.DBX0.7", "data_type": "BOOL", "data": 0}]) # 保护置0
  89. if self._error == 0 and task_mode == 1:
  90. # 获取投喂配置参数
  91. sql_get_automatic_feeding_configuration = "SELECT * FROM automatic_feeding_configuration WHERE activation = 1 AND system = 1;"
  92. automatic_feeding_configuration = self._storage.execute_sql(sql_get_automatic_feeding_configuration)
  93. # print(self._error)
  94. # 自动投饵逻辑
  95. if len(automatic_feeding_configuration) > 0:
  96. weekday = int(time.strftime('%w', time.localtime(time.time())))
  97. # feedingAdvise = GetFeedingAdvise()
  98. # feeding_port = feedingAdvise.operation(None)['port']
  99. feeding_port = 1 #设备原因,临时跳过智慧投喂判断
  100. if feeding_port > 0:
  101. for task in automatic_feeding_configuration:
  102. try:
  103. time_begin = self.t2s(task['time_begin'])
  104. time_end = int(task['time_end'])
  105. feeding_line = eval(task['feeding_line'])
  106. task_info = eval(task['phase_frequency'])
  107. feeder_frequency_set_stage1 = int(task_info['stage1']['frequency'])
  108. feeder_frequency_set_stage2 = int(task_info['stage2']['frequency'])
  109. feeder_frequency_set_stage3 = int(task_info['stage3']['frequency'])
  110. time_stage1 = int(task_info['stage1']['time'])
  111. time_stage2 = int(task_info['stage2']['time'])
  112. time_stage3 = int(task_info['stage3']['time'])
  113. feeding_amount_end = float(task['feeding_amount'])
  114. weekday_task = eval(task['date_bait'])
  115. except Exception as e:
  116. self._error = 1
  117. print(e)
  118. self._log.error("get tank_number_info in AutoFeeding [ERROR]:" + str(e))
  119. if self._error == 0:
  120. # 判断当日是否进行投饵
  121. if weekday_task.count(weekday) > 0:
  122. # 判断是否到达开启时间,系统未运行,模式为投喂模式
  123. time_now = time.strftime('%H:%M', time.localtime(time.time()))
  124. time_now = self.t2s(time_now)
  125. # 自动投饵操作
  126. print("准备自动投饵")
  127. # # 调试代码
  128. # if time_begin == time_now:
  129. # # 开始投饵
  130. # print("开始投饵")
  131. #
  132. # if task['mode'] == "timing":
  133. # # 变频逻辑
  134. # if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60:
  135. # print("开始变频1")
  136. # elif self._run_timing > time_stage2*60 and self._run_timing < time_stage3*60:
  137. # print("开始变频2")
  138. # # 停止逻辑
  139. # if time_now == time_end:
  140. # print("停止")
  141. if time_begin > (time_now - 120) and system_status == 0 and system_mode == 1 and p_all_status == 0:
  142. if len(feeding_line_list) > 0 and len(feeding_line_list) < 3:
  143. # 线路重置
  144. self.send_usual_command("DB14.DBX0.0", "BOOL", 0) # 线路1置0
  145. self.send_usual_command("DB14.DBX0.1", "BOOL", 0) # 线路2置0
  146. self.send_usual_command("DB14.DBX0.2", "BOOL", 0) # 线路3置0
  147. self.send_usual_command("DB14.DBX0.3", "BOOL", 0) # 线路4置0
  148. set_status = 1 # 假设线路设置成功
  149. if int(real_data_dict["c755"]) + int(real_data_dict["c756"]) + int(real_data_dict["c757"]) + int(real_data_dict["c758"]) != 0:
  150. set_status = 0
  151. # 线路设置
  152. line_addr_list = ["DB14.DBX0.0", "DB14.DBX0.1", "DB14.DBX0.2", "DB14.DBX0.3"]
  153. for i in feeding_line_list:
  154. line_addr = line_addr_list[i-1]
  155. self.send_usual_command(line_addr, "BOOL", 1) # 线路选择
  156. if int(real_data_dict["c" + str(i+754)]) == 0:
  157. set_status = 0
  158. # if 设置成功:
  159. if set_status == 1:
  160. if time_begin == time_now and system_mode == 1:
  161. if 1 in feeding_line_list and system1_status == 0 and p01_status == 0:
  162. self.send_usual_command("DB1.DBD4", "FLOAT32", 50) # 设置风机频率
  163. self.send_usual_command("DB1.DBD8", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
  164. self.send_auto_start_stop_command("DB1.DBX0.3") # 启动1
  165. if 2 in feeding_line_list and system1_status == 0 and p02_status == 0:
  166. self.send_usual_command("DB1.DBD12", "FLOAT32", 50) # 设置风机频率
  167. self.send_usual_command("DB1.DBD8", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
  168. self.send_auto_start_stop_command("DB1.DBX0.3") # 启动1
  169. if 3 in feeding_line_list and system2_status == 0 and p01_status == 0:
  170. self.send_usual_command("DB1.DBD4", "FLOAT32", 50) # 设置风机频率
  171. self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
  172. self.send_auto_start_stop_command("DB1.DBX0.5") # 启动2
  173. if 4 in feeding_line_list and system2_status == 0 and p02_status == 0:
  174. self.send_usual_command("DB1.DBD12", "FLOAT32", 50) # 设置风机频率
  175. self.send_usual_command("DB1.DBD16", "FLOAT32", feeder_frequency_set_stage1) # 设置供料器频率
  176. self.send_auto_start_stop_command("DB1.DBX0.5") # 启动2
  177. if task['mode'] == "timing" and system_status > 0 and system_mode == 1:
  178. # 变频逻辑
  179. if self._run_timing > time_stage1*60 and self._run_timing< time_stage2*60 and feeder_frequency_set_stage2 != int(feeder_frequency):
  180. self.send_glq_command(system1_status, system2_status, feeder_frequency_set_stage2)
  181. elif self._run_timing > time_stage2*60 and self._run_timing < time_stage3*60 and feeder_frequency_set_stage3 != int(feeder_frequency):
  182. self.send_glq_command(system1_status, system2_status, feeder_frequency_set_stage3)
  183. # 停止逻辑
  184. if self._run_timing > time_end*60:
  185. if system1_status == 1:
  186. print("停止1")
  187. self.send_auto_start_stop_command("DB1.DBX0.4")
  188. if system1_status == 2:
  189. print("停止2")
  190. self.send_auto_start_stop_command("DB1.DBX0.6")
  191. elif task['mode'] == "ration" and system_status > 0 and system_mode == 1:
  192. # 变频逻辑
  193. if self._feed_diameter > time_stage1 and self._feed_diameter < time_stage2 and feeder_frequency_set_stage2 != int(feeder_frequency):
  194. self.send_glq_command(system1_status, system2_status, feeder_frequency_set_stage2)
  195. elif self._feed_diameter > time_stage2 and self._feed_diameter < time_stage3 and feeder_frequency_set_stage3 != int(feeder_frequency):
  196. self.send_glq_command(system1_status, system2_status, feeder_frequency_set_stage3)
  197. # 停止逻辑
  198. if self._feed_diameter > feeding_amount_end:
  199. if system1_status == 1:
  200. print("停止1")
  201. self.send_auto_start_stop_command("DB1.DBX0.4")
  202. if system1_status == 2:
  203. print("停止2")
  204. self.send_auto_start_stop_command("DB1.DBX0.6")
  205. else:
  206. if system1_status == 1:
  207. print("停止1")
  208. self.send_auto_start_stop_command("DB1.DBX0.4")
  209. if system1_status == 2:
  210. print("停止2")
  211. self.send_auto_start_stop_command("DB1.DBX0.6")
  212. self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,自动停止投饵!')
  213. else:
  214. self._log.info('[AutoFeeding]—自然环境恶劣或系统存在异常,无法执行自动投饵!')
  215. def send_auto_start_stop_command(self, addr):
  216. feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 1}])
  217. time.sleep(1)
  218. feeding_connectors.send_command([{"addr_data": addr, "data_type": "BOOL", "data": 0}])
  219. def send_usual_command(self, addr, type, data):
  220. feeding_connectors.send_command([{"addr_data": addr, "data_type": type, "data": data}])
  221. def send_glq_command(self, system1_status, system2_status, feeder_frequency):
  222. if system1_status == 1:
  223. print("开始变频1")
  224. feeding_connectors.send_command([{"addr_data": "DB1.DBD8", "data_type": "FLOAT32", "data": feeder_frequency}]) # 设置频率
  225. if system2_status == 1:
  226. print("开始变频2")
  227. feeding_connectors.send_command([{"addr_data": "DB1.DBD16", "data_type": "FLOAT32", "data": feeder_frequency}]) # 设置频率