package com.mes.component; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.github.s7connector.api.DaveArea; import com.github.s7connector.api.S7Connector; import com.github.s7connector.api.S7Serializer; import com.github.s7connector.api.factory.S7ConnectorFactory; import com.github.s7connector.api.factory.S7SerializerFactory; import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; import com.mes.ui.CacheData; import com.mes.ui.DataUtil; import com.mes.ui.MesClient; import com.mes.ui.ParamReq; import com.mes.util.DateLocalUtils; import com.mes.util.JdbcUtils; import com.mes.util.S7Util; //import com.mes.util.S7Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class WorkJPanelOP20 extends JPanel { public static final Logger log = LoggerFactory.getLogger(WorkJPanelOP20.class); public String oprno = "OP040"; public String pos = "A"; public String lastSn = ""; // 最近的码 public String lineSn = ""; public JLabel feedinglabel; public JLabel feedingmeslabel; public JLabel runlabel; //运行反馈, public JLabel blankinglabel; // 下料反馈 public JLabel blankingmeslabel; // 下料MES反馈 public JLabel feedingtodevicelabel; // 上料到设备 public JButton scanBtn; public JTextField productSn; public JTextField productSnFeeding; public JButton status_menu; public JButton resetBtn; public JButton resetFeedingBtn; public ImageIcon imageGreen; public ImageIcon imageGray; public String plcIp1 = "192.168.90.81"; // plc1 西侧 public String plcIp2 = "192.168.90.82"; // plc2 东侧 public String deviceIp = ""; public String deviceIp1 = "80.20.10.42"; public String deviceIp2 = "80.20.10.43"; public String deviceIp3 = "80.21.10.42"; public String deviceIp4 = "80.21.10.43"; // FSW3 机床IP 按现有规律暂定,待现场确认 public String deviceIp5 = "80.20.10.44"; // 西侧 FSW3 public String deviceIp6 = "80.21.10.44"; // 东侧 FSW3 public S7Connector s7Connector; public S7PLC s7PLC = null; public S7PLC s7PLCFSW = null; public Integer workWidth = 470; public Integer workHeight = 300; public String plcDb = ""; public String plcIp = ""; // 当前连接的 PLC IP public JLabel ipLabel; // 界面显示 PLC/DB/机床IP public String plcDb2 = "DB7003"; public String preSn = ""; // 扫码内容 public Integer tjFeedingFlag = 0; // 上料状态标识 public Integer tjFlag = 0; // 1=可加工 2=设备运行中 3=加工结束 public Integer tjStatus = 0; // 提交标识 public Boolean toDevice = false; public Boolean plcHeart = false; public Integer isLocal = 0; // 0=正式 1=本地 public Integer isScan = 1; // 1=西侧 2=东测 public Boolean fswOnline = true; // public String finallySn = ""; public List hjparams = new ArrayList<>(); public Integer errflag = 0; // 1=报警 0=正常 public Integer errflag2 = 0; public String fswDeviceName = ""; public JLabel oprnoLabel2; public JLabel oprnoLabel; // 缓存上一次的信号值,用于检测变化(key=地址, value=上次值) private java.util.Map lastBooleanValues = new java.util.HashMap<>(); private java.util.Map lastWriteBooleanValues = new java.util.HashMap<>(); public JButton signalBtn; // 信号监控按钮 public PlcSignalMonitor signalMonitor; // 信号监控组件 public CacheData cacheData = null; public String cacheName = ""; public WorkJPanelOP20(String position){ this.setLayout(null); this.setBackground(Color.WHITE); this.pos = position; this.lineSn = MesClient.mes_line_sn; cacheName = "cache_"+oprno+pos; cacheData = JdbcUtils.getCache(cacheName); if(cacheData == null){ cacheData = new CacheData(); cacheData.setLastSn(""); cacheData.setSn(""); cacheData.setFeedingSn(""); cacheData.setTjStatus(0); cacheData.setTjFlag(0); cacheData.setError(-1); cacheData.setMsg(""); JdbcUtils.setCache(cacheName,JSON.toJSONString(cacheData)); } if(MesClient.mes_line_pos.equals("east")){ // 东侧 plcIp = plcIp2; s7PLC = new S7PLC(EPlcType.S1500, plcIp2); isScan = 2; log.info("IP:"+plcIp2); }else{ plcIp = plcIp1; s7PLC = new S7PLC(EPlcType.S1500, plcIp1); isScan = 1; log.info("IP:"+plcIp1); } s7PLC.setConnectTimeout(800); s7PLC.setReceiveTimeout(800); // 子工位 -> DB块 / 机床IP / 设备名(西侧 A/B/E,东侧 C/D/F,E/F 为新增 FSW3) switch (pos) { case "A": plcDb = "DB7000"; deviceIp = deviceIp1; fswDeviceName = "西侧反面1号机"; break; case "B": plcDb = "DB7001"; deviceIp = deviceIp2; fswDeviceName = "西侧反面2号机"; break; case "E": plcDb = "DB7004"; deviceIp = deviceIp5; fswDeviceName = "西侧反面3号机"; break; case "C": plcDb = "DB7000"; deviceIp = deviceIp3; fswDeviceName = "东侧反面1号机"; break; case "D": plcDb = "DB7001"; deviceIp = deviceIp4; fswDeviceName = "东侧反面2号机"; break; case "F": plcDb = "DB7004"; deviceIp = deviceIp6; fswDeviceName = "东侧反面3号机"; break; default: log.error("未知子工位:" + pos); break; } log.info("deviceIp:"+deviceIp); s7PLCFSW = new S7PLC(EPlcType.SINUMERIK_828D, deviceIp); s7PLCFSW.setConnectTimeout(800); s7PLCFSW.setReceiveTimeout(800); oprnoLabel = new JLabel(oprno+pos); oprnoLabel.setHorizontalAlignment(SwingConstants.CENTER); oprnoLabel.setForeground(Color.BLACK); oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20)); oprnoLabel.setBounds(0, -10, workWidth/5, 40); this.add(oprnoLabel); oprnoLabel2 = new JLabel(fswDeviceName); oprnoLabel2.setHorizontalAlignment(SwingConstants.CENTER); oprnoLabel2.setForeground(Color.BLACK); oprnoLabel2.setFont(new Font("微软雅黑", Font.PLAIN, 14)); oprnoLabel2.setBounds(0, 12, workWidth / 5, 40); this.add(oprnoLabel2); // 底部显示 PLC/DB/机床IP,机床离线时变红 ipLabel = new JLabel("PLC " + plcIp + " | " + plcDb + " | 机床 " + deviceIp); ipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12)); ipLabel.setForeground(Color.DARK_GRAY); ipLabel.setBounds(5, 230, workWidth*2/3 + 55, 26); this.add(ipLabel); status_menu = new JButton("等待请求扫码"); status_menu.setBounds(workWidth/5, 0, workWidth*4/5, 40); status_menu.setForeground(Color.GREEN); status_menu.setForeground(Color.GREEN); status_menu.setFont(new Font("微软雅黑", Font.PLAIN, 16)); status_menu.setBackground(Color.BLACK); this.add(status_menu); JLabel oprnoLabelFeeding = new JLabel("上料"); oprnoLabelFeeding.setHorizontalAlignment(SwingConstants.CENTER); oprnoLabelFeeding.setForeground(Color.BLACK); oprnoLabelFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 20)); oprnoLabelFeeding.setBounds(0, 50, workWidth/5, 40); this.add(oprnoLabelFeeding); productSnFeeding = new JTextField(""); productSnFeeding.setHorizontalAlignment(SwingConstants.CENTER); productSnFeeding.setForeground(Color.BLACK); productSnFeeding.setFont(new Font("微软雅黑", Font.PLAIN, 16)); productSnFeeding.setEditable(false); productSnFeeding.setBounds(workWidth/5, 50, workWidth*3/5, 40); productSnFeeding.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { String sn = productSnFeeding.getText().trim(); if(!sn.isEmpty()){ MesClient.createQRCode(productSnFeeding.getText(),oprno+pos); } } }); this.add(productSnFeeding); JLabel oprnoLabelWork = new JLabel("加工"); oprnoLabelWork.setHorizontalAlignment(SwingConstants.CENTER); oprnoLabelWork.setForeground(Color.BLACK); oprnoLabelWork.setFont(new Font("微软雅黑", Font.PLAIN, 20)); oprnoLabelWork.setBounds(0, 100, workWidth/5, 40); this.add(oprnoLabelWork); productSn = new JTextField(""); productSn.setHorizontalAlignment(SwingConstants.CENTER); productSn.setForeground(Color.BLACK); productSn.setFont(new Font("微软雅黑", Font.PLAIN, 16)); productSn.setEditable(false); productSn.setBounds(workWidth/5, 100, workWidth*3/5, 40); productSn.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { String sn = productSn.getText().trim(); if(!sn.isEmpty()){ MesClient.createQRCode(productSn.getText(),oprno+pos); } } }); this.add(productSn); resetFeedingBtn = new JButton("刷新"); resetFeedingBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int choice = JOptionPane.showConfirmDialog(null, "确定刷新上料工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { resetFeeding(); } } }); resetFeedingBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22)); resetFeedingBtn.setBounds(workWidth*4/5 + 10, 50, workWidth/5 - 10, 40); this.add(resetFeedingBtn); resetBtn = new JButton("刷新"); resetBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int choice = JOptionPane.showConfirmDialog(null, "确定刷新加工工件吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { resetBlanking(); } } }); resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 22)); resetBtn.setBounds(workWidth*4/5 + 10, 100, workWidth/5 - 10, 40); this.add(resetBtn); imageGreen = new ImageIcon(MesClient.class.getResource("/bg/green_dot.png")); imageGreen.setImage(imageGreen.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)); imageGray = new ImageIcon(MesClient.class.getResource("/bg/grey_dot.png")); imageGray.setImage(imageGray.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)); feedinglabel = new JLabel("请求扫码"); feedinglabel.setHorizontalAlignment(SwingConstants.LEFT); feedinglabel.setIcon(imageGreen); feedinglabel.setForeground(Color.BLACK); feedinglabel.setBounds(0, 150, workWidth/3, 30); this.add(feedinglabel); feedingmeslabel = new JLabel("MES扫码反馈"); feedingmeslabel.setHorizontalAlignment(SwingConstants.LEFT); feedingmeslabel.setIcon(imageGreen); feedingmeslabel.setForeground(Color.BLACK); feedingmeslabel.setBounds(workWidth/3, 150, workWidth/3, 30); feedingmeslabel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { boolean ret = readAllowFeeding(); int choice = JOptionPane.showConfirmDialog(null, "确定修改MES扫码反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { writeAllowFeeding(!ret); } } }); this.add(feedingmeslabel); runlabel = new JLabel("设备运行中"); runlabel.setHorizontalAlignment(SwingConstants.LEFT); runlabel.setIcon(imageGreen); runlabel.setForeground(Color.BLACK); runlabel.setBounds(workWidth*2/3, 150, workWidth/3, 30); this.add(runlabel); blankinglabel = new JLabel("下料请求"); blankinglabel.setHorizontalAlignment(SwingConstants.LEFT); blankinglabel.setIcon(imageGreen); blankinglabel.setForeground(Color.BLACK); blankinglabel.setBounds(0, 190, workWidth/3, 30); this.add(blankinglabel); blankingmeslabel = new JLabel("MES下料反馈"); blankingmeslabel.setHorizontalAlignment(SwingConstants.LEFT); blankingmeslabel.setIcon(imageGreen); blankingmeslabel.setForeground(Color.BLACK); blankingmeslabel.setBounds(workWidth/3, 190, workWidth/3, 30); this.add(blankingmeslabel); blankingmeslabel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { boolean ret = readAllowBlanking(); int choice = JOptionPane.showConfirmDialog(null, "确定修改MES下料反馈结果为"+(ret?"false":"true")+"吗?", "操作确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { writeAllowBlanking(!ret); } } }); // 信号监控按钮 signalMonitor = new PlcSignalMonitor(s7PLC, plcDb, oprno + pos, MesClient.mesClientFrame); signalBtn = signalMonitor.createSignalButton(); signalBtn.setBounds(workWidth*2/3 + 63, 230, 90, 26); this.add(signalBtn); heartData(); startOprnoTimer(); monitorDeviceStatus(); // if(pos.equals("A")||pos.equals("C")){ // monitorDeviceStatus(); // } resetScanA(); formatElementUI(); } public void formatElementUI(){ cacheData = JdbcUtils.getCache(cacheName); log.info("cacheData1:"+cacheData.toString()); if(cacheData == null){ return; } String msg = cacheData.getMsg(); int error = cacheData.getError(); String sn = cacheData.getSn(); productSn.setText(sn); String feedingSn = cacheData.getFeedingSn(); productSnFeeding.setText(feedingSn); tjFlag = cacheData.getTjFlag(); lastSn = cacheData.getLastSn(); setMenuStatus(msg,error); this.repaint(); } public void setMenuStatus(String msg,int error){ if(error == 0){ this.status_menu.setForeground(Color.GREEN); }else{ this.status_menu.setForeground(Color.RED); } this.status_menu.setText(msg); cacheData.setMsg(msg); cacheData.setError(error); cacheData.setTjFlag(tjFlag); cacheData.setTjStatus(tjStatus); cacheData.setLastSn(lastSn); cacheData.setSn(productSn.getText()); cacheData.setFeedingSn(productSnFeeding.getText()); JdbcUtils.setCache(cacheName, JSON.toJSONString(cacheData)); } // 定时任务 ----------------------------------------------------------------------------------------------------- public Timer cjTimerText2; public void heartData() { if(cjTimerText2!=null) { cjTimerText2.cancel(); } cjTimerText2 = new Timer(); cjTimerText2.schedule(new TimerTask() { public void run() { if(MesClient.isLogin == 1 && isLocal == 0){ plcHeart = !plcHeart; writeBoolean(plcDb+".0.6",plcHeart); } } }, 1000,1000); } public Timer cjTimerText3; public void monitorDeviceStatus() { if(cjTimerText3!=null) { cjTimerText3.cancel(); } cjTimerText3 = new Timer(); cjTimerText3.schedule(new TimerTask() { public void run() { if(MesClient.isLogin == 1 && isLocal == 0){ try{ initS7(); monitorDevice(); boolean ret = S7Util.getFswAlarm(s7Connector); if(ret){ errflag = 0; } if(!ret && errflag == 0 ){ errflag = 1; DataUtil.deviceAlarm(oprno+pos,productSn.getText(),oprno+pos+"随动夹具报警",MesClient.user_menu.getText()); } fswOnline = true; }catch (Exception e){ log.info(e.getMessage()); fswOnline = false; } if(fswOnline){ oprnoLabel.setForeground(Color.BLACK); oprnoLabel2.setForeground(Color.BLACK); ipLabel.setForeground(Color.DARK_GRAY); }else{ oprnoLabel.setForeground(Color.RED); oprnoLabel2.setForeground(Color.RED); ipLabel.setForeground(Color.RED); } try{ int ret = getAlarm(); if(ret == 0){ errflag2 = 0; } if(ret != 0 && errflag2 == 0 ){ String content = oprno+pos; if(ret == 1){ content += "安全门故障"; }else if(ret == 2){ content += "急停故障"; }else if(ret == 3){ content += "光栅故障"; }else if(ret == 4){ content += "夹紧故障"; } errflag2 = 1; DataUtil.deviceAlarm(oprno+pos,productSn.getText(),content,MesClient.user_menu.getText()); } }catch (Exception e){ } } } }, 1000,1000); } public Timer oprnoTimer; public void startOprnoTimer() { //心跳显示图标 if(oprnoTimer!=null) { oprnoTimer.cancel(); } oprnoTimer = new Timer(); oprnoTimer.schedule(new TimerTask() { public void run() { try{ if(MesClient.isLogin == 1){ Boolean retFeeding = readBoolean(plcDb+".0.0"); feedinglabel.setIcon(retFeeding?imageGreen:imageGray); Boolean retFeedingAllow = readBoolean(plcDb+".0.4"); feedingmeslabel.setIcon(retFeedingAllow?imageGreen:imageGray); if(!retFeeding && retFeedingAllow){ writeAllowFeeding(false); } Boolean retBlanking = readBoolean(plcDb+".0.1"); blankinglabel.setIcon(retBlanking?imageGreen:imageGray); Boolean retAllowBlanking = readBoolean(plcDb+".0.5"); blankingmeslabel.setIcon(retAllowBlanking?imageGreen:imageGray); Boolean retRunning = readBoolean(plcDb+".0.2"); runlabel.setIcon(retRunning?imageGreen:imageGray); if(retBlanking && productSn.getText().isEmpty()){ writeAllowBlanking(true); resetBlanking(); } //上料 if(tjFeedingFlag == 0){ if(retFeeding){ barCode(); } if(retRunning){ resetFeeding(); } }else if(tjFeedingFlag == 1){ if(retRunning){ tjFlag = 1; productSn.setText(productSnFeeding.getText()); resetFeeding(); } } if(tjFlag == 0 && !retBlanking){ writeBoolean(plcDb+".0.5",false); } if(tjFlag == 1){ if(retRunning){ tjFlag = 2; setMenuStatus("工件加工中", 0); // 运行中清除下料反馈 writeAllowBlanking(false); } }else if(tjFlag == 2){ if(retBlanking){ // 运行结束 tjFlag = 3; setMenuStatus("工件加工结束,结果上传中", 0); if(tjStatus == 0){ tjStatus = 1; JSONObject retObj = DataUtil.sendQuality(productSn.getText(),oprno+pos,"OK",MesClient.user_menu.getText()); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { resetBlanking(); }else{ if(retObj.get("result")==null){ setMenuStatus("请求失败,请重试",-1); }else{ if(retObj.get("result").toString().equalsIgnoreCase("false")){ setMenuStatus(retObj.getString("message"),-1); } } } } } } } }catch (Exception e) { log.error(e.getMessage()); } } }, 100,1500); } public void barCode2(){ String sn = ""; if(isScan == 1){ sn = MesClient.scanQrcode2(); }else{ sn = MesClient.scanQrcode3(); } preSn = sn.trim(); log.info("preSn:"+preSn); MesClient.setMenuStatus("工件码:"+sn, 0); // 兼容 CZ060 16位底板码和原19位码 if(sn.length() != 16 && sn.length() != 19){ // MesClient.setMenuStatus("扫码结果为空", -1); }else{ finallySn = sn; //全局码 productSnFeeding.setText(sn); // writeScanFinish(true); } } public void barCode(){ //如果为空则不进行上料 if(!productSnFeeding.getText().isEmpty()){ return; } String sn = readQrcode(); if(sn.equals("")){ if(productSnFeeding.getText().isEmpty()){ productSnFeeding.setText(""); this.setMenuStatus("扫码结果为空", -1); } }else{ productSnFeeding.setText(sn); lastSn = sn; // 判断刀具寿命 // Integer hjjs = S7Util.formatS7Int(s7Connector.read(DaveArea.DB, 9051, 2, 26)); // if(hjjs > 320){ // this.setMenuStatus("搅拌头达到使用寿命,请换搅拌头", -1); // return; // } // 查询工件质量 JSONObject retObj = DataUtil.checkQuality(sn,oprno+pos,MesClient.user_menu.getText()); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { tjFeedingFlag = 1;//可加工 setMenuStatus("工件可以加工", 0); writeAllowFeeding(true); //允许下料 // resetFeedingBtn.setEnabled(false); }else{ writeAllowFeeding(false); resetFeedingBtn.setEnabled(true); if(retObj.get("result")==null){ setMenuStatus("请求失败,请重试",-1); }else{ if(retObj.get("result").toString().equalsIgnoreCase("false")){ setMenuStatus(retObj.getString("message"),-1); } } } } } public void resetBlanking() { tjFlag = 0; tjStatus = 0; productSn.setText(""); setMenuStatus("工件结果已提交,等待下一件",0); resetBtn.setEnabled(true); // writeAllowFeeding(false); // writeAllowBlanking(false); } public void resetFeeding() { productSnFeeding.setText(""); tjFeedingFlag = 0; setMenuStatus("等待请求扫码",0); resetFeedingBtn.setEnabled(true); writeAllowFeeding(false); } public void resetScanA() { tjFeedingFlag = 0; tjFlag = 0; tjStatus = 0; productSn.setText(""); productSnFeeding.setText(""); resetBtn.setEnabled(true); resetFeedingBtn.setEnabled(true); if(isLocal == 0){ writeAllowFeeding(false); writeAllowBlanking(false); } } //原来的扫码为上料 // 与设备交互----------------------------------------------------------------------------------------- // 请求扫码 public boolean readScanFeeding(){ if(isLocal == 1){ return false; }else{ return readBoolean(plcDb2+".0.0"); //7002 扫码 } } // 扫码完成 public boolean readScanFinish(){ if(isLocal == 1){ return false; }else{ return readBoolean(plcDb2+".0.1"); //扫码完成 } } // 扫码完成反馈 public boolean writeScanFinish(boolean bt){ if(isLocal == 1){ return false; }else{ return writeBoolean(plcDb2+".0.1",bt); } } // 请求扫码 public boolean readFeeding(){ if(isLocal == 1){ return false; }else{ return readBoolean(plcDb+".0.0"); } } // 读取上料到设备 public boolean readFeedingToDevice(){ if(isLocal == 1){ return true; }else{ return readBoolean(plcDb+".0.7"); } } // 读取下料信号 public boolean readBlanking(){ if(isLocal == 1){ return false; }else{ return readBoolean(plcDb+".0.1"); } } // 读取设备运行信号 public boolean readRunning(){ if(isLocal == 1){ return true; }else{ return readBoolean(plcDb+".0.2"); } } // 读取设备通讯状态 public boolean readDeviceStatus(){ if(isLocal == 1){ return true; }else{ return readBoolean(plcDb+".0.3"); } } // 读取MES允许上料信号 public boolean readAllowFeeding(){ if(isLocal == 1){ return false; }else{ return readBoolean(plcDb+".0.4"); } } // 写入MES允许上料信号 public boolean writeAllowFeeding(Boolean b){ if(isLocal == 1){ return true; }else{ return writeBoolean(plcDb+".0.4",b); } } // 读取MES允许下料信号 public boolean readAllowBlanking(){ if(isLocal == 1){ return true; }else{ return readBoolean(plcDb+".0.5"); } } // 写入MES允许下料信号 public boolean writeAllowBlanking(Boolean b){ if(isLocal == 1){ return true; }else{ return writeBoolean(plcDb+".0.5",b); } } // 控制扫码枪读取二维码 public String readQrcode(){ // String sn = "5001040043561010006"; String sn = ""; // String sn = finallySn; if(isLocal == 1){ return ""; }else{ return MesClient.preSn20; } } public boolean readBoolean(String db){ boolean permit; try { permit = s7PLC.readBoolean(db); // 值变化时才打印日志(排除心跳地址.0.4和.0.6) Boolean lastValue = lastBooleanValues.get(db); if (lastValue == null || lastValue != permit) { if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) { log.info("<"+this.oprno+this.pos+">读取"+db+"-->变化:{} -> {}", lastValue, permit); } lastBooleanValues.put(db, permit); } } catch (Exception e) { log.error("<"+this.oprno+this.pos+">读取"+db+"-->失败", e); return false; } return permit; } public boolean writeBoolean(String db, boolean permit){ try { // 值变化时才写入和打印日志(排除心跳地址.0.4和.0.6) Boolean lastValue = lastWriteBooleanValues.get(db); if (lastValue == null || lastValue != permit) { s7PLC.writeBoolean(db,permit); if (!db.endsWith(".0.4") && !db.endsWith(".0.6")) { log.info("<"+this.oprno+this.pos+">写入"+db+"-->变化:{} -> {}", lastValue, permit); } lastWriteBooleanValues.put(db, permit); } } catch (Exception e) { log.error("<"+this.oprno+this.pos+">写入"+db+"-->失败,写入内容:{}", permit ? "true" : "false"); log.error(e.getMessage(), e); return false; } return true; } public void initS7(){ try { if(s7Connector == null) { //PLC地址 String ipAddress = deviceIp; log.info("deviceIp2:"+deviceIp); //默认端口 int port = 102; int rack=0; int slot=3; int timeout=5000; s7Connector= S7ConnectorFactory .buildTCPConnector() .withHost(ipAddress) .withPort(port) //optional // .withRack(rack) //optional // .withSlot(slot) //optional .withTimeout(timeout) //连接超时时间 .build(); S7Serializer s7Serializer2L = S7SerializerFactory.buildSerializer(s7Connector); } } catch (Exception e) { // 如果连接失败保存失败再次尝试连接一次 s7Connector = null; e.printStackTrace(); } } public void monitorDevice(){ log.info("monitorDevice"); if(tjFlag == 2){ try{ String zzzs = String.valueOf(s7PLCFSW.readSpindleRate()); String jgsd = String.valueOf(s7PLCFSW.readFeedRate()); // String zzzs = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 0)); // String jgsd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 4)); String zzzb = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 8)); String ylz = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 12)); Integer hjjs = S7Util.formatS7Int(s7Connector.read(DaveArea.DB, 9051, 2, 26)); String hjcd = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 28)); String hjms = S7Util.formatS7Val(s7Connector.read(DaveArea.DB, 9051, 4, 32)); String record_time = DateLocalUtils.getCurrentTime(); hjparams.add(zzzs+"|"+jgsd+"|"+zzzb+"|"+ylz+"|"+hjjs+"|"+hjcd+"|"+hjms+"|"+record_time); if (hjparams.size() == 60 && productSn.getText() != null && !productSn.getText().trim().isEmpty() ) { JdbcUtils.insertParamData(oprno+pos,MesClient.mes_line_sn,productSn.getText(), JSON.toJSONString(hjparams)); hjparams = new ArrayList<>(); } }catch (Exception e){ e.printStackTrace(); s7Connector = null; } log.info("monitorDevice:"+JSON.toJSONString(hjparams)); }else{ if(hjparams.size() > 0){ JdbcUtils.insertParamData(oprno,MesClient.mes_line_sn,lastSn, JSON.toJSONString(hjparams)); hjparams = new ArrayList<>(); } } } public int getAlarm(){ boolean ret0 = readBoolean(plcDb+".6.0"); if(ret0){ return 1; } boolean ret1 = readBoolean(plcDb+".6.1"); if(ret1){ return 2; } boolean ret2 = readBoolean(plcDb+".6.2"); if(ret2){ return 3; } boolean ret4 = readBoolean(plcDb+".6.3"); if(ret4){ return 4; } return 0; } }