Parcourir la source

对接PLC程序

jingbo il y a 3 jours
Parent
commit
0b7a06db52

+ 83 - 0
src/com/mes/ui/DataUtil.java

@@ -2,7 +2,9 @@ package com.mes.ui;
 
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
+import com.mes.util.Base64Utils;
 import com.mes.util.JdbcUtils;
+import com.mes.util.QmParam;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -10,6 +12,7 @@ import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
@@ -291,4 +294,84 @@ public class DataUtil {
         }
         return resp;
     }
+
+
+    // 发送气密数据
+    public static JSONObject sendQualityParam(String user, String sn, String oprno, String lineSn, QmParam qmParam) {
+        try {
+            String enconding = "UTF-8";
+            InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
+            Properties pro = new Properties();
+            BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
+            pro.load(br);
+            String mes_server_ip = pro.getProperty("mes.server_ip");
+            String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmresult";
+            // 根据服务器端代码,对特定参数进行Base64编码
+            //测试压
+            String testPressureBase64 = Base64Utils.getBase64(qmParam.getTestPressure() != null ? qmParam.getTestPressure() : "");
+            // 泄露值
+            String leakValBase64 = Base64Utils.getBase64(qmParam.getLeakVal() != null ? qmParam.getLeakVal() : "");
+            // 测试时间
+            String testTimeBase64 = Base64Utils.getBase64(qmParam.getCsDuration() != null ? qmParam.getCsDuration() : "");
+            String titleBase64 = Base64Utils.getBase64(MesClient.mes_gw_des != null ? MesClient.mes_gw_des : "");
+
+            log.info("服务器端需要Base64编码的参数: testPressure='{}'->'{}', leakVal='{}'->'{}', testTime='{}'->'{}', title='{}'->'{}'"
+                    , qmParam.getTestPressure(), testPressureBase64, qmParam.getLeakVal(), leakValBase64
+                    , qmParam.getCsDuration(), testTimeBase64, MesClient.mes_gw_des, titleBase64);
+
+            String params = "__ajax=json&oprno="+ URLEncoder.encode(oprno != null ? oprno : "", "UTF-8")
+                    +"&lineSn="+URLEncoder.encode(lineSn != null ? lineSn : "", "UTF-8")
+                    +"&sn="+URLEncoder.encode(sn != null ? sn : "", "UTF-8")
+                    +"&workNum="+URLEncoder.encode(user != null ? user : "", "UTF-8")
+                    +"&cxm="+URLEncoder.encode("1" != null ? "1" : "", "UTF-8")
+                    +"&result="+URLEncoder.encode(qmParam.getTestResult() != null ? qmParam.getTestResult() : "", "UTF-8")
+                    +"&testPressure="+URLEncoder.encode(testPressureBase64, "UTF-8")
+                    +"&testPressureUnit="+URLEncoder.encode(qmParam.getTestPressureUnit() != null ? qmParam.getTestPressureUnit() : "", "UTF-8")
+                    +"&leakVal="+URLEncoder.encode(leakValBase64, "UTF-8")
+                    +"&leakValUnit="+URLEncoder.encode(qmParam.getLeakValUnit() != null ? qmParam.getLeakValUnit() : "", "UTF-8")
+                    +"&testTime="+URLEncoder.encode(testTimeBase64, "UTF-8")
+                    +"&title="+URLEncoder.encode(titleBase64, "UTF-8")
+                    +"&remark="+URLEncoder.encode(qmParam.getRemark() != null ? qmParam.getRemark() : "", "UTF-8")
+                    +"&deviceType="+URLEncoder.encode(qmParam.getDeviceType() != null ? qmParam.getDeviceType() : "", "UTF-8")
+                    // 添加服务器端期望的参数
+                    +"&cq="+URLEncoder.encode("", "UTF-8")
+                    +"&by="+URLEncoder.encode("", "UTF-8")
+                    +"&cs="+URLEncoder.encode("", "UTF-8")
+                    +"&leakRate="+URLEncoder.encode("", "UTF-8")
+                    +"&leakRateUnit="+URLEncoder.encode("", "UTF-8")
+                    +"&type="+URLEncoder.encode("", "UTF-8");
+
+            // 添加sessionid
+//            if (MesClient.sessionid != null && !MesClient.sessionid.isEmpty()) {
+//                params += "&__sid=" + MesClient.sessionid;
+//            }
+            log.info("params="+params);
+            String result = doPost(url,params);
+            log.info("result="+result);
+
+            // 检查result是否为null或空
+            if(result == null || result.trim().isEmpty()) {
+                log.error("HTTP请求返回结果为空,请检查服务器连接和参数格式");
+                log.info("压力值: '{}', 压力单位: '{}', 泄漏值: '{}', 泄漏单位: '{}'",
+                        qmParam.getTestPressure(), qmParam.getTestPressureUnit(), qmParam.getLeakVal(), qmParam.getLeakValUnit());
+                return null;
+            }
+
+            if(result.equalsIgnoreCase("false")) {
+                return null;
+            }else {
+                try {
+                    return JSONObject.parseObject(result);
+                } catch (Exception e) {
+                    log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result);
+                    return null;
+                }
+            }
+
+        }catch (Exception e){
+            log.error("发送气密数据异常: " + e.getMessage());
+            e.printStackTrace();
+        }
+
+    }
 }

