WorkJPanelCnc.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. package com.mes.component;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  5. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  6. import com.mes.ui.CacheData;
  7. import com.mes.ui.DataUtil;
  8. import com.mes.ui.MesClient;
  9. import com.mes.util.JdbcUtils;
  10. import com.mes.util.SnSlot;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.awt.event.MouseAdapter;
  16. import java.awt.event.MouseEvent;
  17. import java.util.Timer;
  18. import java.util.TimerTask;
  19. /**
  20. * 总成CNC工位面板(工位、PLC、DB 由 MesClient 工位表传入)
  21. * 正面 OP130A-E = 92 DB7000-7004,OP130F = 93 DB7004;反面 OP135A-D = 93 DB7000-7003
  22. *
  23. * 点位 DB700X:0.0 请求上料 / 0.1 请求下料 / 0.2 运行中 / 0.3 设备状态 / 0.4 MES允许上料
  24. * 0.5 MES允许下料 / 0.6 心跳 / 0.7 上料到设备;1.0、1.1、2、4 为预留,不读不写
  25. *
  26. * 上料:OP130 从本 PLC 扫码槽取码,校验通过写 0.4;OP135 请求上料后直接写 0.4,扫码码由独立扫码握手回填
  27. * 进设备:0.7 上升沿或 0.2 运行中,码从「上料」移到「加工」
  28. * 下料:请求下料时上传结果,接口成功后才写 0.5;无码时直接写 0.5
  29. */
  30. public class WorkJPanelCnc extends JPanel {
  31. public static final Logger log = LoggerFactory.getLogger(WorkJPanelCnc.class);
  32. public static final String PLC_92 = "192.168.90.92";
  33. public static final String PLC_93 = "192.168.90.93";
  34. // 旧的 130 -> 135 共享码流转入口保留但关闭,避免现场误用。
  35. private static final boolean LEGACY_SHARED_FLOW_ENABLED = false;
  36. private static final String[] CN_NO = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
  37. public String oprno; // OP130 / OP135
  38. public String pos; // 子工位 A-F
  39. public boolean front; // true=正面
  40. public String plcIp;
  41. public String plcDb;
  42. public String deviceName;
  43. public SnSlot sourceSlot; // 上料取码来源
  44. public SnSlot targetSlot; // 旧共享流转目标,当前仅保留以隐藏旧实现
  45. public S7PLC s7PLC;
  46. public Integer workWidth = 470;
  47. public Integer workHeight = 300;
  48. public Integer tjFeedingFlag = 0; // 0=未取码 1=已取码校验通过
  49. public Integer tjFlag = 0; // 0=空闲 1=已上料 2=加工中 3=加工结束已上传
  50. public Integer tjStatus = 0; // 下料上传标识 0=未上传 1=已上传
  51. public String lastSn = "";
  52. private boolean feedingReleased = true; // 请求上料复位过,才处理下一次上料
  53. private boolean lastToDevice = false; // 上一轮 0.7,用于取上升沿
  54. private volatile boolean atDevice = false;
  55. private volatile boolean running = false;
  56. private boolean plcHeart = false;
  57. private boolean op135ScanOwnerRegistered = false;
  58. private String completionSubmittedSn = "";
  59. private DirectScanHandshake scanHandshake;
  60. public JButton status_menu;
  61. public JTextField productSnFeeding; // 上料码
  62. public JTextField productSn; // 加工码
  63. public JButton resetFeedingBtn;
  64. public JButton resetBtn;
  65. public JLabel feedinglabel; // 0.0 请求上料
  66. public JLabel feedingmeslabel; // 0.4 MES允许上料
  67. public JLabel runlabel; // 0.2 运行中
  68. public JLabel blankinglabel; // 0.1 请求下料
  69. public JLabel blankingmeslabel; // 0.5 MES允许下料
  70. public JLabel feedingtodevicelabel; // 0.7 上料到设备
  71. public ImageIcon imageGreen;
  72. public ImageIcon imageGray;
  73. public JButton signalBtn;
  74. public PlcSignalMonitor signalMonitor;
  75. public CacheData cacheData;
  76. public String cacheName;
  77. private String lastCacheJson = "";
  78. public Timer oprnoTimer;
  79. public Timer heartTimer;
  80. /**
  81. * @param oprno OP130=正面 / OP135=反面
  82. * @param pos 子工位
  83. * @param plcIp PLC 地址
  84. * @param plcDb DB7000-DB7004
  85. * @param sourceSlot 上料取码来源
  86. * @param targetSlot 下料存码目标,null=不存码
  87. */
  88. public WorkJPanelCnc(String oprno, String pos, String plcIp, String plcDb, SnSlot sourceSlot, SnSlot targetSlot) {
  89. this.setLayout(null);
  90. this.setBackground(Color.WHITE);
  91. this.oprno = oprno;
  92. this.pos = pos;
  93. this.front = "OP130".equals(oprno);
  94. this.plcIp = plcIp;
  95. this.plcDb = plcDb;
  96. this.sourceSlot = sourceSlot;
  97. this.targetSlot = targetSlot;
  98. // 设备名沿用原编号:92 DB7000-7004=一~五号机,93 DB7000-7004=六~十号机
  99. int dbIdx = plcDb.charAt(plcDb.length() - 1) - '0';
  100. deviceName = "东侧" + CN_NO[(PLC_93.equals(plcIp) ? 5 : 0) + dbIdx] + "号机";
  101. s7PLC = new S7PLC(EPlcType.S1500, plcIp);
  102. s7PLC.setReceiveTimeout(1000);
  103. s7PLC.setConnectTimeout(1000);
  104. // OP135 改用新缓存,旧 cache_OP135X 中的共享码不再恢复显示。
  105. cacheName = front ? "cache_" + oprno + pos : "cache_" + oprno + pos + "_independent";
  106. cacheData = JdbcUtils.getCache(cacheName);
  107. if (cacheData == null) {
  108. cacheData = new CacheData();
  109. cacheData.setLastSn("");
  110. cacheData.setSn("");
  111. cacheData.setFeedingSn("");
  112. cacheData.setTjStatus(0);
  113. cacheData.setTjFlag(0);
  114. cacheData.setTjFeedingFlag(0);
  115. cacheData.setError(-1);
  116. cacheData.setMsg("");
  117. JdbcUtils.setCache(cacheName, JSON.toJSONString(cacheData));
  118. }
  119. initUI();
  120. formatElementUI();
  121. startOprnoTimer();
  122. heartData();
  123. }
  124. // 界面 -----------------------------------------------------------------------------------------------------
  125. private void initUI() {
  126. JLabel oprnoLabel = new JLabel(oprno + pos);
  127. oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
  128. oprnoLabel.setForeground(Color.BLACK);
  129. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  130. oprnoLabel.setBounds(0, 0, workWidth / 5, 40);
  131. this.add(oprnoLabel);
  132. JLabel nameLabel = new JLabel(deviceName);
  133. nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
  134. nameLabel.setForeground(Color.BLACK);
  135. nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  136. nameLabel.setBounds(0, 17, workWidth / 5, 40);
  137. this.add(nameLabel);
  138. status_menu = new JButton("等待请求上料");
  139. status_menu.setBounds(workWidth / 5, 0, workWidth * 4 / 5, 40);
  140. status_menu.setForeground(Color.GREEN);
  141. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  142. status_menu.setBackground(Color.BLACK);
  143. this.add(status_menu);
  144. addRowLabel("上料", 50);
  145. productSnFeeding = createSnField(50);
  146. resetFeedingBtn = createResetBtn(50, () -> {
  147. String sn = productSnFeeding.getText().trim();
  148. if (confirm("确定刷新上料工件" + sn + "吗?")) {
  149. resetFeeding();
  150. }
  151. });
  152. addRowLabel("加工", 100);
  153. productSn = createSnField(100);
  154. resetBtn = createResetBtn(100, () -> {
  155. String sn = productSn.getText().trim();
  156. if (confirm("确定刷新加工工件" + sn + "吗?\n刷新后不上传该件结果")) {
  157. resetBlanking("已手动清除加工工件");
  158. }
  159. });
  160. imageGreen = scaledIcon("/bg/green_dot.png");
  161. imageGray = scaledIcon("/bg/grey_dot.png");
  162. feedinglabel = createLight("请求上料", 0, 150);
  163. feedingmeslabel = createLight("MES允许上料", workWidth / 3, 150);
  164. runlabel = createLight("运行中", workWidth * 2 / 3, 150);
  165. blankinglabel = createLight("请求下料", 0, 190);
  166. blankingmeslabel = createLight("MES允许下料", workWidth / 3, 190);
  167. feedingtodevicelabel = createLight("上料到设备", workWidth * 2 / 3, 190);
  168. // 点击 MES 写入的灯可手动切换信号
  169. feedingmeslabel.addMouseListener(toggleSignal("MES允许上料", plcDb + ".0.4"));
  170. blankingmeslabel.addMouseListener(toggleSignal("MES允许下料", plcDb + ".0.5"));
  171. JLabel ipLabel = new JLabel("PLC " + plcIp + " | " + plcDb);
  172. ipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
  173. ipLabel.setForeground(Color.DARK_GRAY);
  174. ipLabel.setBounds(5, 230, workWidth * 2 / 3 + 55, 26);
  175. this.add(ipLabel);
  176. signalMonitor = new PlcSignalMonitor(s7PLC, plcDb, oprno + pos, MesClient.mesClientFrame);
  177. signalBtn = signalMonitor.createSignalButton();
  178. signalBtn.setBounds(workWidth * 2 / 3 + 63, 230, 90, 26);
  179. this.add(signalBtn);
  180. }
  181. private void addRowLabel(String text, int y) {
  182. JLabel label = new JLabel(text);
  183. label.setHorizontalAlignment(SwingConstants.CENTER);
  184. label.setForeground(Color.BLACK);
  185. label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  186. label.setBounds(0, y, workWidth / 5, 40);
  187. this.add(label);
  188. }
  189. private JTextField createSnField(int y) {
  190. JTextField field = new JTextField("");
  191. field.setHorizontalAlignment(SwingConstants.CENTER);
  192. field.setForeground(Color.BLACK);
  193. field.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  194. field.setEditable(false);
  195. field.setBounds(workWidth / 5, y, workWidth * 3 / 5, 40);
  196. field.addMouseListener(new MouseAdapter() {
  197. public void mouseClicked(MouseEvent e) {
  198. String sn = field.getText().trim();
  199. if (!sn.isEmpty()) {
  200. MesClient.createQRCode(sn, oprno + pos);
  201. }
  202. }
  203. });
  204. this.add(field);
  205. return field;
  206. }
  207. private JButton createResetBtn(int y, Runnable action) {
  208. JButton btn = new JButton("刷新");
  209. btn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  210. btn.setBounds(workWidth * 4 / 5 + 10, y, workWidth / 5 - 10, 40);
  211. btn.addActionListener(e -> action.run());
  212. this.add(btn);
  213. return btn;
  214. }
  215. private JLabel createLight(String text, int x, int y) {
  216. JLabel label = new JLabel(text);
  217. label.setHorizontalAlignment(SwingConstants.LEFT);
  218. label.setIcon(imageGray);
  219. label.setForeground(Color.BLACK);
  220. label.setBounds(x, y, workWidth / 3, 30);
  221. this.add(label);
  222. return label;
  223. }
  224. private MouseAdapter toggleSignal(String name, String addr) {
  225. return new MouseAdapter() {
  226. public void mouseClicked(MouseEvent e) {
  227. boolean ret = readBoolean(addr);
  228. if (confirm("确定修改" + name + "(" + addr + ")为" + !ret + "吗?")) {
  229. writeBoolean(addr, !ret);
  230. }
  231. }
  232. };
  233. }
  234. private static ImageIcon scaledIcon(String path) {
  235. ImageIcon icon = new ImageIcon(MesClient.class.getResource(path));
  236. icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  237. return icon;
  238. }
  239. private static boolean confirm(String msg) {
  240. return JOptionPane.showConfirmDialog(null, msg, "操作确认", JOptionPane.YES_NO_OPTION,
  241. JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
  242. }
  243. private ImageIcon light(boolean on) {
  244. return on ? imageGreen : imageGray;
  245. }
  246. public void formatElementUI() {
  247. cacheData = JdbcUtils.getCache(cacheName);
  248. if (cacheData == null) {
  249. return;
  250. }
  251. productSn.setText(nvl(cacheData.getSn()));
  252. productSnFeeding.setText(nvl(cacheData.getFeedingSn()));
  253. tjFlag = nvl(cacheData.getTjFlag(), 0);
  254. tjFeedingFlag = nvl(cacheData.getTjFeedingFlag(), 0);
  255. lastSn = nvl(cacheData.getLastSn());
  256. setMenuStatus(nvl(cacheData.getMsg()), nvl(cacheData.getError(), -1));
  257. this.repaint();
  258. }
  259. public void setMenuStatus(String msg, int error) {
  260. status_menu.setForeground(error == 0 ? Color.GREEN : Color.RED);
  261. status_menu.setText(msg);
  262. cacheData.setMsg(msg);
  263. cacheData.setError(error);
  264. cacheData.setTjFlag(tjFlag);
  265. cacheData.setTjFeedingFlag(tjFeedingFlag);
  266. cacheData.setTjStatus(tjStatus);
  267. cacheData.setLastSn(lastSn);
  268. cacheData.setSn(productSn.getText());
  269. cacheData.setFeedingSn(productSnFeeding.getText());
  270. // 内容没变不重复写库(等待取码时每轮都会刷新状态)
  271. String json = JSON.toJSONString(cacheData);
  272. if (!json.equals(lastCacheJson)) {
  273. JdbcUtils.setCache(cacheName, json);
  274. lastCacheJson = json;
  275. }
  276. }
  277. // 定时任务 -----------------------------------------------------------------------------------------------------
  278. public void heartData() {
  279. if (heartTimer != null) {
  280. heartTimer.cancel();
  281. }
  282. heartTimer = new Timer();
  283. heartTimer.schedule(new TimerTask() {
  284. public void run() {
  285. if (MesClient.isLogin == 1) {
  286. plcHeart = !plcHeart;
  287. writeBoolean(plcDb + ".0.6", plcHeart, false);
  288. }
  289. }
  290. }, 1000, 1000);
  291. }
  292. public void startOprnoTimer() {
  293. if (oprnoTimer != null) {
  294. oprnoTimer.cancel();
  295. }
  296. oprnoTimer = new Timer();
  297. // OP135 的上料请求可能在写入 0.4 后很快撤销,缩短采样周期以捕获短脉冲。
  298. long pollInterval = front ? 3000L : 1000L;
  299. oprnoTimer.schedule(new TimerTask() {
  300. public void run() {
  301. try {
  302. if (MesClient.isLogin == 1) {
  303. cycle();
  304. }
  305. } catch (Exception e) {
  306. log.error("<{}{}>轮询异常", oprno, pos, e);
  307. }
  308. }
  309. }, 100, pollInterval);
  310. }
  311. private void cycle() {
  312. boolean retFeeding = readBoolean(plcDb + ".0.0");
  313. boolean retBlanking = readBoolean(plcDb + ".0.1");
  314. boolean retRunning = readBoolean(plcDb + ".0.2");
  315. boolean retAllowFeeding = readBoolean(plcDb + ".0.4");
  316. boolean retAllowBlanking = readBoolean(plcDb + ".0.5");
  317. boolean retToDevice = readBoolean(plcDb + ".0.7");
  318. boolean toDeviceRise = retToDevice && !lastToDevice;
  319. lastToDevice = retToDevice;
  320. running = retRunning;
  321. atDevice = retToDevice;
  322. feedinglabel.setIcon(light(retFeeding));
  323. feedingmeslabel.setIcon(light(retAllowFeeding));
  324. runlabel.setIcon(light(retRunning));
  325. blankinglabel.setIcon(light(retBlanking));
  326. blankingmeslabel.setIcon(light(retAllowBlanking));
  327. feedingtodevicelabel.setIcon(light(retToDevice));
  328. if (!front) {
  329. cycleOp135(retFeeding, retBlanking, retRunning, retAllowFeeding,
  330. retAllowBlanking, toDeviceRise);
  331. return;
  332. }
  333. cycleOp130(retFeeding, retBlanking, retRunning, retAllowFeeding,
  334. retAllowBlanking, toDeviceRise);
  335. }
  336. private void cycleOp130(boolean retFeeding, boolean retBlanking, boolean retRunning,
  337. boolean retAllowFeeding, boolean retAllowBlanking,
  338. boolean toDeviceRise) {
  339. // 请求上料撤销后复位 MES允许上料
  340. if (!retFeeding && retAllowFeeding) {
  341. writeAllowFeeding(false);
  342. } else if (retFeeding && !retAllowFeeding && tjFeedingFlag == 1 && tjFlag == 0) {
  343. // 码已校验通过但工件还没进设备时 PLC 撤销后又重新请求上料,重新允许上料
  344. writeAllowFeeding(true);
  345. }
  346. // ---------- 上料 ----------
  347. if (!retFeeding) {
  348. feedingReleased = true;
  349. }
  350. if (tjFeedingFlag == 0) {
  351. if (retFeeding && feedingReleased) {
  352. takeSn();
  353. }
  354. } else if (tjFlag == 0 && (toDeviceRise || retRunning)) {
  355. // 工件已进设备:码从上料移到加工
  356. tjFlag = 1;
  357. tjFeedingFlag = 0;
  358. productSn.setText(productSnFeeding.getText());
  359. productSnFeeding.setText("");
  360. feedingReleased = false; // 本次请求上料复位后才处理下一次,防止拿走下一件的码
  361. setMenuStatus("工件已上料到设备", 0);
  362. }
  363. if (tjFlag == 1 && retRunning) {
  364. tjFlag = 2;
  365. writeAllowFeeding(false);
  366. writeAllowBlanking(false);
  367. setMenuStatus("工件加工中", 0);
  368. }
  369. if (LEGACY_SHARED_FLOW_ENABLED && tjFlag == 2 && retBlanking) {
  370. finishWorkLegacy();
  371. return;
  372. }
  373. handleBlankingRequest(retBlanking, retAllowBlanking);
  374. }
  375. private synchronized void cycleOp135(boolean retFeeding, boolean retBlanking,
  376. boolean retRunning, boolean retAllowFeeding,
  377. boolean retAllowBlanking, boolean toDeviceRise) {
  378. // OP135 上料请求不等待扫码,直接允许。
  379. if (retFeeding) {
  380. if (!retAllowFeeding) {
  381. writeAllowFeeding(true);
  382. }
  383. if (!op135ScanOwnerRegistered) {
  384. if (scanHandshake == null) {
  385. log.error("<{}{}>独立扫码未初始化,无法登记扫码归属", oprno, pos);
  386. setMenuStatus("独立扫码未初始化", -1);
  387. } else {
  388. op135ScanOwnerRegistered = scanHandshake.registerScanOwner(this);
  389. if (op135ScanOwnerRegistered) {
  390. setMenuStatus("收到上料请求,等待独立扫码", 0);
  391. } else {
  392. setMenuStatus("已有其他OP135工位等待扫码,本工位暂不绑定", -1);
  393. }
  394. }
  395. }
  396. } else {
  397. if (retAllowFeeding) {
  398. writeAllowFeeding(false);
  399. }
  400. // 0.0 可能先于 64 的扫码请求撤销,归属必须锁存到扫码流程结束。
  401. op135ScanOwnerRegistered = false;
  402. }
  403. // 扫码可能晚于设备进料,收到码后立即移动到加工码,避免丢失归属。
  404. if (tjFeedingFlag == 1 && (toDeviceRise || retRunning || atDevice)) {
  405. tjFlag = Math.max(tjFlag, 1);
  406. tjFeedingFlag = 0;
  407. productSn.setText(productSnFeeding.getText());
  408. productSnFeeding.setText("");
  409. setMenuStatus("工件已上料到设备", 0);
  410. }
  411. if (tjFlag == 0 && retRunning) {
  412. tjFlag = 1;
  413. }
  414. if (tjFlag == 1 && retRunning) {
  415. tjFlag = 2;
  416. writeAllowFeeding(false);
  417. writeAllowBlanking(false);
  418. setMenuStatus("工件加工中", 0);
  419. }
  420. handleBlankingRequest(retBlanking, retAllowBlanking);
  421. }
  422. // 取码并校验:通过后清掉码槽里的码,写 MES允许上料
  423. private void takeSn() {
  424. // 同一个码校验失败后不重复请求,等人工刷新或码槽换成新码
  425. String failedSn = productSnFeeding.getText().trim();
  426. if (!failedSn.isEmpty() && (sourceSlot.isEmpty() || failedSn.equals(sourceSlot.getSn()))) {
  427. return;
  428. }
  429. if (!sourceSlot.tryLock()) {
  430. return; // 其他机台正在取码,下一轮再试
  431. }
  432. try {
  433. String sn = sourceSlot.getSn();
  434. if (sn.isEmpty()) {
  435. setMenuStatus("暂无扫码结果,等待扫码", -1);
  436. return;
  437. }
  438. productSnFeeding.setText(sn);
  439. lastSn = sn;
  440. JSONObject retObj = DataUtil.checkQuality(sn, oprno + pos, MesClient.user_menu.getText());
  441. if (!isOk(retObj)) {
  442. writeAllowFeeding(false);
  443. setMenuStatus(failMsg(retObj), -1);
  444. return;
  445. }
  446. sourceSlot.clearIf(sn, oprno + pos + "取码");
  447. tjFeedingFlag = 1;
  448. writeAllowFeeding(true);
  449. setMenuStatus("工件可以加工", 0);
  450. } finally {
  451. sourceSlot.unlock();
  452. }
  453. }
  454. /**
  455. * 下料必须等待 pcresult 成功后再允许。失败时保留加工码,下一轮自动重试。
  456. */
  457. private void handleBlankingRequest(boolean retBlanking, boolean retAllowBlanking) {
  458. String sn = productSn.getText().trim();
  459. if (!retBlanking) {
  460. if (retAllowBlanking) {
  461. writeAllowBlanking(false);
  462. }
  463. completionSubmittedSn = "";
  464. return;
  465. }
  466. if (sn.isEmpty()) {
  467. completionSubmittedSn = "";
  468. if (!retAllowBlanking) {
  469. writeAllowBlanking(true);
  470. }
  471. return;
  472. }
  473. // 防止上一次遗留的允许信号绕过本次工件完成接口。
  474. if (retAllowBlanking && !sn.equals(completionSubmittedSn)) {
  475. if (!writeAllowBlanking(false)) {
  476. setMenuStatus("清除旧下料允许失败,暂不上传", -1);
  477. return;
  478. }
  479. }
  480. if (!sn.equals(completionSubmittedSn)) {
  481. if (!upload(sn)) {
  482. return;
  483. }
  484. completionSubmittedSn = sn;
  485. }
  486. if (writeAllowBlanking(true)) {
  487. resetBlanking("工件结果已提交,允许下料");
  488. }
  489. }
  490. // 旧 130 -> 135 共享码流转实现,默认关闭但保留以便回退。
  491. private void finishWorkLegacy() {
  492. String sn = productSn.getText().trim();
  493. if (targetSlot == null) {
  494. if (upload(sn)) {
  495. resetBlanking("工件结果已提交,等待下一件");
  496. writeAllowBlanking(true);
  497. }
  498. return;
  499. }
  500. if (!targetSlot.tryLock()) {
  501. return;
  502. }
  503. try {
  504. if (!targetSlot.isEmpty()) {
  505. setMenuStatus("反面待上料码" + targetSlot.getSn() + "未取走,暂不允许下料", -1);
  506. return;
  507. }
  508. if (upload(sn)) {
  509. targetSlot.put(sn, oprno + pos);
  510. resetBlanking("结果已提交,码已存入反面待上料码");
  511. writeAllowBlanking(true);
  512. }
  513. } finally {
  514. targetSlot.unlock();
  515. }
  516. }
  517. // 上传加工结果,失败保留加工码并在下一轮重试。
  518. private boolean upload(String sn) {
  519. tjFlag = 3;
  520. tjStatus = 0;
  521. setMenuStatus("工件加工结束,结果上传中", 0);
  522. log.info("<{}{}>上传加工结果 sn={}", oprno, pos, sn);
  523. JSONObject retObj = DataUtil.sendQuality(sn, oprno + pos, "OK", MesClient.user_menu.getText());
  524. if (isOk(retObj)) {
  525. tjStatus = 1;
  526. return true;
  527. }
  528. setMenuStatus(failMsg(retObj), -1);
  529. return false;
  530. }
  531. public void resetBlanking(String msg) {
  532. tjFlag = 0;
  533. tjStatus = 0;
  534. completionSubmittedSn = "";
  535. productSn.setText("");
  536. setMenuStatus(msg, 0);
  537. }
  538. public void resetFeeding() {
  539. tjFeedingFlag = 0;
  540. op135ScanOwnerRegistered = false;
  541. productSnFeeding.setText("");
  542. writeAllowFeeding(false);
  543. setMenuStatus("等待请求上料", 0);
  544. }
  545. /** 绑定 OP135 独立扫码握手,用于登记扫码归属。 */
  546. public void setScanHandshake(DirectScanHandshake scanHandshake) {
  547. this.scanHandshake = scanHandshake;
  548. }
  549. /**
  550. * 接收独立扫码结果。扫码、MES 校验和 PLC 写码均已在握手线程完成。
  551. */
  552. public synchronized boolean acceptScannedSn(String sn) {
  553. String value = nvl(sn).trim();
  554. if (value.isEmpty()) {
  555. setMenuStatus("独立扫码结果为空,未接收", -1);
  556. return false;
  557. }
  558. if (!productSn.getText().trim().isEmpty() && !productSn.getText().trim().equals(value)) {
  559. setMenuStatus("当前加工码" + productSn.getText().trim() + "尚未下料,不能接收新码", -1);
  560. return false;
  561. }
  562. lastSn = value;
  563. if (running || atDevice || tjFlag >= 1) {
  564. productSn.setText(value);
  565. productSnFeeding.setText("");
  566. tjFeedingFlag = 0;
  567. tjFlag = Math.max(tjFlag, 1);
  568. setMenuStatus("独立扫码码已写入加工:" + value, 0);
  569. } else {
  570. productSnFeeding.setText(value);
  571. tjFeedingFlag = 1;
  572. setMenuStatus("独立扫码码已写入上料:" + value, 0);
  573. }
  574. log.info("<{}{}>接收独立扫码码 {}", oprno, pos, value);
  575. return true;
  576. }
  577. private static boolean isOk(JSONObject retObj) {
  578. return retObj != null && "true".equalsIgnoreCase(String.valueOf(retObj.get("result")));
  579. }
  580. private static String failMsg(JSONObject retObj) {
  581. if (retObj == null || retObj.get("result") == null) {
  582. return "请求失败,请重试";
  583. }
  584. String msg = retObj.getString("message");
  585. return msg == null || msg.isEmpty() ? "校验失败" : msg;
  586. }
  587. private static String nvl(String s) {
  588. return s == null ? "" : s;
  589. }
  590. private static int nvl(Integer v, int def) {
  591. return v == null ? def : v;
  592. }
  593. // 与设备交互 -----------------------------------------------------------------------------------------
  594. public boolean writeAllowFeeding(boolean b) {
  595. return writeBoolean(plcDb + ".0.4", b);
  596. }
  597. public boolean writeAllowBlanking(boolean b) {
  598. return writeBoolean(plcDb + ".0.5", b);
  599. }
  600. public boolean readBoolean(String addr) {
  601. try {
  602. boolean ret = s7PLC.readBoolean(addr);
  603. log.info("<{}{}>读取{}-->成功,结果:{}", oprno, pos, addr, ret);
  604. return ret;
  605. } catch (Exception e) {
  606. log.error("<{}{}>读取{}-->失败:{}", oprno, pos, addr, e.getMessage());
  607. return false;
  608. }
  609. }
  610. public boolean writeBoolean(String addr, boolean value) {
  611. return writeBoolean(addr, value, true);
  612. }
  613. // 心跳不打成功日志
  614. private boolean writeBoolean(String addr, boolean value, boolean logOk) {
  615. try {
  616. s7PLC.writeBoolean(addr, value);
  617. if (logOk) {
  618. log.info("<{}{}>写入{}-->成功,写入内容:{}", oprno, pos, addr, value);
  619. }
  620. return true;
  621. } catch (Exception e) {
  622. log.error("<{}{}>写入{}-->失败,写入内容:{},{}", oprno, pos, addr, value, e.getMessage());
  623. return false;
  624. }
  625. }
  626. }