sunxianglong 11 timmar sedan
förälder
incheckning
507399b165

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

@@ -179,6 +179,8 @@ public class LoginFarme extends JFrame {
                     MesClient.initTcpConnection();
                     //启动timer心跳包
                     MesClient.startHeartBeatTimer();
+                    MesClient.startIweldLoginTimer();
+                    MesClient.startIweldRobotRunInfoTimer();
 
                     //1操作工人,2管理员
                     //登录成功

+ 34 - 0
src/com/mes/ui/MesClient.java

@@ -13,6 +13,7 @@ import com.mes.component.MyDialog;
 import com.mes.netty.NettyClient;
 import com.mes.util.DateLocalUtils;
 import com.mes.util.HttpUtils;
+import com.mes.util.IweldCloudUtil;
 import com.mes.util.JdbcUtils;
 import javafx.embed.swing.JFXPanel;
 import org.slf4j.Logger;
@@ -323,6 +324,8 @@ public class MesClient extends JFrame {
     //閸氼垰濮╄箛鍐儲閸栧懐鈻兼惔锟�
     public static java.util.Timer heartBeatTimer;
     public static java.util.Timer heartBeatIconTimer;
+    public static java.util.Timer iweldLoginTimer;
+    public static java.util.Timer iweldRobotRunInfoTimer;
     public static boolean iconREDFlag = true;
     public static JTextField param1;
     public static JTextField param2;
@@ -387,6 +390,31 @@ public class MesClient extends JFrame {
         }, 100,mes_heart_icon_cycle*1000);
     }
 
+    public static void startIweldLoginTimer() {
+        if(iweldLoginTimer != null) {
+            iweldLoginTimer.cancel();
+        }
+        iweldLoginTimer = new Timer();
+        long twoHoursMs = 2 * 60 * 60 * 1000L;
+        iweldLoginTimer.schedule(new TimerTask() {
+            public void run() {
+                IweldCloudUtil.login();
+            }
+        }, 0, twoHoursMs);
+    }
+
+    public static void startIweldRobotRunInfoTimer() {
+        if(iweldRobotRunInfoTimer != null) {
+            iweldRobotRunInfoTimer.cancel();
+        }
+        iweldRobotRunInfoTimer = new Timer();
+        iweldRobotRunInfoTimer.schedule(new TimerTask() {
+            public void run() {
+                IweldCloudUtil.fetchRobotRunInfo();
+            }
+        }, 1000, 1000);
+    }
+
     //鐠佸墽鐤唗cp鏉╃偞甯撮悩鑸碉拷浣规▔缁�锟�
     public static void setTcpStatus() {
         if(tcp_connect_flag) {
@@ -538,6 +566,12 @@ public class MesClient extends JFrame {
         if(heartBeatIconTimer!=null) {
             heartBeatIconTimer.cancel();
         }
+        if(iweldLoginTimer!=null) {
+            iweldLoginTimer.cancel();
+        }
+        if(iweldRobotRunInfoTimer!=null) {
+            iweldRobotRunInfoTimer.cancel();
+        }
         tcp_connect_flag = false;
         connect_request_flag = false;
     }

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

@@ -154,6 +154,39 @@ public class HttpUtils {
         return response.toString();
     }
 
+    public static String sendGetRequestWithHeader(String apiUrl, String headerKey, String headerValue) throws IOException {
+        URL url = new URL(apiUrl);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.setRequestMethod("GET");
+        conn.setRequestProperty(headerKey, headerValue);
+        conn.setDoInput(true);
+        conn.setUseCaches(false);
+
+        int responseCode = conn.getResponseCode();
+        InputStream inputStream = responseCode >= 200 && responseCode < 300
+                ? conn.getInputStream()
+                : conn.getErrorStream();
+        if (inputStream == null) {
+            conn.disconnect();
+            return "false";
+        }
+
+        StringBuilder response = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"))) {
+            String responseLine;
+            while ((responseLine = br.readLine()) != null) {
+                response.append(responseLine.trim());
+            }
+        }
+
+        conn.disconnect();
+        if (responseCode < 200 || responseCode >= 300) {
+            return "false";
+        }
+        return response.toString();
+    }
+
+
 	
 
 }

+ 100 - 0
src/com/mes/util/IweldCloudUtil.java

