auto_feeding_line2.py 13 KB

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