|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|