浏览代码

新增客户端版本更新功能

hou 2 天之前
父节点
当前提交
e1bc7bfaa0

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

@@ -259,6 +259,7 @@ public class DataUtil {
     }
 
     public static String doPost(String httpUrl, String param) {
+        httpUrl = appendSid(httpUrl);
         HttpURLConnection connection = null;
         InputStream is = null;
         OutputStream os = null;
@@ -411,4 +412,20 @@ public class DataUtil {
 
         return  oprno;
     }
+
+    private static String appendSid(String url) {
+        if (url == null || url.contains("__sid=") || url.contains("/login")) {
+            return url;
+        }
+        try {
+            String sid = MesClient.sessionid;
+            if (sid == null || sid.length() == 0) {
+                return url;
+            }
+            return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
+        } catch (Exception e) {
+            return url;
+        }
+    }
+
 }

+ 5 - 2
src/com/mes/ui/LoginFarme.java

@@ -3,6 +3,7 @@ package com.mes.ui;
 import com.alibaba.fastjson2.JSONObject;
 import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
+import com.mes.util.ClientUpgrade;
 import com.mes.util.Base64Utils;
 import com.mes.util.HttpUtils;
 import org.slf4j.Logger;
@@ -49,11 +50,11 @@ public class LoginFarme extends JFrame {
         userPasswordLabel.setBounds(300, 150, 120, 40);
         userPasswordLabel.setFont(new java.awt.Font("Dialog",   1,   16));
         userNameTxt = new JTextField(20);
-        userNameTxt.setText("system");
+        userNameTxt.setText("");
         userNameTxt.setBounds(400, 105, 150, 30);
         userPasswordTxt = new JPasswordField(20);
         userPasswordTxt.setBounds(400, 155, 150, 30);
-        userPasswordTxt.setText("Aa111111");
+        userPasswordTxt.setText("");
         loginButton.setFont(new java.awt.Font("Dialog",   1,   16));
         loginButton.setBounds(300, 200, 255, 40);
         loginButton.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
@@ -95,6 +96,7 @@ public class LoginFarme extends JFrame {
 
     //登录
     public static void login() {
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         String user_str = userNameTxt.getText().toString();
         String password_str = userPasswordTxt.getText().toString();
         if(user_str.equalsIgnoreCase("")||password_str.equalsIgnoreCase("")) {
@@ -124,6 +126,7 @@ public class LoginFarme extends JFrame {
     }
     //扫码登录
     public static void scanLogin() {
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         //userNameTxt.setText("");
         //userPasswordTxt.setText("");
         String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码");

+ 22 - 1
src/com/mes/ui/MesClient.java

@@ -8,6 +8,8 @@ import com.google.zxing.common.BitMatrix;
 import com.google.zxing.qrcode.QRCodeWriter;
 import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
 import com.mes.component.*;
+import com.mes.util.ClientVersionUtils;
+import com.mes.util.ClientUpgrade;
 import com.mes.util.DateLocalUtils;
 import com.mes.util.JdbcUtils;
 import com.mes.util.TCPClient;
@@ -40,7 +42,8 @@ public class MesClient extends JFrame {
     public static String mes_gw = ""; // 工位号
     public static String mes_gw_des = ""; // 工位名称
     public static String mes_server_ip = ""; // 服务器IP地址
-    public static String mes_line_sn = ""; // 产线编号
+    public static String mes_line_sn = "";
+    public static String client_version = "0.0.0"; // 打包发布版本,需和后台 JAR 管理版本号对齐 // 产线编号
     public static String mes_line_pos = "";
     public static String mes_plc_ip = "192.168.1.100";
     public static int mes_plc_port = 502;
@@ -112,12 +115,21 @@ public class MesClient extends JFrame {
     public static CncStationRecordPanel cncStationRecordPanel;
 
     public static void main(String[] args) {
+        if (ClientUpgrade.handleInstallArgs(args)) {
+            return;
+        }
         EventQueue.invokeLater(new Runnable() {
             @Override
             public void run() {
                 try{
                     //读文件配置
                     readProperty();
+                    ClientUpgrade.setStationOprno("OP090A");
+                    if (ClientUpgrade.recoverIncompleteInstall()) {
+                        return;
+                    }
+                    ClientUpgrade.cleanupInstallLeftovers();
+                    ClientUpgrade.checkIfConfigured(null);
 
                     JdbcUtils.getConn();
 
@@ -168,12 +180,21 @@ public class MesClient extends JFrame {
     }
 
     //读配置文件
+
+    private static String parseClientVersion(String value) {
+        if (value == null || value.trim().isEmpty()) {
+            return "0.0.0";
+        }
+        return ClientVersionUtils.normalize(value.trim());
+    }
+
     private static void readProperty() throws IOException{
         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);
+        client_version = parseClientVersion(pro.getProperty("mes.client.version"));
         mes_gw =  pro.getProperty("mes.gw");
         mes_gw_des = pro.getProperty("mes.gw_des");
         mes_server_ip = pro.getProperty("mes.server_ip");

文件差异内容过多而无法显示
+ 1091 - 0
src/com/mes/util/ClientUpgrade.java


+ 75 - 0
src/com/mes/util/ClientVersionUtils.java

@@ -0,0 +1,75 @@
+package com.mes.util;
+
+/**
+ * 客户端版本号比较,格式 0.0.0 / 0.0.1。
+ * 没有点的旧数字(如 1)按 0.0.1 处理。
+ */
+public final class ClientVersionUtils {
+
+    private ClientVersionUtils() {
+    }
+
+    public static int compare(String left, String right) {
+        int[] a = parse(left);
+        int[] b = parse(right);
+        int n = Math.max(a.length, b.length);
+        for (int i = 0; i < n; i++) {
+            int va = i < a.length ? a[i] : 0;
+            int vb = i < b.length ? b[i] : 0;
+            if (va != vb) {
+                return va < vb ? -1 : 1;
+            }
+        }
+        return 0;
+    }
+
+    public static boolean isNewer(String latest, String current) {
+        return compare(latest, current) > 0;
+    }
+
+    public static String max(String left, String right) {
+        if (isBlank(left)) {
+            return normalize(right);
+        }
+        if (isBlank(right)) {
+            return normalize(left);
+        }
+        return compare(left, right) >= 0 ? normalize(left) : normalize(right);
+    }
+
+    public static String normalize(String version) {
+        if (isBlank(version)) {
+            return "0.0.0";
+        }
+        int[] parts = parse(version);
+        return parts[0] + "." + parts[1] + "." + parts[2];
+    }
+
+    private static int[] parse(String version) {
+        int[] result = new int[] {0, 0, 0};
+        if (isBlank(version)) {
+            return result;
+        }
+        String[] parts = version.trim().split("\\.");
+        if (parts.length == 1) {
+            result[2] = parseInt(parts[0]);
+            return result;
+        }
+        for (int i = 0; i < parts.length && i < 3; i++) {
+            result[i] = parseInt(parts[i]);
+        }
+        return result;
+    }
+
+    private static int parseInt(String value) {
+        try {
+            return Integer.parseInt(value.trim());
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    private static boolean isBlank(String value) {
+        return value == null || value.trim().length() == 0;
+    }
+}

+ 21 - 0
src/com/mes/util/HttpUtils.java

@@ -1,5 +1,7 @@
 package com.mes.util;
 
+import com.mes.ui.MesClient;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -18,6 +20,7 @@ public class HttpUtils {
 	 */
     //http post请求
     public static String sendRequest(String urlParam) {
+        urlParam = appendSid(urlParam);
 		String requestType = "POST";
         //根据接收内容返回数据结果
     	String ret = "";
@@ -71,6 +74,7 @@ public class HttpUtils {
 
 
     public static String sendPostRequest(String urlParam, String params) {
+        urlParam = appendSid(urlParam);
         String requestType = "POST";
         //根据接收内容返回数据结果
         String ret = "";
@@ -129,6 +133,7 @@ public class HttpUtils {
     }
 
     public static String sendPostRequestJson(String apiUrl, String jsonData) throws IOException {
+        apiUrl = appendSid(apiUrl);
         URL url = new URL(apiUrl);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("POST");
@@ -161,4 +166,20 @@ public class HttpUtils {
 
 	
 
+
+    private static String appendSid(String url) {
+        if (url == null || url.contains("__sid=") || url.contains("/login")) {
+            return url;
+        }
+        try {
+            String sid = MesClient.sessionid;
+            if (sid == null || sid.length() == 0) {
+                return url;
+            }
+            return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
+        } catch (Exception e) {
+            return url;
+        }
+    }
+
 }

+ 6 - 3
src/resources/config/config.properties

@@ -1,5 +1,5 @@
-#mes.server_ip=127.0.0.1
-mes.server_ip=192.168.16.99
+mes.server_ip=127.0.0.1
+#mes.server_ip=192.168.16.99
 mes.line_sn=XT
 mes.gw=OP090
 mes.line_pos=west
@@ -8,6 +8,9 @@ mes.line_pos=west
 mes.plc_ip=192.168.1.88
 mes.plc_port=502
 # 0=连接PLC  1=本地调试(不读写PLC)
-mes.is_local=0
+mes.is_local=1
 # 上料区质量校验工位号
 mes.feeding_oprno=OP090A
+# 打包发布时改成和后台「JAR管理」里填的版本号一致(或更大),格式 0.0.0。
+# 每次打包切记去后台查看对应子工位最新版本,打包时版本号+1
+mes.client.version=0.0.0