billet_trace_pusher.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import logging
  2. from utils.statepoint import *
  3. from utils.s7data import *
  4. from models.data_sender import *
  5. class Trace_pusher:
  6. def __init__(self, data_s7: S7data, logger: logging.Logger, sender: Sender, strand_position: list, hostsend_flag=False):
  7. self.data_s7 = data_s7
  8. self.logger = logger
  9. self.sender = sender
  10. self.strands_cutting = [[], [], [], [], [], [], [], []]
  11. self.strands_buffer = [[], [], [], [], [], [], [], []]
  12. self.locks = [threading.Lock() for i in range(8)]
  13. self.strand_position = strand_position
  14. self.hostsend_flag = hostsend_flag
  15. self.old_heatNo = "00000000"
  16. self.current_heatNo = "00000000"
  17. self.old_heatData = {}
  18. self.current_heatData = {}
  19. self.total = 0
  20. self.strand = [0, 0, 0, 0, 0, 0, 0, 0]
  21. self.count_lock = threading.Lock()
  22. self.sizing_count_heatNo = "00000000"
  23. self.sizing_count = {}
  24. self.pusher_left_list = []
  25. self.pusher_right_list = []
  26. self.bed_left = [[], [], []]
  27. self.bed_right = [[], [], []]
  28. self.pusher_left = data_s7.make_point('推钢机激光')
  29. self.pusher_right = data_s7.make_point('推钢机激光')
  30. self.pusher_left.set_convertor(lambda data: data < min(self.strand_position))
  31. self.pusher_right.set_convertor(lambda data: data > max(self.strand_position))
  32. self.pusher_left.set_excite_action(lambda: self.arrive_cooling_bed('left'))
  33. self.pusher_right.set_excite_action(lambda: self.arrive_cooling_bed('right'))
  34. self.billet_out = [[], [], [], [], [], [], [], []]
  35. self.length_cutting = []
  36. for i in range(8):
  37. self.length_cutting.append(self.data_s7.make_point(f'L{i+1}定尺'))
  38. self.drawing_speed = []
  39. for i in range(8):
  40. self.drawing_speed.append(self.data_s7.make_point(f'L{i+1}拉速'))
  41. self.billet_position = [
  42. self.data_s7.make_point('L1坯头位置'),
  43. self.data_s7.make_point('L2坯头位置'),
  44. self.data_s7.make_point('L3坯头位置'),
  45. self.data_s7.make_point('L4坯头位置'),
  46. self.data_s7.make_point('L5坯头位置'),
  47. self.data_s7.make_point('L6坯头位置'),
  48. self.data_s7.make_point('L7坯头位置'),
  49. self.data_s7.make_point('L8坯头位置')
  50. ]
  51. self.barrier = [
  52. self.data_s7.make_point('L1挡板'),
  53. self.data_s7.make_point('L2挡板'),
  54. self.data_s7.make_point('L3挡板'),
  55. self.data_s7.make_point('L4挡板'),
  56. self.data_s7.make_point('L5挡板'),
  57. self.data_s7.make_point('L6挡板'),
  58. self.data_s7.make_point('L7挡板'),
  59. self.data_s7.make_point('L8挡板')
  60. ]
  61. self.barrier_checker = [False, False, False, False, False, False, False, False]
  62. if self.hostsend_flag:
  63. self.hostsend_barrier = [
  64. self.data_s7.make_point('热送挡板[0]'),
  65. self.data_s7.make_point('热送挡板[1]'),
  66. self.data_s7.make_point('热送挡板[2]'),
  67. self.data_s7.make_point('热送挡板[3]'),
  68. self.data_s7.make_point('热送挡板[4]'),
  69. self.data_s7.make_point('热送挡板[5]'),
  70. self.data_s7.make_point('热送挡板[6]'),
  71. self.data_s7.make_point('热送挡板[7]'),
  72. ]
  73. for i in range(8):
  74. # 坯子出现
  75. self.billet_position[i].allow_update(False)
  76. self.billet_position[i].set_state(False)
  77. self.billet_position[i].set_convertor(lambda data: data > 12000)
  78. self.billet_position[i].set_excite_action(lambda i=i: self.billet_out_action(i))
  79. self.billet_position[i].set_reset_action(lambda i=i: self.billet_in_buffer_action(i))
  80. self.billet_position[i].allow_update()
  81. # 挡板抬起
  82. self.barrier[i].allow_update(False)
  83. self.barrier[i].set_state(False)
  84. self.barrier[i].set_convertor(lambda data: not bool(data))
  85. self.barrier[i].set_excite_action(lambda i=i: self.barrier_up_action(i))
  86. self.barrier[i].set_reset_action(lambda i=i: self.barrier_down_action(i))
  87. self.barrier[i].allow_update()
  88. # 直轧挡板抬起
  89. if self.hostsend_flag:
  90. self.hostsend_barrier[i].allow_update(False)
  91. self.hostsend_barrier[i].set_state(False)
  92. self.hostsend_barrier[i].set_convertor(lambda data: not bool(data))
  93. self.hostsend_barrier[i].set_excite_action(lambda i=i: self.hostsend_barrier_up_action(i))
  94. self.hostsend_barrier[i].set_reset_action(lambda i=i: self.logger.debug(f"{i+1}流热送挡板关闭"))
  95. self.hostsend_barrier[i].allow_update()
  96. def billet_out_action(self, i):
  97. # [坯号, 炉次信息, 定尺, 拉速, 开切时间, 停切时间]
  98. billetNo = self.current_heatNo + '0' + str(i+1) + '99'
  99. sizing = self.length_cutting[i].data
  100. speed = self.drawing_speed[i].data
  101. self.billet_out[i] = [billetNo, self.current_heatData, sizing, speed, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), '']
  102. def billet_in_buffer_action(self, i):
  103. with self.locks[i]:
  104. if self.strands_cutting[i]:
  105. self.logger.info(f"[TRACE]{i+1}流新增钢坯存储")
  106. self.strands_buffer[i] = self.strands_cutting[i]
  107. self.strands_cutting[i] = []
  108. else:
  109. self.logger.info(f"[TRACE]{i+1}流开切漏钢,算法补入")
  110. self.strands_buffer[i] = self.billet_out[i]
  111. self.billet_out[i] = []
  112. if self.barrier[i].state and self.barrier_checker[i] == False:
  113. self.barrier_up_action(i)
  114. def barrier_up_action(self, i):
  115. with self.locks[i]:
  116. if self.strands_buffer[i]:
  117. self.barrier_checker[i] = True
  118. time.sleep(5)
  119. billetData = self.strands_buffer[i]
  120. self.strands_buffer[i] = []
  121. if self.strand_position[i] <= self.pusher_left.data:
  122. self.logger.info(f"[TRACE]{i+1}流钢坯通过挡板进入推钢区域,在推钢机左侧")
  123. self.pusher_left_list.append(billetData)
  124. else:
  125. self.logger.info(f"[TRACE]{i+1}流钢坯通过挡板进入推钢区域,在推钢机右侧")
  126. self.pusher_right_list.append(billetData)
  127. def barrier_down_action(self, i):
  128. if self.barrier_checker[i]:
  129. self.logger.debug(f"[TRACE]{i+1}流挡板关闭")
  130. else:
  131. self.logger.error(f"[TRACE]{i+1}流挡板关闭,期间无钢坯流出")
  132. self.barrier_checker[i] = False
  133. def hostsend_barrier_up_action(self, i):
  134. with self.count_lock:
  135. gp_tmp = []
  136. if self.strand_position[i] <= self.pusher_left.data:
  137. index = -1
  138. for j in range(len(self.pusher_left_list)-1, -1, -1):
  139. if self.pusher_left_list[j][0][-3] == str(i+1):
  140. index = j
  141. break
  142. if index == -1:
  143. self.logger.warning(f"[TRACE]推钢机左侧未找到{i+1}流的热送钢坯")
  144. else:
  145. gp_tmp = self.pusher_left_list[index]
  146. self.pusher_left_list = self.pusher_left_list[:index] + self.pusher_left_list[index+1:]
  147. else:
  148. index = -1
  149. for j in range(len(self.pusher_right_list)-1, -1, -1):
  150. if self.pusher_right_list[j][0][-3] == str(i+1):
  151. index = j
  152. break
  153. if index == -1:
  154. self.logger.warning(f"[TRACE]推钢机右侧未找到{i+1}流的热送钢坯")
  155. else:
  156. gp_tmp = self.pusher_right_list[index]
  157. self.pusher_right_list = self.pusher_right_list[:index] + self.pusher_right_list[index+1:]
  158. if gp_tmp:
  159. if not (gp_tmp[0].startswith(self.current_heatNo) or gp_tmp[0].startswith(self.old_heatNo)):
  160. self.change_heat(gp_tmp)
  161. self.hostsend(gp_tmp)
  162. def change_heat(self, data):
  163. # 换炉代码
  164. self.old_heatNo = self.current_heatNo
  165. self.old_heatData = self.current_heatData
  166. self.current_heatNo = data[0][:8]
  167. self.current_heatData = data[1]
  168. # 上一炉终止信号在这里发
  169. self.total = 0
  170. self.strand = [0, 0, 0, 0, 0, 0, 0, 0]
  171. def arrive_cooling_bed(self, direc):
  172. with self.count_lock:
  173. if direc == 'left':
  174. self.logger.debug(f"左侧冷床上推入{len(self.pusher_left_list)}根钢坯")
  175. tmp = self.pusher_left_list
  176. self.pusher_left_list = []
  177. elif direc == 'right':
  178. self.logger.debug(f"右侧冷床上推入{len(self.pusher_right_list)}根钢坯")
  179. tmp = self.pusher_right_list
  180. self.pusher_right_list = []
  181. for i in tmp:
  182. if not (i[0].startswith(self.current_heatNo) or i[0].startswith(self.old_heatNo)):
  183. self.change_heat(i)
  184. break
  185. self.billet_to_bed(tmp, direc)
  186. def billet_to_bed(self, billets, direc):
  187. if direc == 'left':
  188. if len(billets):
  189. self.billet_to_bed_impl(billets, self.bed_left)
  190. self.logger.debug(f"左侧冷床目前情况:{len(self.bed_left[0])}根|{len(self.bed_left[1])}根|{len(self.bed_left[2])}根")
  191. elif direc == 'right':
  192. if len(billets):
  193. self.billet_to_bed_impl(billets, self.bed_right)
  194. self.logger.debug(f"右侧冷床目前情况:{len(self.bed_right[2])}根|{len(self.bed_right[1])}根|{len(self.bed_right[0])}根")
  195. def billet_to_bed_impl(self, billets: list, dst: list):
  196. i = 0
  197. count = 0
  198. while i < len(dst) and dst[i]:
  199. count += 1
  200. i += 1
  201. if count == 0:
  202. dst[0].extend(billets)
  203. elif count == 3 and len(billets) >= 4:
  204. self.logger.error(f"组坯异常!")
  205. flag = False
  206. for j in dst:
  207. if len(j) < 4:
  208. flag = True
  209. dst.remove(j)
  210. break
  211. if flag:
  212. dst.append(billets)
  213. else:
  214. dst.remove(dst[0])
  215. dst.append(billets)
  216. count -= 1
  217. elif count == 3 and len(dst[count-1]) + len(billets) > 4:
  218. self.logger.error(f"组坯异常!")
  219. dst[count-1].extend(billets)
  220. count -= 1
  221. elif len(billets) >= 4:
  222. dst[count].extend(billets)
  223. else:
  224. dst[count-1].extend(billets)
  225. count -= 1
  226. if len(dst[count]) >= 4:
  227. self.billet_union(dst[count])
  228. def billet_union(self, billets):
  229. if self.sizing_count_heatNo != self.current_heatNo:
  230. self.sizing_count_heatNo = self.current_heatNo
  231. self.sizing_count = {}
  232. if billets[0][2] not in self.sizing_count:
  233. self.sizing_count[billets[0][2]] = 0
  234. self.sizing_count[billets[0][2]] += 1
  235. billet_unionNo = self.current_heatNo + '{:0>5}'.format(int(billets[0][2])) + '{:0>2}'.format(self.sizing_count[billets[0][2]])
  236. billetsNo = []
  237. ccmNo = self.current_heatData['ccmNo'] if self.current_heatData else '0'
  238. for i in billets:
  239. strandNo = i[0][9]
  240. self.total += 1
  241. self.strand[int(strandNo)-1] += 1
  242. billetNo = self.current_heatNo + ccmNo + strandNo + '{:0>2}'.format(self.strand[int(strandNo)-1])
  243. billetsNo.append(billetNo)
  244. if self.current_heatData:
  245. self.sender.billet_upload(self.current_heatData, billetNo, self.total, i[2], i[3], i[4], i[5], billet_unionNo)
  246. if self.current_heatData:
  247. self.sender.billet_union(self.current_heatData, billet_unionNo, billetsNo, int(billets[0][2]))
  248. self.logger.info(f"{self.current_heatNo}炉组号{billet_unionNo}钢坯{len(billets)}根:\n {'、'.join(billetsNo)}")
  249. def data_from_casting(self, i, data, extend=False):
  250. with self.locks[i]:
  251. if extend:
  252. if self.strands_cutting[i] and self.strands_cutting[i][0] == data[0]:
  253. self.logger.info(f"{i+1}流补充了钢坯停切时间")
  254. self.strands_cutting[i] = data
  255. elif self.strands_buffer[i] and self.strands_buffer[i][0] == data[0]:
  256. self.logger.info(f"{i+1}流补充了钢坯停切时间")
  257. self.strands_buffer[i] = data
  258. else:
  259. self.logger.warning(f"{i+1}流对已经离开的钢坯补充停切时间,无效")
  260. else:
  261. if self.strands_cutting[i]:
  262. self.logger.warning(f"{i+1}流有钢坯开切冲突")
  263. self.strands_cutting[i] = data
  264. self.strands_cutting[i].append('')
  265. else:
  266. self.logger.info(f"{i+1}流钢坯开切")
  267. self.strands_cutting[i] = data
  268. self.strands_cutting[i].append('')
  269. def hostsend(self, i):
  270. ccmNo = self.current_heatData['ccmNo']
  271. strandNo = i[0][9]
  272. self.total += 1
  273. self.strand[int(strandNo)-1] += 1
  274. billetNo = self.current_heatNo + ccmNo + strandNo + '{:0>2}'.format(self.strand[int(strandNo)-1])
  275. self.sender.billet_upload(self.current_heatData, billetNo, self.total, i[2], i[3], i[4], i[5], '')
  276. self.logger.info(f"{self.current_heatNo}炉钢坯热送:{billetNo}")
  277. def clean_status(self):
  278. self.logger.debug(f"[TRACE]小冷床状态清空")
  279. with self.count_lock:
  280. self.pusher_left_list = []
  281. self.pusher_right_list = []
  282. self.bed_left = [[], [], []]
  283. self.bed_right = [[], [], []]
  284. def get_billet(self, direc):
  285. if direc == "left":
  286. return self.get_billet_action(self.bed_left)
  287. if direc == "right":
  288. return self.get_billet_action(self.bed_right)
  289. def get_billet_action(self, src):
  290. for i in range(len(src)-1, -1, -1):
  291. if len(src[i]) >= 4:
  292. tmp = [j[0] for j in src[i]]
  293. src[i] = []
  294. return tmp
  295. return []
  296. # [坯号, 炉次信息, 定尺, 拉速, 开切时间, 停切时间]