billet_trace_pusher.py 15 KB

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