WorkJPanelOP20.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. package com.mes.component;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.github.s7connector.api.DaveArea;
  5. import com.github.s7connector.api.S7Connector;
  6. import com.github.s7connector.api.S7Serializer;
  7. import com.github.s7connector.api.factory.S7ConnectorFactory;
  8. import com.github.s7connector.api.factory.S7SerializerFactory;
  9. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  10. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  11. import com.mes.ui.CacheData;
  12. import com.mes.ui.DataUtil;
  13. import com.mes.ui.MesClient;
  14. import com.mes.ui.ParamReq;
  15. import com.mes.util.DateLocalUtils;
  16. import com.mes.util.JdbcUtils;
  17. import com.mes.util.S7Util;
  18. //import com.mes.util.S7Utils;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import javax.swing.*;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Timer;
  27. import java.util.TimerTask;
  28. public class WorkJPanelOP20 extends JPanel {
  29. public static final Logger log = LoggerFactory.getLogger(WorkJPanelOP20.class);
  30. public String oprno = "OP040";
  31. public String pos = "A";
  32. public String lastSn = ""; // 最近的码
  33. public String lineSn = "";
  34. public JLabel feedinglabel;
  35. public JLabel feedingmeslabel;
  36. public JLabel runlabel; //运行反馈,
  37. public JLabel blankinglabel; // 下料反馈
  38. public JLabel blankingmeslabel; // 下料MES反馈
  39. public JLabel feedingtodevicelabel; // 上料到设备
  40. public JButton scanBtn;
  41. public JTextField productSn;
  42. public JTextField productSnFeeding;
  43. public JButton status_menu;
  44. public JButton resetBtn;
  45. public JButton resetFeedingBtn;
  46. public ImageIcon imageGreen;
  47. public ImageIcon imageGray;
  48. public String plcIp1 = "192.168.90.81"; // plc1 西侧
  49. public String plcIp2 = "192.168.90.82"; // plc2 东侧
  50. public String deviceIp = "";
  51. public String deviceIp1 = "80.20.10.42";
  52. public String deviceIp2 = "80.20.10.43";
  53. public String deviceIp3 = "80.21.10.42";
  54. public String deviceIp4 = "80.21.10.43";
  55. // FSW3 机床IP 按现有规律暂定,待现场确认
  56. public String deviceIp5 = "80.20.10.44"; // 西侧 FSW3
  57. public String deviceIp6 = "80.21.10.44"; // 东侧 FSW3
  58. public S7Connector s7Connector;
  59. public S7PLC s7PLC = null;
  60. public S7PLC s7PLCFSW = null;
  61. public Integer workWidth = 470;
  62. public Integer workHeight = 300;
  63. public String plcDb = "";
  64. public String plcIp = ""; // 当前连接的 PLC IP
  65. public JLabel ipLabel; // 界面显示 PLC/DB/机床IP
  66. public String plcDb2 = "DB7003";
  67. public String preSn = ""; // 扫码内容
  68. public Integer tjFeedingFlag = 0; // 上料状态标识
  69. public Integer tjFlag = 0; // 1=可加工 2=设备运行中 3=加工结束
  70. public Integer tjStatus = 0; // 提交标识
  71. public Boolean toDevice = false;
  72. public Boolean plcHeart = false;
  73. public Integer isLocal = 0; // 0=正式 1=本地
  74. public Integer isScan = 1; // 1=西侧 2=东测
  75. public Boolean fswOnline = true; //
  76. public String finallySn = "";
  77. public List<String> hjparams = new ArrayList<>();
  78. public Integer errflag = 0; // 1=报警 0=正常
  79. public Integer errflag2 = 0;
  80. public String fswDeviceName = "";
  81. public JLabel oprnoLabel2;
  82. public JLabel oprnoLabel;
  83. // 缓存上一次的信号值,用于检测变化(key=地址, value=上次值)
  84. private java.util.Map<String, Boolean> lastBooleanValues = new java.util.HashMap<>();
  85. private java.util.Map<String, Boolean> lastWriteBooleanValues = new java.util.HashMap<>();
  86. public JButton signalBtn; // 信号监控按钮
  87. public PlcSignalMonitor signalMonitor; // 信号监控组件
  88. public CacheData cacheData = null;
  89. public String cacheName = "";
  90. public WorkJPanelOP20(String position){
  91. this.setLayout(null);
  92. this.setBackground(Color.WHITE);
  93. this.pos = position;
  94. this.lineSn = MesClient.mes_line_sn;
  95. cacheName = "cache_"+oprno+pos;
  96. cacheData = JdbcUtils.getCache(cacheName);
  97. if(cacheData == null){
  98. cacheData = new CacheData();
  99. cacheData.setLastSn("");
  100. cacheData.setSn("");
  101. cacheData.setFeedingSn("");
  102. cacheData.setTjStatus(0);
  103. cacheData.setTjFlag(0);
  104. cacheData.setError(-1);
  105. cacheData.setMsg("");
  106. JdbcUtils.setCache(cacheName,JSON.toJSONString(cacheData));
  107. }
  108. if(MesClient.mes_line_pos.equals("east")){ // 东侧
  109. plcIp = plcIp2;
  110. s7PLC = new S7PLC(EPlcType.S1500, plcIp2);
  111. isScan = 2;
  112. log.info("IP:"+plcIp2);
  113. }else{
  114. plcIp = plcIp1;
  115. s7PLC = new S7PLC(EPlcType.S1500, plcIp1);
  116. isScan = 1;
  117. log.info("IP:"+plcIp1);
  118. }
  119. s7PLC.setConnectTimeout(800);
  120. s7PLC.setReceiveTimeout(800);
  121. // 子工位 -> DB块 / 机床IP / 设备名(西侧 A/B/E,东侧 C/D/F,E/F 为新增 FSW3)
  122. switch (pos) {
  123. case "A": plcDb = "DB7000"; deviceIp = deviceIp1; fswDeviceName = "西侧反面1号机"; break;
  124. case "B": plcDb = "DB7001"; deviceIp = deviceIp2; fswDeviceName = "西侧反面2号机"; break;
  125. case "E": plcDb = "DB7004"; deviceIp = deviceIp5; fswDeviceName = "西侧反面3号机"; break;
  126. case "C": plcDb = "DB7000"; deviceIp = deviceIp3; fswDeviceName = "东侧反面1号机"; break;
  127. case "D": plcDb = "DB7001"; deviceIp = deviceIp4; fswDeviceName = "东侧反面2号机"; break;
  128. case "F": plcDb = "DB7004"; deviceIp = deviceIp6; fswDeviceName = "东侧反面3号机"; break;
  129. default: log.error("未知子工位:" + pos); break;
  130. }
  131. log.info("deviceIp:"+deviceIp);
  132. s7PLCFSW = new S7PLC(EPlcType.SINUMERIK_828D, deviceIp);
  133. s7PLCFSW.setConnectTimeout(800);
  134. s7PLCFSW.setReceiveTimeout(800);
  135. oprnoLabel = new JLabel(oprno+pos);
  136. oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
  137. oprnoLabel.setForeground(Color.BLACK);
  138. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  139. oprnoLabel.setBounds(0, -10, workWidth/5, 40);
  140. this.add(oprnoLabel);
  141. oprnoLabel2 = new JLabel(fswDeviceName);
  142. oprnoLabel2.setHorizontalAlignment(SwingConstants.CENTER);
  143. oprnoLabel2.setForeground(Color.BLACK);
  144. oprnoLabel2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  145. oprnoLabel2.setBounds(0, 12, workWidth / 5, 40);
  146. this.add(oprnoLabel2);
  147. // 底部显示 PLC/DB/机床IP,机床离线时变红
  148. ipLabel = new JLabel("PLC " + plcIp + " | " + plcDb + " | 机床 " + deviceIp);
  149. ipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
  150. ipLabel.setForeground(Color.DARK_GRAY);
  151. ipLabel.setBounds(5, 230, workWidth*2/3 + 55, 26);
  152. this.add(ipLabel);
  153. status_menu = new JButton("等待请求扫码");
  154. status_menu.setBounds(workWidth/5, 0, workWidth*4/5, 40);
  155. status_menu.setForeground(Color.GREEN);
  156. status_menu.setForeground(Color.GREEN);
  157. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  158. status_menu.setBackground(Color.BLACK);
  159. this.add(status_menu);
  160. JLabel oprnoLabelFeeding = new JLabel("上料");
  161. oprnoLabelFeeding.setHorizontalAlignment(SwingConstants.CENTER);
  162. oprnoLabelFeeding.setForeground(Color.BLACK);
  163. oprnoLabelFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  164. oprnoLabelFeeding.setBounds(0, 50, workWidth/5, 40);
  165. this.add(oprnoLabelFeeding);
  166. productSnFeeding = new JTextField("");
  167. productSnFeeding.setHorizontalAlignment(SwingConstants.CENTER);
  168. productSnFeeding.setForeground(Color.BLACK);
  169. productSnFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  170. productSnFeeding.setEditable(false);
  171. productSnFeeding.setBounds(workWidth/5, 50, workWidth*3/5, 40);
  172. productSnFeeding.addMouseListener(new MouseAdapter() {
  173. public void mouseClicked(MouseEvent e) {
  174. String sn = productSnFeeding.getText().trim();
  175. if(!sn.isEmpty()){
  176. MesClient.createQRCode(productSnFeeding.getText(),oprno+pos);
  177. }
  178. }
  179. });
  180. this.add(productSnFeeding);
  181. JLabel oprnoLabelWork = new JLabel("加工");
  182. oprnoLabelWork.setHorizontalAlignment(SwingConstants.CENTER);
  183. oprnoLabelWork.setForeground(Color.BLACK);
  184. oprnoLabelWork.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  185. oprnoLabelWork.setBounds(0, 100, workWidth/5, 40);
  186. this.add(oprnoLabelWork);
  187. productSn = new JTextField("");
  188. productSn.setHorizontalAlignment(SwingConstants.CENTER);
  189. productSn.setForeground(Color.BLACK);
  190. productSn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  191. productSn.setEditable(false);
  192. productSn.setBounds(workWidth/5, 100, workWidth*3/5, 40);
  193. productSn.addMouseListener(new MouseAdapter() {
  194. public void mouseClicked(MouseEvent e) {
  195. String sn = productSn.getText().trim();
  196. if(!sn.isEmpty()){
  197. MesClient.createQRCode(productSn.getText(),oprno+pos);
  198. }
  199. }
  200. });
  201. this.add(productSn);
  202. resetFeedingBtn = new JButton("刷新");
  203. resetFeedingBtn.addActionListener(new ActionListener() {
  204. public void actionPerformed(ActionEvent e) {
  205. int choice = JOptionPane.showConfirmDialog(null, "确定刷新上料工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  206. if (choice == JOptionPane.YES_OPTION) {
  207. resetFeeding();
  208. }
  209. }
  210. });
  211. resetFeedingBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  212. resetFeedingBtn.setBounds(workWidth*4/5 + 10, 50, workWidth/5 - 10, 40);
  213. this.add(resetFeedingBtn);
  214. resetBtn = new JButton("刷新");
  215. resetBtn.addActionListener(new ActionListener() {
  216. public void actionPerformed(ActionEvent e) {
  217. int choice = JOptionPane.showConfirmDialog(null, "确定刷新加工工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  218. if (choice == JOptionPane.YES_OPTION) {
  219. resetBlanking();
  220. }
  221. }
  222. });
  223. resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  224. resetBtn.setBounds(workWidth*4/5 + 10, 100, workWidth/5 - 10, 40);
  225. this.add(resetBtn);
  226. imageGreen = new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"));
  227. imageGreen.setImage(imageGreen.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  228. imageGray = new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"));
  229. imageGray.setImage(imageGray.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  230. feedinglabel = new JLabel("请求扫码");
  231. feedinglabel.setHorizontalAlignment(SwingConstants.LEFT);
  232. feedinglabel.setIcon(imageGreen);
  233. feedinglabel.setForeground(Color.BLACK);
  234. feedinglabel.setBounds(0, 150, workWidth/3, 30);
  235. this.add(feedinglabel);
  236. feedingmeslabel = new JLabel("MES扫码反馈");
  237. feedingmeslabel.setHorizontalAlignment(SwingConstants.LEFT);
  238. feedingmeslabel.setIcon(imageGreen);
  239. feedingmeslabel.setForeground(Color.BLACK);
  240. feedingmeslabel.setBounds(workWidth/3, 150, workWidth/3, 30);
  241. feedingmeslabel.addMouseListener(new MouseAdapter() {
  242. public void mouseClicked(MouseEvent e) {
  243. boolean ret = readAllowFeeding();
  244. int choice = JOptionPane.showConfirmDialog(null, "确定修改MES扫码反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  245. if (choice == JOptionPane.YES_OPTION) {
  246. writeAllowFeeding(!ret);
  247. }
  248. }
  249. });
  250. this.add(feedingmeslabel);
  251. runlabel = new JLabel("设备运行中");
  252. runlabel.setHorizontalAlignment(SwingConstants.LEFT);
  253. runlabel.setIcon(imageGreen);
  254. runlabel.setForeground(Color.BLACK);
  255. runlabel.setBounds(workWidth*2/3, 150, workWidth/3, 30);
  256. this.add(runlabel);
  257. blankinglabel = new JLabel("下料请求");
  258. blankinglabel.setHorizontalAlignment(SwingConstants.LEFT);
  259. blankinglabel.setIcon(imageGreen);
  260. blankinglabel.setForeground(Color.BLACK);
  261. blankinglabel.setBounds(0, 190, workWidth/3, 30);
  262. this.add(blankinglabel);
  263. blankingmeslabel = new JLabel("MES下料反馈");
  264. blankingmeslabel.setHorizontalAlignment(SwingConstants.LEFT);
  265. blankingmeslabel.setIcon(imageGreen);
  266. blankingmeslabel.setForeground(Color.BLACK);
  267. blankingmeslabel.setBounds(workWidth/3, 190, workWidth/3, 30);
  268. this.add(blankingmeslabel);
  269. blankingmeslabel.addMouseListener(new MouseAdapter() {
  270. public void mouseClicked(MouseEvent e) {
  271. boolean ret = readAllowBlanking();
  272. int choice = JOptionPane.showConfirmDialog(null, "确定修改MES下料反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  273. if (choice == JOptionPane.YES_OPTION) {
  274. writeAllowBlanking(!ret);
  275. }
  276. }
  277. });
  278. // 信号监控按钮
  279. signalMonitor = new PlcSignalMonitor(s7PLC, plcDb, oprno + pos, MesClient.mesClientFrame);
  280. signalBtn = signalMonitor.createSignalButton();
  281. signalBtn.setBounds(workWidth*2/3 + 63, 230, 90, 26);
  282. this.add(signalBtn);
  283. heartData();
  284. startOprnoTimer();
  285. monitorDeviceStatus();
  286. // if(pos.equals("A")||pos.equals("C")){
  287. // monitorDeviceStatus();
  288. // }
  289. resetScanA();
  290. formatElementUI();
  291. }
  292. public void formatElementUI(){
  293. cacheData = JdbcUtils.getCache(cacheName);
  294. log.info("cacheData1:"+cacheData.toString());
  295. if(cacheData == null){
  296. return;
  297. }
  298. String msg = cacheData.getMsg();
  299. int error = cacheData.getError();
  300. String sn = cacheData.getSn();
  301. productSn.setText(sn);
  302. String feedingSn = cacheData.getFeedingSn();
  303. productSnFeeding.setText(feedingSn);
  304. tjFlag = cacheData.getTjFlag();
  305. lastSn = cacheData.getLastSn();
  306. setMenuStatus(msg,error);
  307. this.repaint();
  308. }
  309. public void setMenuStatus(String msg,int error){
  310. if(error == 0){
  311. this.status_menu.setForeground(Color.GREEN);
  312. }else{
  313. this.status_menu.setForeground(Color.RED);
  314. }
  315. this.status_menu.setText(msg);
  316. cacheData.setMsg(msg);
  317. cacheData.setError(error);
  318. cacheData.setTjFlag(tjFlag);
  319. cacheData.setTjStatus(tjStatus);
  320. cacheData.setLastSn(lastSn);
  321. cacheData.setSn(productSn.getText());
  322. cacheData.setFeedingSn(productSnFeeding.getText());
  323. JdbcUtils.setCache(cacheName, JSON.toJSONString(cacheData));
  324. }
  325. // 定时任务 -----------------------------------------------------------------------------------------------------
  326. public Timer cjTimerText2;
  327. public void heartData() {
  328. if(cjTimerText2!=null) {
  329. cjTimerText2.cancel();
  330. }
  331. cjTimerText2 = new Timer();
  332. cjTimerText2.schedule(new TimerTask() {
  333. public void run() {
  334. if(MesClient.isLogin == 1 && isLocal == 0){
  335. plcHeart = !plcHeart;
  336. writeBoolean(plcDb+".0.6",plcHeart);
  337. }
  338. }
  339. }, 1000,1000);
  340. }
  341. public Timer cjTimerText3;
  342. public void monitorDeviceStatus() {
  343. if(cjTimerText3!=null) {
  344. cjTimerText3.cancel();
  345. }
  346. cjTimerText3 = new Timer();
  347. cjTimerText3.schedule(new TimerTask() {
  348. public void run() {
  349. if(MesClient.isLogin == 1 && isLocal == 0){
  350. try{
  351. initS7();
  352. monitorDevice();
  353. boolean ret = S7Util.getFswAlarm(s7Connector);
  354. if(ret){
  355. errflag = 0;
  356. }
  357. if(!ret && errflag == 0 ){
  358. errflag = 1;
  359. DataUtil.deviceAlarm(oprno+pos,productSn.getText(),oprno+pos+"随动夹具报警",MesClient.user_menu.getText());
  360. }
  361. fswOnline = true;
  362. }catch (Exception e){
  363. log.info(e.getMessage());
  364. fswOnline = false;
  365. }
  366. if(fswOnline){
  367. oprnoLabel.setForeground(Color.BLACK);
  368. oprnoLabel2.setForeground(Color.BLACK);
  369. ipLabel.setForeground(Color.DARK_GRAY);
  370. }else{
  371. oprnoLabel.setForeground(Color.RED);
  372. oprnoLabel2.setForeground(Color.RED);
  373. ipLabel.setForeground(Color.RED);
  374. }
  375. try{
  376. int ret = getAlarm();
  377. if(ret == 0){
  378. errflag2 = 0;
  379. }
  380. if(ret != 0 && errflag2 == 0 ){
  381. String content = oprno+pos;
  382. if(ret == 1){
  383. content += "安全门故障";
  384. }else if(ret == 2){
  385. content += "急停故障";
  386. }else if(ret == 3){
  387. content += "光栅故障";
  388. }else if(ret == 4){
  389. content += "夹紧故障";
  390. }
  391. errflag2 = 1;
  392. DataUtil.deviceAlarm(oprno+pos,productSn.getText(),content,MesClient.user_menu.getText());
  393. }
  394. }catch (Exception e){
  395. }
  396. }
  397. }
  398. }, 1000,1000);
  399. }
  400. public Timer oprnoTimer;
  401. public void startOprnoTimer() {
  402. //心跳显示图标
  403. if(oprnoTimer!=null) {
  404. oprnoTimer.cancel();
  405. }
  406. oprnoTimer = new Timer();
  407. oprnoTimer.schedule(new TimerTask() {
  408. public void run() {
  409. try{
  410. if(MesClient.isLogin == 1){
  411. Boolean retFeeding = readBoolean(plcDb+".0.0");
  412. feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
  413. Boolean retFeedingAllow = readBoolean(plcDb+".0.4");
  414. feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
  415. if(!retFeeding && retFeedingAllow){
  416. writeAllowFeeding(false);
  417. }
  418. Boolean retBlanking = readBoolean(plcDb+".0.1");
  419. blankinglabel.setIcon(retBlanking?imageGreen:imageGray);
  420. Boolean retAllowBlanking = readBoolean(plcDb+".0.5");
  421. blankingmeslabel.setIcon(retAllowBlanking?imageGreen:imageGray);
  422. Boolean retRunning = readBoolean(plcDb+".0.2");
  423. runlabel.setIcon(retRunning?imageGreen:imageGray);
  424. if(retBlanking && productSn.getText().isEmpty()){
  425. writeAllowBlanking(true);
  426. resetBlanking();
  427. }
  428. //上料
  429. if(tjFeedingFlag == 0){
  430. if(retFeeding){
  431. barCode();
  432. }
  433. if(retRunning){
  434. resetFeeding();
  435. }
  436. }else if(tjFeedingFlag == 1){
  437. if(retRunning){
  438. tjFlag = 1;
  439. productSn.setText(productSnFeeding.getText());
  440. resetFeeding();
  441. }
  442. }
  443. if(tjFlag == 0 && !retBlanking){
  444. writeBoolean(plcDb+".0.5",false);
  445. }
  446. if(tjFlag == 1){
  447. if(retRunning){
  448. tjFlag = 2;
  449. setMenuStatus("工件加工中", 0);
  450. // 运行中清除下料反馈
  451. writeAllowBlanking(false);
  452. }
  453. }else if(tjFlag == 2){
  454. if(retBlanking){ // 运行结束
  455. tjFlag = 3;
  456. setMenuStatus("工件加工结束,结果上传中", 0);
  457. if(tjStatus == 0){
  458. tjStatus = 1;
  459. JSONObject retObj = DataUtil.sendQuality(productSn.getText(),oprno+pos,"OK",MesClient.user_menu.getText());
  460. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  461. resetBlanking();
  462. }else{
  463. if(retObj.get("result")==null){
  464. setMenuStatus("请求失败,请重试",-1);
  465. }else{
  466. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  467. setMenuStatus(retObj.getString("message"),-1);
  468. }
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }catch (Exception e) {
  476. log.error(e.getMessage());
  477. }
  478. }
  479. }, 100,1500);
  480. }
  481. public void barCode2(){
  482. String sn = "";
  483. if(isScan == 1){
  484. sn = MesClient.scanQrcode2();
  485. }else{
  486. sn = MesClient.scanQrcode3();
  487. }
  488. preSn = sn.trim();
  489. log.info("preSn:"+preSn);
  490. MesClient.setMenuStatus("工件码:"+sn, 0);
  491. // 兼容 CZ060 16位底板码和原19位码
  492. if(sn.length() != 16 && sn.length() != 19){
  493. // MesClient.setMenuStatus("扫码结果为空", -1);
  494. }else{
  495. finallySn = sn; //全局码
  496. productSnFeeding.setText(sn);
  497. // writeScanFinish(true);
  498. }
  499. }
  500. public void barCode(){
  501. //如果为空则不进行上料
  502. if(!productSnFeeding.getText().isEmpty()){
  503. return;
  504. }
  505. String sn = readQrcode();
  506. if(sn.equals("")){
  507. if(productSnFeeding.getText().isEmpty()){
  508. productSnFeeding.setText("");
  509. this.setMenuStatus("扫码结果为空", -1);
  510. }
  511. }else{
  512. productSnFeeding.setText(sn);
  513. lastSn = sn;
  514. // 判断刀具寿命
  515. // Integer hjjs = S7Util.formatS7Int(s7Connector.read(DaveArea.DB, 9051, 2, 26));
  516. // if(hjjs > 320){
  517. // this.setMenuStatus("搅拌头达到使用寿命,请换搅拌头", -1);
  518. // return;
  519. // }
  520. // 查询工件质量
  521. JSONObject retObj = DataUtil.checkQuality(sn,oprno+pos,MesClient.user_menu.getText());
  522. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  523. tjFeedingFlag = 1;//可加工
  524. setMenuStatus("工件可以加工", 0);
  525. writeAllowFeeding(true); //允许下料
  526. // resetFeedingBtn.setEnabled(false);
  527. }else{
  528. writeAllowFeeding(false);
  529. resetFeedingBtn.setEnabled(true);
  530. if(retObj.get("result")==null){
  531. setMenuStatus("请求失败,请重试",-1);
  532. }else{
  533. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  534. setMenuStatus(retObj.getString("message"),-1);
  535. }
  536. }
  537. }
  538. }
  539. }
  540. public void resetBlanking() {
  541. tjFlag = 0;
  542. tjStatus = 0;
  543. productSn.setText("");
  544. setMenuStatus("工件结果已提交,等待下一件",0);
  545. resetBtn.setEnabled(true);
  546. // writeAllowFeeding(false);
  547. // writeAllowBlanking(false);
  548. }
  549. public void resetFeeding() {
  550. productSnFeeding.setText("");
  551. tjFeedingFlag = 0;
  552. setMenuStatus("等待请求扫码",0);
  553. resetFeedingBtn.setEnabled(true);
  554. writeAllowFeeding(false);
  555. }
  556. public void resetScanA() {
  557. tjFeedingFlag = 0;
  558. tjFlag = 0;
  559. tjStatus = 0;
  560. productSn.setText("");
  561. productSnFeeding.setText("");
  562. resetBtn.setEnabled(true);
  563. resetFeedingBtn.setEnabled(true);
  564. if(isLocal == 0){
  565. writeAllowFeeding(false);
  566. writeAllowBlanking(false);
  567. }
  568. }
  569. //原来的扫码为上料
  570. // 与设备交互-----------------------------------------------------------------------------------------
  571. // 请求扫码
  572. public boolean readScanFeeding(){
  573. if(isLocal == 1){
  574. return false;
  575. }else{
  576. return readBoolean(plcDb2+".0.0"); //7002 扫码
  577. }
  578. }
  579. // 扫码完成
  580. public boolean readScanFinish(){
  581. if(isLocal == 1){
  582. return false;
  583. }else{
  584. return readBoolean(plcDb2+".0.1"); //扫码完成
  585. }
  586. }
  587. // 扫码完成反馈
  588. public boolean writeScanFinish(boolean bt){
  589. if(isLocal == 1){
  590. return false;
  591. }else{
  592. return writeBoolean(plcDb2+".0.1",bt);
  593. }
  594. }
  595. // 请求扫码
  596. public boolean readFeeding(){
  597. if(isLocal == 1){
  598. return false;
  599. }else{
  600. return readBoolean(plcDb+".0.0");
  601. }
  602. }
  603. // 读取上料到设备
  604. public boolean readFeedingToDevice(){
  605. if(isLocal == 1){
  606. return true;
  607. }else{
  608. return readBoolean(plcDb+".0.7");
  609. }
  610. }
  611. // 读取下料信号
  612. public boolean readBlanking(){
  613. if(isLocal == 1){
  614. return false;
  615. }else{
  616. return readBoolean(plcDb+".0.1");
  617. }
  618. }
  619. // 读取设备运行信号
  620. public boolean readRunning(){
  621. if(isLocal == 1){
  622. return true;
  623. }else{
  624. return readBoolean(plcDb+".0.2");
  625. }
  626. }
  627. // 读取设备通讯状态
  628. public boolean readDeviceStatus(){
  629. if(isLocal == 1){
  630. return true;
  631. }else{
  632. return readBoolean(plcDb+".0.3");
  633. }
  634. }
  635. // 读取MES允许上料信号
  636. public boolean readAllowFeeding(){
  637. if(isLocal == 1){
  638. return false;
  639. }else{
  640. return readBoolean(plcDb+".0.4");
  641. }
  642. }
  643. // 写入MES允许上料信号
  644. public boolean writeAllowFeeding(Boolean b){
  645. if(isLocal == 1){
  646. return true;
  647. }else{
  648. return writeBoolean(plcDb+".0.4",b);
  649. }
  650. }
  651. // 读取MES允许下料信号
  652. public boolean readAllowBlanking(){
  653. if(isLocal == 1){
  654. return true;
  655. }else{
  656. return readBoolean(plcDb+".0.5");
  657. }
  658. }
  659. // 写入MES允许下料信号
  660. public boolean writeAllowBlanking(Boolean b){
  661. if(isLocal == 1){
  662. return true;
  663. }else{
  664. return writeBoolean(plcDb+".0.5",b);
  665. }
  666. }
  667. // 控制扫码枪读取二维码
  668. public String readQrcode(){
  669. // String sn = "5001040043561010006";
  670. String sn = "";
  671. // String sn = finallySn;
  672. if(isLocal == 1){
  673. return "";
  674. }else{
  675. return MesClient.preSn20;
  676. }
  677. }
  678. public boolean readBoolean(String db){
  679. boolean permit;
  680. try {
  681. permit = s7PLC.readBoolean(db);
  682. // 值变化时才打印日志(排除心跳地址.0.4和.0.6)
  683. Boolean lastValue = lastBooleanValues.get(db);
  684. if (lastValue == null || lastValue != permit) {
  685. if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) {
  686. log.info("<"+this.oprno+this.pos+">读取"+db+"-->变化:{} -> {}", lastValue, permit);
  687. }
  688. lastBooleanValues.put(db, permit);
  689. }
  690. } catch (Exception e) {
  691. log.error("<"+this.oprno+this.pos+">读取"+db+"-->失败", e);
  692. return false;
  693. }
  694. return permit;
  695. }
  696. public boolean writeBoolean(String db, boolean permit){
  697. try {
  698. // 值变化时才写入和打印日志(排除心跳地址.0.4和.0.6)
  699. Boolean lastValue = lastWriteBooleanValues.get(db);
  700. if (lastValue == null || lastValue != permit) {
  701. s7PLC.writeBoolean(db,permit);
  702. if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) {
  703. log.info("<"+this.oprno+this.pos+">写入"+db+"-->变化:{} -> {}", lastValue, permit);
  704. }
  705. lastWriteBooleanValues.put(db, permit);
  706. }
  707. } catch (Exception e) {
  708. log.error("<"+this.oprno+this.pos+">写入"+db+"-->失败,写入内容:{}", permit ? "true" : "false");
  709. log.error(e.getMessage(), e);
  710. return false;
  711. }
  712. return true;
  713. }
  714. public void initS7(){
  715. try {
  716. if(s7Connector == null) {
  717. //PLC地址
  718. String ipAddress = deviceIp;
  719. log.info("deviceIp2:"+deviceIp);
  720. //默认端口
  721. int port = 102;
  722. int rack=0;
  723. int slot=3;
  724. int timeout=5000;
  725. s7Connector=
  726. S7ConnectorFactory
  727. .buildTCPConnector()
  728. .withHost(ipAddress)
  729. .withPort(port) //optional
  730. // .withRack(rack) //optional
  731. // .withSlot(slot) //optional
  732. .withTimeout(timeout) //连接超时时间
  733. .build();
  734. S7Serializer s7Serializer2L = S7SerializerFactory.buildSerializer(s7Connector);
  735. }
  736. } catch (Exception e) { // 如果连接失败保存失败再次尝试连接一次
  737. s7Connector = null;
  738. e.printStackTrace();
  739. }
  740. }
  741. public void monitorDevice(){
  742. log.info("monitorDevice");
  743. if(tjFlag == 2){
  744. try{
  745. String zzzs = String.valueOf(s7PLCFSW.readSpindleRate());
  746. String jgsd = String.valueOf(s7PLCFSW.readFeedRate());
  747. // String zzzs = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 0));
  748. // String jgsd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 4));
  749. String zzzb = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 8));
  750. String ylz = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 12));
  751. Integer hjjs = S7Util.formatS7Int(s7Connector.read(DaveArea.DB, 9051, 2, 26));
  752. String hjcd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 28));
  753. String hjms = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 32));
  754. String record_time = DateLocalUtils.getCurrentTime();
  755. hjparams.add(zzzs+"|"+jgsd+"|"+zzzb+"|"+ylz+"|"+hjjs+"|"+hjcd+"|"+hjms+"|"+record_time);
  756. if (hjparams.size() == 60 && productSn.getText() != null && !productSn.getText().trim().isEmpty() ) {
  757. JdbcUtils.insertParamData(oprno+pos,MesClient.mes_line_sn,productSn.getText(), JSON.toJSONString(hjparams));
  758. hjparams = new ArrayList<>();
  759. }
  760. }catch (Exception e){
  761. e.printStackTrace();
  762. s7Connector = null;
  763. }
  764. log.info("monitorDevice:"+JSON.toJSONString(hjparams));
  765. }else{
  766. if(hjparams.size() > 0){
  767. JdbcUtils.insertParamData(oprno,MesClient.mes_line_sn,lastSn, JSON.toJSONString(hjparams));
  768. hjparams = new ArrayList<>();
  769. }
  770. }
  771. }
  772. public int getAlarm(){
  773. boolean ret0 = readBoolean(plcDb+".6.0");
  774. if(ret0){
  775. return 1;
  776. }
  777. boolean ret1 = readBoolean(plcDb+".6.1");
  778. if(ret1){
  779. return 2;
  780. }
  781. boolean ret2 = readBoolean(plcDb+".6.2");
  782. if(ret2){
  783. return 3;
  784. }
  785. boolean ret4 = readBoolean(plcDb+".6.3");
  786. if(ret4){
  787. return 4;
  788. }
  789. return 0;
  790. }
  791. }