+ 26 - 24
src/com/mes/ui/MesClient.java

@@ -1,12 +1,11 @@
 package com.mes.ui;
 
 import com.alibaba.fastjson2.JSONObject;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
 import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
-import com.mes.util.DateLocalUtils;
-import com.mes.util.HttpUtils;
-import com.mes.util.JdbcUtils;
-import com.mes.util.PLCUtils;
+import com.mes.util.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,6 +34,7 @@ public class MesClient extends JFrame {
     public static String mes_server_ip = ""; // 服务器IP地址
     public static int mes_heart_icon_cycle = 1;
     public static String mes_line_sn = ""; // 产线编号
+    public static String csyq =  "";
 
     //session
     public static String sessionid = "";
@@ -81,6 +81,9 @@ public class MesClient extends JFrame {
 
     public static Timer getSnBeatTimer;
 
+    // S7plc
+    public static S7PLC plc = new S7PLC(EPlcType.S1200,"192.168.2.1");
+
     public static void main(String[] args) {
 
         if (LockUtil.getInstance().isAppActive() == true){
@@ -124,6 +127,7 @@ public class MesClient extends JFrame {
         mes_gw =  pro.getProperty("mes.gw");
         mes_server_ip = pro.getProperty("mes.server_ip");
         mes_line_sn = pro.getProperty("mes.line_sn");
+        csyq = pro.getProperty("mes.csyq");
         mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
         log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";");
     }
@@ -618,7 +622,6 @@ public class MesClient extends JFrame {
         getSnBeatTimer.schedule(new TimerTask() {
             @Override
             public void run() {
-                System.out.println("kaishi");
                 if (work_status == 1) {
                     // TODO
                     //  加入工作流程
@@ -630,45 +633,44 @@ public class MesClient extends JFrame {
                         }
                     } else if (plc_status == 1) {
                         // 发送允许启动信号
-                        Boolean ret = PLCUtils.writeStartMethod();
+                        Boolean ret = PLCUtils.writeStartMethod(plc);
                         if (ret) {
                             plc_status = 2;
                         }
                     } else if (plc_status == 2) {
-                        // 等待开始加工信号
-                        Boolean ret = PLCUtils.readStartMethod();
-                        if (ret){
-                            boolean ret2 = PLCUtils.writeStopMethod();
-                            if (ret2){
-                                plc_status = 3;
-                            }
-                        }
-                    } else if (plc_status ==3) {
                         // 等待加工结束信号
-                        Boolean ret = PLCUtils.readStopMethod();
+                        Boolean ret = PLCUtils.readStopMethod(plc);
                         if (ret){
+                            // 将允许启动置空
+                            PLCUtils.writeStopMethod(plc);
+                            // 获取参数
+                            QmParam qmParam = PLCUtils.getParameter(plc);
+                            // 发送参数
+                            JSONObject retObj1 = DataUtil.sendQualityParam( user20, product_sn.getText().trim(), MesClient.mes_gw, MesClient.mes_line_sn,qmParam);
+                            if(retObj1 != null && retObj1.get("result") != null && retObj1.get("result").toString().equalsIgnoreCase("true")) {
+                                MesClient.resetScanA();
+                                MesClient.setMenuStatus("测试结果上传成功,请扫下一件",0);
+                            }else{
+                                MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
+                                return;
+                            }
+                            // 获取结果
+                            String result = PLCUtils.getResult(plc);
                             // 发送结果
                             String sn = product_sn.getText().trim();
-                            JSONObject retObj = DataUtil.sendQuality( sn,"OK",user20);
+                            JSONObject retObj = DataUtil.sendQuality( sn,result,user20);
                             if (retObj != null && "true".equalsIgnoreCase(retObj.getString("result"))) {
-                                // 发送允许下料
-                                PLCUtils.writeDownMethod();
                                 resetState();
                                 status_menu.setForeground(Color.GREEN);
                                 status_menu.setText("结果提交成功,请扫下一件");
                                 finish_ng_bt.setEnabled(false);
-
                             } else {
                                 status_menu.setForeground(Color.RED);
                                 String msg = (retObj != null && retObj.containsKey("message")) ? retObj.getString("message") : "提交失败";
                                 status_menu.setText(msg);
                             }
-                        }else {
-                            // 获取参数
-                            PLCUtils.getParameter();
                         }
                     }
-                    return;
                 } else if (work_status == 0) {
                     JSONObject result = DataUtil.getCurSn(mes_gw, mes_server_ip, mes_line_sn);
                     // 正常处理结果

+ 57 - 25
src/com/mes/util/PLCUtils.java

@@ -1,50 +1,82 @@
 package com.mes.util;
 
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
+import com.mes.ui.MesClient;
+
 public class PLCUtils {
 
     // 写入允许启动方法
-    public static boolean writeStartMethod() {
-        // TODO
+    public static boolean writeStartMethod(S7PLC plc) {
         // 对plc写入ture
+        plc.writeBoolean("V90.0",true);
         return true;
     }
 
     // 将允许启动置空
-    public static boolean writeStopMethod() {
-        // TODO
+    public static boolean writeStopMethod(S7PLC plc) {
+        plc.writeBoolean("V90.0",false);
         return true;
     }
 
-    // 读取PLC开始加工信号
-    public static boolean readStartMethod() {
-        // TODO
-        return true;
-    }
 
     // 读取PLC结束加工信号
-    public static boolean readStopMethod() {
-        // TODO
-        return true;
+    public static boolean readStopMethod(S7PLC plc) {
+        return plc.readBoolean("M4.0");
     }
 
-    // 给出允许下料信号
-    public static boolean writeDownMethod() {
-        // TODO
-        return true;
-    }
-
-    // 允许下料置空
-    public static boolean writeDownStopMethod() {
-        // TODO
-        return true;
-    }
 
     // 获取参数
-    public static void getParameter() {
+    public static QmParam getParameter(S7PLC plc) {
         //  TODO
-        return;
+        // 泄露值
+        String leakageValue = "0.0";
+        if ("comso".equals(MesClient.csyq)){
+            leakageValue = plc.readString("VB306", 8);
+        }else if ("ATEQ".equals(MesClient.csyq)){
+            leakageValue = plc.readFloat32("VD800")+"";
+        }
+        // 结果
+        String result = getResult(plc);
+        // 检测压
+        String pressure = plc.readFloat32("VD852")+"";
+        // 预充时间
+        String preChargeTime = plc.readInt16("VW2048")+"";
+        // 充气时间
+        String chargeTime = plc.readInt16("VW504")+"";
+        // 平衡时间
+        String balanceTime = plc.readInt16("VW506")+"";
+        // 检测时间
+        String detectTime = plc.readInt16("VW508")+"";
+        //循环时间
+        String loopTime = plc.readInt16("VW850")+"";
+
+        QmParam qmParam = new QmParam();
+        qmParam.setLeakVal(leakageValue);
+        qmParam.setLeakRateUnit("kPa");
+        qmParam.setTestPressure(pressure);
+        qmParam.setTestPressureUnit("kPa");
+        qmParam.setTestResult(result);
+        qmParam.setCqDuration(chargeTime);
+        qmParam.setByDuration(balanceTime);
+        qmParam.setCsDuration(detectTime);
+        qmParam.setDeviceType(MesClient.csyq);
+        qmParam.setTestPace(loopTime);
+        return qmParam;
     }
 
+    public static String getResult(S7PLC plc){
+        byte data = plc.readByte("VB503");
+        switch (data) {
+            case 2:
+                return "OK";
+            case 3:
+                return "NG";
+            default:
+                return "未知状态";
+        }
+    }
+
+
 
 
 }

+ 214 - 0
src/com/mes/util/QmParam.java

@@ -0,0 +1,214 @@
+package com.mes.util;
+
+import java.util.Date;
+
+public class QmParam {
+
+    private Date testTime;		// 测试时间
+    private String workNum;		// 工号
+    private String workPosNum;		// 工位号
+    private String componentNum;		// 工件号
+    private String testPressure;		// 测试压
+    private String testPressureUnit;		// 测试压单位
+    private String leakVal;		// 泄露值
+    private String leakValUnit;		// 泄露值单位
+    private String leakRate;//泄漏率
+    private String leakRateUnit;//泄漏率单位
+    private String testResult;		// 测试结果
+    private String testPace;		// 测试节拍
+    private String productCate;		// 产品类型
+    private String oprno;		// 工位号
+    private String deviceType;		// 检测设备
+    private String fid;		// 来源主键id
+    private String cqDuration;
+    private String byDuration;
+    private String csDuration;
+    private String lineSn;
+    private String programNo;
+    private String title;
+    private String remark;
+
+    public Date getTestTime() {
+        return testTime;
+    }
+
+    public void setTestTime(Date testTime) {
+        this.testTime = testTime;
+    }
+
+    public String getWorkNum() {
+        return workNum;
+    }
+
+    public void setWorkNum(String workNum) {
+        this.workNum = workNum;
+    }
+
+    public String getWorkPosNum() {
+        return workPosNum;
+    }
+
+    public void setWorkPosNum(String workPosNum) {
+        this.workPosNum = workPosNum;
+    }
+
+    public String getComponentNum() {
+        return componentNum;
+    }
+
+    public void setComponentNum(String componentNum) {
+        this.componentNum = componentNum;
+    }
+
+    public String getTestPressure() {
+        return testPressure;
+    }
+
+    public void setTestPressure(String testPressure) {
+        this.testPressure = testPressure;
+    }
+
+    public String getTestPressureUnit() {
+        return testPressureUnit;
+    }
+
+    public void setTestPressureUnit(String testPressureUnit) {
+        this.testPressureUnit = testPressureUnit;
+    }
+
+    public String getLeakVal() {
+        return leakVal;
+    }
+
+    public void setLeakVal(String leakVal) {
+        this.leakVal = leakVal;
+    }
+
+    public String getLeakValUnit() {
+        return leakValUnit;
+    }
+
+    public void setLeakValUnit(String leakValUnit) {
+        this.leakValUnit = leakValUnit;
+    }
+
+    public String getLeakRate() {
+        return leakRate;
+    }
+
+    public void setLeakRate(String leakRate) {
+        this.leakRate = leakRate;
+    }
+
+    public String getLeakRateUnit() {
+        return leakRateUnit;
+    }
+
+    public void setLeakRateUnit(String leakRateUnit) {
+        this.leakRateUnit = leakRateUnit;
+    }
+
+    public String getTestResult() {
+        return testResult;
+    }
+
+    public void setTestResult(String testResult) {
+        this.testResult = testResult;
+    }
+
+    public String getTestPace() {
+        return testPace;
+    }
+
+    public void setTestPace(String testPace) {
+        this.testPace = testPace;
+    }
+
+    public String getProductCate() {
+        return productCate;
+    }
+
+    public void setProductCate(String productCate) {
+        this.productCate = productCate;
+    }
+
+    public String getOprno() {
+        return oprno;
+    }
+
+    public void setOprno(String oprno) {
+        this.oprno = oprno;
+    }
+
+    public String getDeviceType() {
+        return deviceType;
+    }
+
+    public void setDeviceType(String deviceType) {
+        this.deviceType = deviceType;
+    }
+
+    public String getFid() {
+        return fid;
+    }
+
+    public void setFid(String fid) {
+        this.fid = fid;
+    }
+
+    public String getCqDuration() {
+        return cqDuration;
+    }
+
+    public void setCqDuration(String cqDuration) {
+        this.cqDuration = cqDuration;
+    }
+
+    public String getByDuration() {
+        return byDuration;
+    }
+
+    public void setByDuration(String byDuration) {
+        this.byDuration = byDuration;
+    }
+
+    public String getCsDuration() {
+        return csDuration;
+    }
+
+    public void setCsDuration(String csDuration) {
+        this.csDuration = csDuration;
+    }
+
+    public String getLineSn() {
+        return lineSn;
+    }
+
+    public void setLineSn(String lineSn) {
+        this.lineSn = lineSn;
+    }
+
+    public String getProgramNo() {
+        return programNo;
+    }
+
+    public void setProgramNo(String programNo) {
+        this.programNo = programNo;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getRemark() {
+        return remark;
+    }
+
+    public void setRemark(String remark) {
+        this.remark = remark;
+    }
+}

+ 2 - 1
src/resources/config/config.properties

@@ -1,4 +1,5 @@
 mes.gw=OP190A
 #mes.server_ip=127.0.0.1
 mes.server_ip=192.168.9.180
-mes.line_sn=HEVXT
+mes.line_sn=HEVXT
+mes.csyq=comso