@@ -0,0 +1,100 @@
+package com.mes.util;
+
+import com.alibaba.fastjson2.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Properties;
+
+public class IweldCloudUtil {
+    public static final Logger log = LoggerFactory.getLogger(IweldCloudUtil.class);
+
+    private static volatile String sessionKey = "";
+    private static volatile String robotRunInfo = "";
+
+    public static String getSessionKey() {
+        return sessionKey;
+    }
+
+    public static String getRobotRunInfo() {
+        return robotRunInfo;
+    }
+
+    public static Boolean login() {
+        try {
+            Properties pro = loadConfig();
+            String accessKey = pro.getProperty("iweld.accessKey", "").trim();
+            String loginUrl = pro.getProperty("iweld.login.url", "https://api.iweldcloud.com/ApiServer/Login").trim();
+            if (accessKey.isEmpty()) {
+                log.warn("IweldCloud login skipped: iweld.accessKey is empty");
+                return false;
+            }
+
+            JSONObject body = new JSONObject();
+            body.put("accessKey", accessKey);
+            String response = HttpUtils.sendPostRequestJson(loginUrl, body.toJSONString());
+            if (response == null || response.isEmpty() || "false".equalsIgnoreCase(response)) {
+                log.error("IweldCloud login failed: empty response");
+                return false;
+            }
+
+            JSONObject json = JSONObject.parseObject(response);
+            if (json.getIntValue("execution") != 0) {
+                log.error("IweldCloud login failed: execution={}", json.getIntValue("execution"));
+                return false;
+            }
+
+            JSONObject result = json.getJSONObject("result");
+            if (result == null || result.getIntValue("status") != 1) {
+                log.error("IweldCloud login failed: invalid result status");
+                return false;
+            }
+
+            sessionKey = result.getString("sessionKey");
+            log.info("IweldCloud login success, sessionKey={}", sessionKey);
+            return true;
+        } catch (Exception e) {
+            log.error("IweldCloud login error", e);
+            return false;
+        }
+    }
+
+    public static Boolean fetchRobotRunInfo() {
+        try {
+            if (sessionKey == null || sessionKey.isEmpty()) {
+                return false;
+            }
+
+            Properties pro = loadConfig();
+            String prodCode = pro.getProperty("iweld.prodCode", "0").trim();
+            String baseUrl = pro.getProperty("iweld.robot.run.info.url",
+                    "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getRobotRunInfo").trim();
+            String url = baseUrl + "?prodCode=" + prodCode;
+            String response = HttpUtils.sendGetRequestWithHeader(url, "app-token", sessionKey);
+            if (response == null || response.isEmpty() || "false".equalsIgnoreCase(response)) {
+                log.warn("IweldCloud getRobotRunInfo failed: empty response");
+                return false;
+            }
+
+            robotRunInfo = response;
+            System.out.println("IweldCloud getRobotRunInfo: " + response);
+            return true;
+        } catch (Exception e) {
+            log.error("IweldCloud getRobotRunInfo error", e);
+            return false;
+        }
+    }
+
+    private static Properties loadConfig() throws Exception {
+        Properties pro = new Properties();
+        InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
+        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+        pro.load(br);
+        br.close();
+        is.close();
+        return pro;
+    }
+}

+ 76 - 0
src/com/mes/util/IweldCloudUtilTest.java

@@ -0,0 +1,76 @@
+package com.mes.util;
+
+import com.alibaba.fastjson2.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Properties;
+
+public class IweldCloudUtilTest {
+
+    public static void main(String[] args) {
+        System.out.println("========== IweldCloud 逻辑测试开始 ==========");
+
+        try {
+            Properties pro = loadConfig();
+            System.out.println("accessKey: " + mask(pro.getProperty("iweld.accessKey", "")));
+            System.out.println("loginUrl: " + pro.getProperty("iweld.login.url"));
+            System.out.println("prodCode: " + pro.getProperty("iweld.prodCode", "0"));
+            System.out.println("robotRunInfoUrl: " + pro.getProperty("iweld.robot.run.info.url"));
+            System.out.println();
+
+            System.out.println("--- 1. 测试 Login ---");
+            Boolean loginOk = IweldCloudUtil.login();
+            System.out.println("Login结果: " + (loginOk ? "成功" : "失败"));
+            System.out.println("sessionKey: " + IweldCloudUtil.getSessionKey());
+            System.out.println();
+
+            if (!loginOk) {
+                System.out.println("Login失败,停止后续测试");
+                return;
+            }
+
+            System.out.println("--- 2. 测试 getRobotRunInfo ---");
+            Boolean fetchOk = IweldCloudUtil.fetchRobotRunInfo();
+            System.out.println("getRobotRunInfo结果: " + (fetchOk ? "成功" : "失败"));
+            String robotInfo = IweldCloudUtil.getRobotRunInfo();
+            System.out.println("robotRunInfo原始数据:");
+            System.out.println(robotInfo);
+            System.out.println();
+
+            try {
+                JSONObject json = JSONObject.parseObject(robotInfo);
+                System.out.println("robotRunInfo格式化输出:");
+                System.out.println(json.toJSONString());
+            } catch (Exception e) {
+                System.out.println("robotRunInfo不是有效JSON,已输出原始字符串");
+            }
+        } catch (Exception e) {
+            System.out.println("测试异常: " + e.getMessage());
+            e.printStackTrace();
+        }
+
+        System.out.println("========== IweldCloud 逻辑测试结束 ==========");
+    }
+
+    private static Properties loadConfig() throws Exception {
+        Properties pro = new Properties();
+        InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
+        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+        pro.load(br);
+        br.close();
+        is.close();
+        return pro;
+    }
+
+    private static String mask(String value) {
+        if (value == null || value.isEmpty()) {
+            return "(空)";
+        }
+        if (value.length() <= 6) {
+            return "******";
+        }
+        return value.substring(0, 3) + "******" + value.substring(value.length() - 3);
+    }
+}

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

@@ -5,3 +5,9 @@ mes.tcp_port=3000
 mes.heart_beat_cycle=60
 mes.line_sn=XT
 mes.gwflag=A
+
+# IweldCloud Login
+iweld.accessKey=19353:BN15MTY2NA==08X
+iweld.login.url=https://api.iweldcloud.com/ApiServer/Login
+iweld.prodCode=D262650085
+iweld.robot.run.info.url=https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getRobotRunInfo