WorkJPanelDevice.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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.common.buff.EByteBuffFormat;
  10. import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
  11. import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
  12. import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
  13. import com.mes.ui.DataUtil;
  14. import com.mes.ui.MesClient;
  15. import com.mes.ui.OprnoUtil;
  16. import com.mes.util.DateLocalUtils;
  17. import com.mes.util.JdbcUtils;
  18. import com.mes.util.S7Util;
  19. //import com.mes.util.S7Utils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import javax.swing.*;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Timer;
  28. import java.util.TimerTask;
  29. public class WorkJPanelDevice extends JPanel {
  30. public final Logger log = LoggerFactory.getLogger(WorkJPanelDevice.class);
  31. public String oprno = "OP170";
  32. public String pos = "A";
  33. public String lastSn = ""; // 最近的码
  34. public String lineSn = "XT";
  35. public JLabel feedinglabel;
  36. public JLabel feedingmeslabel;
  37. public JLabel runlabel; //运行反馈,
  38. public JLabel blankinglabel; // 下料反馈
  39. public JLabel blankingmeslabel; // 下料MES反馈
  40. public JButton scanBtn;
  41. public JTextField productSn;
  42. public JTextField productSnFeeding;
  43. public JButton status_menu;
  44. public JButton resetBtn;
  45. public JButton checkBtn;
  46. public JButton historyBtn;
  47. public JButton toolBtn;
  48. public JButton resetFeedingBtn;
  49. public ImageIcon imageGreen;
  50. public ImageIcon imageGray;
  51. public String plcIp = "10.85.10.4";
  52. public String deviceIp = "10.85.10.4";
  53. public S7Connector s7Connector;
  54. public S7PLC s7PLC = null;
  55. public ModbusTcp modbusTcp;
  56. public S7PLC s7PLCFSW = null;
  57. public Integer workWidth = 470;
  58. public Integer workHeight = 300;
  59. public String plcDb = "";
  60. public String preSn = ""; // 扫码内容
  61. public Integer tjFeedingFlag = 0; // 上料状态标识
  62. public Integer tjFlag = 0; // 1=可加工 2=设备运行中 3=加工结束
  63. public Integer tjStatus = 0; // 提交标识
  64. public Boolean plcHeart = false;
  65. public Integer isLocal = 0; // 0=正式 1=本地
  66. public Integer isScan = 1; // 1=西侧 2=东测
  67. public Boolean fswOnline = true; //
  68. public String finallySn = "";
  69. public List<String> hjparams = new ArrayList<>();
  70. public Integer errflag = 0; // 1=报警 0=正常
  71. public Integer errflag2 = 0;
  72. public String fswDeviceName = "";
  73. public JLabel oprnoLabel2;
  74. public JLabel oprnoLabel;
  75. // OP110
  76. // public int[] toolNums = { 1,2,3,4,5};
  77. // public String[] toolTitle = { "高速端铣刀", "合金铣刀", "合金台阶钻", "合金台阶钻", "合金钻头" };
  78. // public String[] toolLife = { "", "", "", "", "" };
  79. // // OP170
  80. // public int[] toolNums = { 1,2,3,4,5};
  81. // public String[] toolTitle = { "合金铣刀", "插孔铣刀", "合金台阶钻", "铝用合金钻头", "切削丝锥" };
  82. // public String[] toolLife = { "", "", "", "", "" };
  83. // OP180
  84. public int[] toolNums = new int[10];
  85. public String[] toolTitle = new String[10];
  86. public String[] toolLife = new String[10];
  87. public int toolNm = 6;
  88. // 缓存上一次的信号值,用于检测变化(key=地址, value=上次值)
  89. private java.util.Map<String, Boolean> lastBooleanValues = new java.util.HashMap<>();
  90. private java.util.Map<String, Boolean> lastWriteBooleanValues = new java.util.HashMap<>();
  91. public WorkJPanelDevice(String position){
  92. this.setLayout(null);
  93. this.setBackground(Color.WHITE);
  94. this.pos = position;
  95. this.oprno = DataUtil.formatOprno(MesClient.mes_gw);
  96. // 对于 OP140 和 OP150,增大窗口尺寸以占满主界面
  97. int yScale = 1;
  98. log.info("deviceIp:"+deviceIp);
  99. fswDeviceName = OprnoUtil.getGwDes(lineSn, oprno);
  100. if(oprno.equals("OP110")){ // 框架反面CNC
  101. plcIp = "10.85.10.6";
  102. if(position.equals("A")){
  103. plcDb = "DB4003";
  104. deviceIp = "10.85.10.80";
  105. }else if(position.equals("B")){
  106. plcDb = "DB4004";
  107. deviceIp = "10.85.10.81";
  108. } else if (position.equals("C")) {
  109. plcDb = "DB4005";
  110. deviceIp = "10.85.10.82";
  111. }
  112. }else if(oprno.equals("OP140")){
  113. plcIp = "10.85.10.2";
  114. if(position.equals("A")){
  115. plcDb = "DB4008";
  116. deviceIp = "";
  117. }
  118. }else if(oprno.equals("OP150")){
  119. plcIp = "10.85.10.2";
  120. if(position.equals("A")){
  121. plcDb = "DB4009";
  122. deviceIp = "10.85.10.160";
  123. }else if(position.equals("B")){
  124. plcDb = "DB4010";
  125. deviceIp = "10.85.10.161";
  126. }
  127. }else if(oprno.equals("OP170")){
  128. plcIp = "10.85.10.3";
  129. if(position.equals("A")){
  130. plcDb = "DB4012";
  131. deviceIp = "10.85.10.83";
  132. }else if(position.equals("B")){
  133. plcDb = "DB4013";
  134. deviceIp = "10.85.10.84";
  135. }else if (position.equals("C")) {
  136. plcDb = "DB4014";
  137. deviceIp = "10.85.10.85";
  138. }else if (position.equals("D")) {
  139. plcDb = "DB4015";
  140. deviceIp = "10.85.10.86";
  141. }
  142. }else if(oprno.equals("OP180")){
  143. plcIp = "10.85.10.4";
  144. if(position.equals("A")){
  145. plcDb = "DB4017";
  146. deviceIp = "10.85.10.86";
  147. }else if(position.equals("B")){
  148. plcDb = "DB4018";
  149. deviceIp = "10.85.10.87";
  150. } else if (position.equals("C")) {
  151. plcDb = "DB4019";
  152. deviceIp = "10.85.10.88";
  153. }
  154. }
  155. s7PLC = new S7PLC(EPlcType.S1500, plcIp);
  156. s7PLC.setConnectTimeout(800);
  157. s7PLC.setReceiveTimeout(800);
  158. if(oprno.equals("OP110")||oprno.equals("OP170")||oprno.equals("OP180")){
  159. modbusTcp = new ModbusTcp(1, deviceIp);
  160. modbusTcp.setConnectTimeout(500);
  161. modbusTcp.setReceiveTimeout(500);
  162. if(oprno.equals("OP170")){
  163. toolNm = 9;
  164. }else if(oprno.equals("OP180")){
  165. toolNm = 5;
  166. }
  167. for(int i=0; i<toolNm; i++){
  168. toolNums[i] = i+1;
  169. toolTitle[i] = "T"+(i+1);
  170. toolLife[i] = "";
  171. }
  172. }
  173. if(oprno.equals("OP150")){
  174. s7PLCFSW = new S7PLC(EPlcType.SINUMERIK_828D, deviceIp);
  175. s7PLCFSW.setReceiveTimeout(500);
  176. s7PLCFSW.setConnectTimeout(500);
  177. }
  178. oprnoLabel = new JLabel(oprno+pos);
  179. oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER);
  180. oprnoLabel.setForeground(Color.BLACK);
  181. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  182. oprnoLabel.setBounds(0, -10*yScale, workWidth/5, 40);
  183. this.add(oprnoLabel);
  184. oprnoLabel2 = new JLabel(fswDeviceName);
  185. oprnoLabel2.setHorizontalAlignment(SwingConstants.CENTER);
  186. oprnoLabel2.setForeground(Color.BLACK);
  187. oprnoLabel2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
  188. oprnoLabel2.setBounds(0, 12*yScale, workWidth / 5, 40);
  189. this.add(oprnoLabel2);
  190. status_menu = new JButton("等待请求扫码");
  191. status_menu.setBounds(workWidth/5, 0, workWidth*4/5, 40);
  192. status_menu.setForeground(Color.GREEN);
  193. status_menu.setForeground(Color.GREEN);
  194. status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  195. status_menu.setBackground(Color.BLACK);
  196. this.add(status_menu);
  197. JLabel oprnoLabelFeeding = new JLabel("工件");
  198. oprnoLabelFeeding.setHorizontalAlignment(SwingConstants.CENTER);
  199. oprnoLabelFeeding.setForeground(Color.BLACK);
  200. oprnoLabelFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  201. oprnoLabelFeeding.setBounds(0, 50*yScale, workWidth/5, 40);
  202. this.add(oprnoLabelFeeding);
  203. productSnFeeding = new JTextField("");
  204. productSnFeeding.setHorizontalAlignment(SwingConstants.CENTER);
  205. productSnFeeding.setForeground(Color.BLACK);
  206. productSnFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  207. productSnFeeding.setEditable(false);
  208. productSnFeeding.setBounds(workWidth/5, 50*yScale, workWidth*3/5, 40);
  209. productSnFeeding.addMouseListener(new MouseAdapter() {
  210. public void mouseClicked(MouseEvent e) {
  211. String sn = productSnFeeding.getText().trim();
  212. if(!sn.isEmpty()){
  213. MesClient.createQRCode(productSnFeeding.getText(),oprno+pos);
  214. }
  215. }
  216. });
  217. this.add(productSnFeeding);
  218. resetFeedingBtn = new JButton("刷新");
  219. resetFeedingBtn.addActionListener(new ActionListener() {
  220. public void actionPerformed(ActionEvent e) {
  221. int choice = JOptionPane.showConfirmDialog(null, "确定刷新上料工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  222. if (choice == JOptionPane.YES_OPTION) {
  223. log.info("人工点击刷新上料工件:"+productSnFeeding.getText().trim());
  224. resetFeeding();
  225. resetScanA();
  226. }
  227. }
  228. });
  229. resetFeedingBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  230. resetFeedingBtn.setBounds(workWidth*4/5 + 10, 50*yScale, workWidth/5 - 10, 40);
  231. this.add(resetFeedingBtn);
  232. JLabel oprnoLabelWork = new JLabel("加工");
  233. oprnoLabelWork.setHorizontalAlignment(SwingConstants.CENTER);
  234. oprnoLabelWork.setForeground(Color.BLACK);
  235. oprnoLabelWork.setFont(new Font("微软雅黑", Font.PLAIN, 20));
  236. oprnoLabelWork.setBounds(0, 100, workWidth/5, 40);
  237. // this.add(oprnoLabelWork);
  238. productSn = new JTextField("");
  239. productSn.setHorizontalAlignment(SwingConstants.CENTER);
  240. productSn.setForeground(Color.BLACK);
  241. productSn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  242. productSn.setEditable(false);
  243. productSn.setBounds(workWidth/5, 100, workWidth*3/5, 40);
  244. productSn.addMouseListener(new MouseAdapter() {
  245. public void mouseClicked(MouseEvent e) {
  246. String sn = productSn.getText().trim();
  247. if(!sn.isEmpty()){
  248. MesClient.createQRCode(productSn.getText(),oprno+pos);
  249. }
  250. }
  251. });
  252. // this.add(productSn);
  253. resetBtn = new JButton("刷新");
  254. resetBtn.addActionListener(new ActionListener() {
  255. public void actionPerformed(ActionEvent e) {
  256. int choice = JOptionPane.showConfirmDialog(null, "确定刷新加工工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  257. if (choice == JOptionPane.YES_OPTION) {
  258. resetBlanking();
  259. }
  260. }
  261. });
  262. resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22));
  263. resetBtn.setBounds(workWidth*4/5 + 10, 100, workWidth/5 - 10, 40);
  264. // this.add(resetBtn);
  265. checkBtn = new JButton("开班点检");
  266. checkBtn.addActionListener(new ActionListener() {
  267. public void actionPerformed(ActionEvent e) {
  268. showCheckDialog();
  269. }
  270. });
  271. checkBtn.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  272. checkBtn.setBounds(0, 220*yScale, workWidth/4, 35);
  273. this.add(checkBtn);
  274. historyBtn = new JButton("工作记录");
  275. historyBtn.addActionListener(new ActionListener() {
  276. public void actionPerformed(ActionEvent e) {
  277. showRecordDialog();
  278. }
  279. });
  280. historyBtn.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  281. historyBtn.setBounds(workWidth*3/4, 220*yScale, workWidth/4, 35);
  282. this.add(historyBtn);
  283. if(oprno.equals("OP110") || oprno.equals("OP170")|| oprno.equals("OP180")){
  284. toolBtn = new JButton("刀具寿命");
  285. toolBtn.addActionListener(new ActionListener() {
  286. public void actionPerformed(ActionEvent e) {
  287. new CncLifeDialog(MesClient.mesClientFrame,toolNums,toolTitle,toolLife);
  288. }
  289. });
  290. toolBtn.setFont(new Font("微软雅黑", Font.PLAIN, 18));
  291. toolBtn.setBounds(workWidth*1/2 - workWidth/8, 220*yScale, workWidth/4, 35);
  292. this.add(toolBtn);
  293. }
  294. imageGreen = new ImageIcon(MesClient.class.getResource("/bg/green_dot.png"));
  295. imageGreen.setImage(imageGreen.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  296. imageGray = new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png"));
  297. imageGray.setImage(imageGray.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
  298. feedinglabel = new JLabel("允许MES读码");
  299. feedinglabel.setHorizontalAlignment(SwingConstants.LEFT);
  300. feedinglabel.setIcon(imageGreen);
  301. feedinglabel.setForeground(Color.BLACK);
  302. feedinglabel.setBounds(0, 120*yScale, workWidth/3, 30);
  303. this.add(feedinglabel);
  304. feedingmeslabel = new JLabel("MES允许上料");
  305. feedingmeslabel.setHorizontalAlignment(SwingConstants.LEFT);
  306. feedingmeslabel.setIcon(imageGreen);
  307. feedingmeslabel.setForeground(Color.BLACK);
  308. feedingmeslabel.setBounds(workWidth/3, 120*yScale, workWidth/3, 30);
  309. feedingmeslabel.addMouseListener(new MouseAdapter() {
  310. public void mouseClicked(MouseEvent e) {
  311. boolean ret = readAllowFeeding();
  312. int choice = JOptionPane.showConfirmDialog(null, "确定修改MES扫码反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  313. if (choice == JOptionPane.YES_OPTION) {
  314. writeAllowFeeding(!ret);
  315. }
  316. }
  317. });
  318. this.add(feedingmeslabel);
  319. runlabel = new JLabel("工位开始工作");
  320. runlabel.setHorizontalAlignment(SwingConstants.LEFT);
  321. runlabel.setIcon(imageGreen);
  322. runlabel.setForeground(Color.BLACK);
  323. runlabel.setBounds(workWidth*2/3, 120*yScale, workWidth/3, 30);
  324. this.add(runlabel);
  325. blankinglabel = new JLabel("下料请求");
  326. blankinglabel.setHorizontalAlignment(SwingConstants.LEFT);
  327. blankinglabel.setIcon(imageGreen);
  328. blankinglabel.setForeground(Color.BLACK);
  329. blankinglabel.setBounds(0, 160*yScale, workWidth/3, 30);
  330. this.add(blankinglabel);
  331. blankingmeslabel = new JLabel("MES下料反馈");
  332. blankingmeslabel.setHorizontalAlignment(SwingConstants.LEFT);
  333. blankingmeslabel.setIcon(imageGreen);
  334. blankingmeslabel.setForeground(Color.BLACK);
  335. blankingmeslabel.setBounds(workWidth/3, 160*yScale, workWidth/3, 30);
  336. this.add(blankingmeslabel);
  337. blankingmeslabel.addMouseListener(new MouseAdapter() {
  338. public void mouseClicked(MouseEvent e) {
  339. boolean ret = readAllowBlanking();
  340. int choice = JOptionPane.showConfirmDialog(null, "确定修改MES下料反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  341. if (choice == JOptionPane.YES_OPTION) {
  342. writeAllowBlanking(!ret);
  343. }
  344. }
  345. });
  346. heartData();
  347. startOprnoTimer();
  348. if(oprno.equals("OP150")){
  349. monitorDevice();
  350. }
  351. resetScanA();
  352. }
  353. private void showCheckDialog() {
  354. String url = "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProcessCheckRecord/ulist?ucode=" + MesClient.user_menu.getText() + "&oprno=" + oprno + pos + "&lineSn=" + MesClient.mes_line_sn;
  355. new WebViewDialog(MesClient.mesClientFrame,"开班点检 - " + oprno + pos,url);
  356. }
  357. private void showRecordDialog() {
  358. String url = "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProductRecord/work?oprno=" + oprno + pos+ "&lineSn=" + MesClient.mes_line_sn;
  359. new WebViewDialog(MesClient.mesClientFrame,"工作记录 - " + oprno + pos,url);
  360. }
  361. public void setMenuStatus(String msg,int error){
  362. if(error == 0){
  363. this.status_menu.setForeground(Color.GREEN);
  364. }else{
  365. this.status_menu.setForeground(Color.RED);
  366. }
  367. this.status_menu.setText(msg);
  368. }
  369. // 定时任务 -----------------------------------------------------------------------------------------------------
  370. public Timer cjTimerText2;
  371. public void heartData() {
  372. if(cjTimerText2!=null) {
  373. cjTimerText2.cancel();
  374. }
  375. cjTimerText2 = new Timer();
  376. cjTimerText2.schedule(new TimerTask() {
  377. public void run() {
  378. if(MesClient.isLogin == 1 && isLocal == 0){
  379. if(oprno.equals("OP110") && pos.equals("A")){
  380. try{ // 心跳
  381. sendHeart("DB3000",s7PLC);
  382. }catch (Exception e){
  383. e.printStackTrace();
  384. }
  385. // try{ // CNC上线
  386. // ReplyPlc("DB3000",s7PLC);
  387. // }catch (Exception e){
  388. // e.printStackTrace();
  389. // }
  390. //
  391. // try{ // 涂胶上线
  392. // ReplyPlc("DB3010",s7PLC);
  393. // }catch (Exception e){
  394. // e.printStackTrace();
  395. // }
  396. }
  397. }
  398. }
  399. }, 1000,2000);
  400. }
  401. //MES->PLC心跳
  402. public void sendHeart(String db,S7PLC s7PLC){
  403. try{
  404. Boolean ret = s7PLC.readBoolean(db+".60.0");
  405. s7PLC.writeBoolean(db+".60.0",!ret);
  406. }catch (Exception e){
  407. e.printStackTrace();
  408. }
  409. }
  410. private void ReplyPlc(String db,S7PLC s7PLC){
  411. Boolean ret = s7PLC.readBoolean(db+".12.0");
  412. if(ret){ // 允许MES读工件码
  413. if(!db.equals("DB3010")){
  414. String sn = s7PLC.readString(db+".24.0",33).trim();
  415. if(!sn.isEmpty()){
  416. // TODO::判断是否可上件
  417. s7PLC.writeBoolean(db+".0.0",true);
  418. }
  419. }else{
  420. s7PLC.writeBoolean(db+".0.0",true);
  421. }
  422. }else{ // 允许上料置位
  423. s7PLC.writeBoolean(db+".0.0",false);
  424. }
  425. }
  426. public Timer cjTimerText3;
  427. public void monitorDeviceStatus() {
  428. if(cjTimerText3!=null) {
  429. cjTimerText3.cancel();
  430. }
  431. cjTimerText3 = new Timer();
  432. cjTimerText3.schedule(new TimerTask() {
  433. public void run() {
  434. if(MesClient.isLogin == 1 && isLocal == 0){
  435. try{
  436. initS7();
  437. monitorDevice();
  438. boolean ret = S7Util.getFswAlarm(s7Connector);
  439. if(ret){
  440. errflag = 0;
  441. }
  442. if(!ret && errflag == 0 ){
  443. errflag = 1;
  444. DataUtil.deviceAlarm(oprno+pos,productSn.getText(),oprno+pos+"随动夹具报警",MesClient.user_menu.getText());
  445. }
  446. fswOnline = true;
  447. }catch (Exception e){
  448. log.info(e.getMessage());
  449. fswOnline = false;
  450. }
  451. if(fswOnline){
  452. oprnoLabel.setForeground(Color.BLACK);
  453. oprnoLabel2.setForeground(Color.BLACK);
  454. }else{
  455. oprnoLabel.setForeground(Color.RED);
  456. oprnoLabel2.setForeground(Color.RED);
  457. }
  458. try{
  459. // int ret = getAlarm();
  460. // if(ret == 0){
  461. // errflag2 = 0;
  462. // }
  463. // if(ret != 0 && errflag2 == 0 ){
  464. // String content = oprno+pos;
  465. // if(ret == 1){
  466. // content += "安全门故障";
  467. // }else if(ret == 2){
  468. // content += "急停故障";
  469. // }else if(ret == 3){
  470. // content += "光栅故障";
  471. // }else if(ret == 4){
  472. // content += "夹紧故障";
  473. // }
  474. //
  475. // errflag2 = 1;
  476. // DataUtil.deviceAlarm(oprno+pos,productSn.getText(),content,MesClient.user_menu.getText());
  477. // }
  478. }catch (Exception e){
  479. }
  480. }
  481. }
  482. }, 1000,1000);
  483. }
  484. public Timer oprnoTimer;
  485. public void startOprnoTimer() {
  486. //心跳显示图标
  487. if(oprnoTimer!=null) {
  488. oprnoTimer.cancel();
  489. }
  490. oprnoTimer = new Timer();
  491. oprnoTimer.schedule(new TimerTask() {
  492. public void run() {
  493. try{
  494. if(MesClient.isLogin == 1){
  495. //请求扫码
  496. Boolean retFeeding = readFeeding();
  497. feedinglabel.setIcon(retFeeding?imageGreen:imageGray);
  498. // 读取MES允许上料信号
  499. Boolean retFeedingAllow = readAllowFeeding();
  500. feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray);
  501. if(!retFeeding && retFeedingAllow){
  502. writeAllowFeeding(false);
  503. }
  504. // 读取下料信号
  505. Boolean retBlanking = readBlanking();
  506. blankinglabel.setIcon(retBlanking?imageGreen:imageGray);
  507. // 读取MES允许下料信号
  508. Boolean retAllowBlanking = readAllowBlanking();
  509. blankingmeslabel.setIcon(retAllowBlanking?imageGreen:imageGray);
  510. // 当检测到允许MES读码信号为 true,且当前MES下料反馈还为 true 时,将其复位为 false
  511. if (retFeeding && retAllowBlanking) {
  512. writeAllowBlanking(false);
  513. // 同步更新UI图标反馈为灰色
  514. blankingmeslabel.setIcon(imageGray);
  515. }
  516. // 读取设备运行信号
  517. Boolean retRunning = readRunning();
  518. runlabel.setIcon(retRunning?imageGreen:imageGray);
  519. //上料
  520. if(tjFeedingFlag == 0){
  521. if(retRunning){
  522. resetFeeding();
  523. }
  524. if(retBlanking && !retAllowBlanking){
  525. writeAllowBlanking(true);
  526. resetBlanking();
  527. }
  528. if(retFeeding){
  529. barCode();
  530. }
  531. }else if(tjFeedingFlag == 1){
  532. if(retRunning || retBlanking){ // 运行和下料双判断
  533. tjFeedingFlag = 2;
  534. resetFeeding();
  535. setMenuStatus("工件加工中", 0);
  536. getToolLife();
  537. }else{
  538. if(!retFeedingAllow){
  539. writeAllowFeeding(true);
  540. }
  541. }
  542. }else if(tjFeedingFlag == 2){
  543. if(retBlanking || retFeeding){ // 运行结束(下料或者再次上料)
  544. tjFeedingFlag = 0;
  545. setMenuStatus("工件加工结束,结果上传中", 0);
  546. writeAllowBlanking(true);
  547. if(tjStatus == 0){
  548. tjStatus = 1;
  549. // String result = readResult();
  550. String result = "OK";
  551. result = result.equals("OK")?"OK":"NG";
  552. // if(oprno.equals("OP110")||oprno.equals("OP170")||oprno.equals("OP180")){
  553. // result = "OK";
  554. // }
  555. JSONObject retObj = DataUtil.sendQuality(productSnFeeding.getText(),oprno+pos,result,MesClient.user_menu.getText());
  556. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  557. resetBlanking();
  558. }else{
  559. if(retObj.get("result")==null){
  560. setMenuStatus("请求失败,请重试",-1);
  561. }else{
  562. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  563. setMenuStatus(retObj.getString("message"),-1);
  564. }
  565. }
  566. }
  567. }else{
  568. }
  569. }
  570. }
  571. }
  572. }catch (Exception e) {
  573. log.error(e.getMessage());
  574. }
  575. }
  576. }, 100,1500);
  577. }
  578. public void barCode(){
  579. //如果为空则不进行上料
  580. if(!productSnFeeding.getText().isEmpty()){
  581. return;
  582. }
  583. String sn = readQrcode();
  584. if(sn.equals("")){
  585. if(productSnFeeding.getText().isEmpty()){
  586. productSnFeeding.setText("");
  587. this.setMenuStatus("扫码结果为空", -1);
  588. }
  589. }else{
  590. productSnFeeding.setText(sn);
  591. lastSn = sn;
  592. // 查询工件质量
  593. JSONObject retObj = DataUtil.checkQuality(sn,oprno+pos,MesClient.user_menu.getText());
  594. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  595. tjFeedingFlag = 1;//可加工
  596. setMenuStatus("工件可以加工", 0);
  597. writeAllowFeeding(true); //允许上料
  598. }else{
  599. writeAllowFeeding(false);
  600. resetFeedingBtn.setEnabled(true);
  601. if(retObj.get("result")==null){
  602. setMenuStatus("请求失败,请重试",-1);
  603. }else{
  604. if(retObj.get("result").toString().equalsIgnoreCase("false")){
  605. setMenuStatus(retObj.getString("message"),-1);
  606. }
  607. }
  608. }
  609. }
  610. }
  611. public void resetBlanking() {
  612. tjFeedingFlag = 0;
  613. tjStatus = 0;
  614. productSnFeeding.setText("");
  615. setMenuStatus("工件结果已提交,等待下一件",0);
  616. // resetBtn.setEnabled(true);
  617. }
  618. public void resetFeeding() {
  619. // productSnFeeding.setText("");
  620. // tjFeedingFlag = 0;
  621. // setMenuStatus("等待请求扫码",0);
  622. resetFeedingBtn.setEnabled(true);
  623. writeAllowFeeding(false);
  624. }
  625. public void resetScanA() {
  626. tjFeedingFlag = 0;
  627. tjFlag = 0;
  628. tjStatus = 0;
  629. // productSn.setText("");
  630. productSnFeeding.setText("");
  631. setMenuStatus("等待请求扫码",0);
  632. // resetBtn.setEnabled(true);
  633. resetFeedingBtn.setEnabled(true);
  634. if(isLocal == 0){
  635. writeAllowFeeding(false);
  636. writeAllowBlanking(false);
  637. }
  638. }
  639. // 与设备交互-----------------------------------------------------------------------------------------
  640. // 请求扫码
  641. public boolean readFeeding(){
  642. if(isLocal == 1){
  643. return false;
  644. }else{
  645. return readBoolean(plcDb+".12.0");
  646. }
  647. }
  648. // 读取下料信号
  649. public boolean readBlanking(){
  650. if(isLocal == 1){
  651. return false;
  652. }else{
  653. return readBoolean(plcDb+".12.2");
  654. }
  655. }
  656. // 读取设备运行信号
  657. public boolean readRunning(){
  658. if(isLocal == 1){
  659. return true;
  660. }else{
  661. return readBoolean(plcDb+".12.1");
  662. }
  663. }
  664. // 读取设备通讯状态
  665. public boolean readDeviceStatus(){
  666. if(isLocal == 1){
  667. return true;
  668. }else{
  669. return readBoolean(plcDb+".12.5");
  670. }
  671. }
  672. // 读取MES允许上料信号
  673. public boolean readAllowFeeding(){
  674. if(isLocal == 1){
  675. return false;
  676. }else{
  677. return readBoolean(plcDb+".0.0");
  678. }
  679. }
  680. // 写入MES允许上料信号
  681. public boolean writeAllowFeeding(Boolean b){
  682. if(isLocal == 1){
  683. return true;
  684. }else{
  685. return writeBoolean(plcDb+".0.0",b);
  686. }
  687. }
  688. // 读取MES允许下料信号
  689. public boolean readAllowBlanking(){
  690. if(isLocal == 1){
  691. return true;
  692. }else{
  693. return readBoolean(plcDb+".0.1");
  694. }
  695. }
  696. // 写入MES允许下料信号
  697. public boolean writeAllowBlanking(Boolean b){
  698. if(isLocal == 1){
  699. return true;
  700. }else{
  701. return writeBoolean(plcDb+".0.1",b);
  702. }
  703. }
  704. // 读取二维码
  705. public String readQrcode(){
  706. if(isLocal == 1){
  707. return "";
  708. }else{
  709. String sn = s7PLC.readString(plcDb+".24.0", 33).trim();
  710. return sn;
  711. }
  712. }
  713. public boolean readBoolean(String db){
  714. boolean permit;
  715. try {
  716. permit = s7PLC.readBoolean(db);
  717. // 值变化时才打印日志(排除心跳地址.0.4和.0.6)
  718. Boolean lastValue = lastBooleanValues.get(db);
  719. if (lastValue == null || lastValue != permit) {
  720. if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) {
  721. log.info("<"+this.oprno+this.pos+">读取"+db+"-->变化:{} -> {}", lastValue, permit);
  722. }
  723. lastBooleanValues.put(db, permit);
  724. }
  725. } catch (Exception e) {
  726. log.error("<"+this.oprno+this.pos+">读取"+db+"-->失败", e);
  727. return false;
  728. }
  729. return permit;
  730. }
  731. public boolean writeBoolean(String db, boolean permit){
  732. try {
  733. // 值变化时才写入和打印日志(排除心跳地址.0.4和.0.6)
  734. Boolean lastValue = lastWriteBooleanValues.get(db);
  735. if (lastValue == null || lastValue != permit) {
  736. s7PLC.writeBoolean(db,permit);
  737. if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) {
  738. log.info("<"+this.oprno+this.pos+">写入"+db+"-->变化:{} -> {}", lastValue, permit);
  739. }
  740. lastWriteBooleanValues.put(db, permit);
  741. }
  742. } catch (Exception e) {
  743. log.error("<"+this.oprno+this.pos+">写入"+db+"-->失败,写入内容:{}", permit ? "true" : "false");
  744. log.error(e.getMessage(), e);
  745. return false;
  746. }
  747. return true;
  748. }
  749. public void getToolLife(){
  750. try{
  751. int i=0;
  752. List<String> ps = new ArrayList<>();
  753. for(Integer toolNum : toolNums){
  754. if(i < toolNm){
  755. String life = String.valueOf(getLife(modbusTcp,toolNum));
  756. toolLife[i] = life;
  757. ps.add(toolNum+"_"+life);
  758. i++;
  759. }
  760. }
  761. if(!toolLife[i].isEmpty()){
  762. DataUtil.saveLife(lineSn,oprno,productSnFeeding.getText(),MesClient.user_menu.getText(),String.join("\\|",ps));
  763. }
  764. }catch (Exception e){
  765. e.printStackTrace();
  766. }
  767. }
  768. //
  769. public Integer getLife(ModbusTcp modbusTcp,Integer i){
  770. try{
  771. if(isLocal == 1){
  772. return 0;
  773. }else{
  774. // short max1 = modbusTcp.readInt16((2103+(i*10))*2+3);
  775. // short cur1 = modbusTcp.readInt16((2103+(i*10))*2+1);
  776. // return max1 - cur1;
  777. Integer st = 500; // 宏变量对应R变量 901 - 551 @551=>R151 @901=>R501
  778. Integer max1 = modbusTcp.readInt32((st + i)*2, EByteBuffFormat.DC_BA);
  779. return max1;
  780. }
  781. }catch (Exception e){
  782. e.printStackTrace();
  783. return 0;
  784. }
  785. }
  786. public void initS7(){
  787. try {
  788. if(s7Connector == null) {
  789. //PLC地址
  790. String ipAddress = deviceIp;
  791. log.info("deviceIp2:"+deviceIp);
  792. //默认端口
  793. int port = 102;
  794. int rack=0;
  795. int slot=3;
  796. int timeout=5000;
  797. s7Connector=
  798. S7ConnectorFactory
  799. .buildTCPConnector()
  800. .withHost(ipAddress)
  801. .withPort(port) //optional
  802. // .withRack(rack) //optional
  803. // .withSlot(slot) //optional
  804. .withTimeout(timeout) //连接超时时间
  805. .build();
  806. S7Serializer s7Serializer2L = S7SerializerFactory.buildSerializer(s7Connector);
  807. }
  808. } catch (Exception e) { // 如果连接失败保存失败再次尝试连接一次
  809. s7Connector = null;
  810. e.printStackTrace();
  811. }
  812. }
  813. public Timer cjTimerText33;
  814. public void monitorDevice(){
  815. if(cjTimerText33!=null) {
  816. cjTimerText33.cancel();
  817. }
  818. cjTimerText33 = new Timer();
  819. cjTimerText33.schedule(new TimerTask() {
  820. public void run() {
  821. log.info("monitorDevice");
  822. if(oprno.equals("OP150")){
  823. if(tjFeedingFlag == 2 && !productSnFeeding.getText().isEmpty()){
  824. try{
  825. String zzzs = String.valueOf(s7PLCFSW.readSpindleRate());
  826. String jgsd = String.valueOf(s7PLCFSW.readFeedRate());
  827. String record_time = DateLocalUtils.getCurrentTime();
  828. hjparams.add(zzzs+"|"+jgsd+"|"+record_time);
  829. if (hjparams.size() == 60 && productSn.getText() != null && !productSnFeeding.getText().trim().isEmpty() ) {
  830. JdbcUtils.insertParamData(oprno+pos,MesClient.mes_line_sn,productSn.getText(), JSON.toJSONString(hjparams));
  831. hjparams = new ArrayList<>();
  832. }
  833. }catch (Exception e){
  834. e.printStackTrace();
  835. }
  836. log.info("monitorDevice:"+JSON.toJSONString(hjparams));
  837. }else{
  838. if(hjparams.size() > 0){
  839. JdbcUtils.insertParamData(oprno+pos,MesClient.mes_line_sn,lastSn, JSON.toJSONString(hjparams));
  840. hjparams = new ArrayList<>();
  841. }
  842. }
  843. }
  844. }
  845. }, 1000,1000);
  846. }
  847. public void monitorDeviceOld(){
  848. log.info("monitorDevice");
  849. if(tjFlag == 2){
  850. try{
  851. String zzzs = String.valueOf(s7PLCFSW.readSpindleRate());
  852. String jgsd = String.valueOf(s7PLCFSW.readFeedRate());
  853. // String zzzs = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 0));
  854. // String jgsd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 4));
  855. String zzzb = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 8));
  856. String ylz = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 12));
  857. Integer hjjs = S7Util.formatS7Int(s7Connector.read(DaveArea.DB, 9051, 2, 26));
  858. String hjcd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 28));
  859. String hjms = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 32));
  860. String record_time = DateLocalUtils.getCurrentTime();
  861. hjparams.add(zzzs+"|"+jgsd+"|"+zzzb+"|"+ylz+"|"+hjjs+"|"+hjcd+"|"+hjms+"|"+record_time);
  862. if (hjparams.size() == 60 && productSn.getText() != null && !productSn.getText().trim().isEmpty() ) {
  863. JdbcUtils.insertParamData(oprno+pos,MesClient.mes_line_sn,productSn.getText(), JSON.toJSONString(hjparams));
  864. hjparams = new ArrayList<>();
  865. }
  866. }catch (Exception e){
  867. e.printStackTrace();
  868. s7Connector = null;
  869. }
  870. log.info("monitorDevice:"+JSON.toJSONString(hjparams));
  871. }else{
  872. if(hjparams.size() > 0){
  873. JdbcUtils.insertParamData(oprno,MesClient.mes_line_sn,lastSn, JSON.toJSONString(hjparams));
  874. hjparams = new ArrayList<>();
  875. }
  876. }
  877. }
  878. public String readResult(){
  879. String result = "OK";
  880. boolean resultOk = false;
  881. boolean resultNg = false;
  882. try {
  883. resultOk = s7PLC.readBoolean(plcDb+".12.3");
  884. log.info("<4>读取信号-->读取工位加工结果是否OK成功,结果:{}", resultOk ? "true" : "false");
  885. resultNg = s7PLC.readBoolean(plcDb+".12.4");
  886. log.info("<4>读取信号-->读取工位加工结果是否NG成功,结果:{}", resultNg ? "true" : "false");
  887. if(resultOk){
  888. return "OK";
  889. }
  890. if(resultNg){
  891. return "NG";
  892. }
  893. } catch (Exception e) {
  894. return result;
  895. }
  896. return result;
  897. }
  898. }