package com.mes.util; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Properties; public class IweldCloudUtilTest { private static final long API_INTERVAL_MS = IweldCloudUtil.API_MIN_INTERVAL_MS; public static void main(String[] args) { System.out.println("========== IweldCloud 逻辑测试开始 =========="); try { Properties pro = loadConfig(); printConfig(pro); System.out.println(); System.out.println("--- 1. 测试 Login ---"); Boolean loginOk = IweldCloudUtil.login(); System.out.println("Login结果: " + (loginOk ? "成功" : "失败")); System.out.println("sessionKey: " + mask(IweldCloudUtil.getSessionKey())); System.out.println(); if (!loginOk) { System.out.println("Login失败,停止后续测试"); return; } sleepBetweenRequests(); System.out.println("--- 2. 测试 getWeldListInfo(设备列表) ---"); String weldListInfo = IweldCloudUtil.fetchWeldListInfo(); if (weldListInfo == null) { System.out.println("getWeldListInfo结果: 失败"); } else { System.out.println("getWeldListInfo结果: 成功"); System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(weldListInfo)); printDeviceList(weldListInfo, pro); } System.out.println(); sleepBetweenRequests(); System.out.println("--- 3. 测试实时数据接口 ---"); String prodCode = pro.getProperty("iweld.prodCode", "0").trim(); Boolean fetchOk = IweldCloudUtil.fetchRobotRunInfo(); String runInfo = IweldCloudUtil.getRobotRunInfo(); System.out.println("prodCode=" + prodCode + " 拉取结果: " + (fetchOk ? "成功" : "失败")); if (fetchOk && runInfo != null && !runInfo.isEmpty()) { System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(runInfo)); } else { System.out.println("接口状态: 响应为空"); runInfo = tryAlternateRunInfoApi(pro, prodCode); } System.out.println("原始数据:"); System.out.println(runInfo); System.out.println(); if (runInfo != null && !runInfo.isEmpty()) { try { JSONObject json = JSONObject.parseObject(runInfo); System.out.println("格式化JSON:"); System.out.println(json.toJSONString()); } catch (Exception e) { System.out.println("响应不是有效JSON"); } } System.out.println(); System.out.println("--- 4. 测试参数解析(prodCode) ---"); IweldCloudUtil.WelderParams[] welders = IweldCloudUtil.parseWelderParamsFromResponse(runInfo); printWelderParams("焊机1(prodCode=" + prodCode + ")", welders, 0); String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim(); if (!prodCode2.isEmpty()) { sleepBetweenRequests(); System.out.println(); System.out.println("--- 5. 测试参数解析(prodCode2) ---"); String runInfo2 = IweldCloudUtil.fetchRunInfoForProdCode(prodCode2); System.out.println("prodCode2=" + prodCode2 + " 拉取结果: " + (runInfo2 != null ? "成功" : "失败")); if (runInfo2 != null) { System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(runInfo2)); System.out.println("原始数据:"); System.out.println(runInfo2); printProgramNameDebug(runInfo2); } // 再试焊机实时接口,对比云平台“在线”是否来自另一套状态 sleepBetweenRequests(); String welderUrl = "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getWeldRunInfo"; String runInfo2Welder = IweldCloudUtil.fetchRunInfoForProdCodeWithUrl(prodCode2, welderUrl); System.out.println("prodCode2 走 getWeldRunInfo: " + (runInfo2Welder != null ? "成功" : "失败/无效")); if (runInfo2Welder != null) { System.out.println("焊机接口原始数据:"); System.out.println(runInfo2Welder); printProgramNameDebug(runInfo2Welder); } IweldCloudUtil.WelderParams[] welders2 = IweldCloudUtil.parseWelderParamsFromResponse(runInfo2); printWelderParams("焊机2(prodCode2=" + prodCode2 + ")", welders2, 0); if (welders != null && welders2 != null && welders2[0].hasData()) { welders[1] = welders2[0]; } System.out.println(); System.out.println("合并后双焊机参数:"); printWelderParams("焊机1", welders, 0); printWelderParams("焊机2", welders, 1); } else { printWelderParams("焊机2(dataList第2条)", welders, 1); } } catch (Exception e) { System.out.println("测试异常: " + e.getMessage()); e.printStackTrace(); } System.out.println(); System.out.println("========== IweldCloud 逻辑测试结束 =========="); } private static void printConfig(Properties pro) { System.out.println("accessKey: " + mask(pro.getProperty("iweld.accessKey", ""))); System.out.println("loginUrl: " + pro.getProperty("iweld.login.url")); System.out.println("device.type: " + pro.getProperty("iweld.device.type", "robot")); System.out.println("prodCode: " + pro.getProperty("iweld.prodCode", "0")); System.out.println("prodCode2: " + pro.getProperty("iweld.prodCode2", "(未配置)")); String runInfoUrl = pro.getProperty("iweld.run.info.url", "").trim(); if (runInfoUrl.isEmpty()) { runInfoUrl = pro.getProperty("iweld.robot.run.info.url", "(默认getRobotRunInfo)"); } System.out.println("runInfoUrl: " + runInfoUrl); } private static String tryAlternateRunInfoApi(Properties pro, String prodCode) { String currentType = pro.getProperty("iweld.device.type", "robot").trim(); String alternateType = "robot".equalsIgnoreCase(currentType) ? "welder" : "robot"; System.out.println(); System.out.println("提示: 当前接口拉取失败,尝试备用接口(" + alternateType + ")..."); sleepBetweenRequests(); String alternateUrl = "welder".equalsIgnoreCase(alternateType) ? "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getWeldRunInfo" : "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getRobotRunInfo"; String response = IweldCloudUtil.fetchRunInfoForProdCodeWithUrl(prodCode, alternateUrl); if (response != null) { System.out.println("备用接口拉取成功,建议将 iweld.device.type 改为 " + alternateType); System.out.println("或设置 iweld.run.info.url=" + alternateUrl); System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(response)); return response; } System.out.println("备用接口也失败,请检查 prodCode 是否正确、设备是否在线"); return null; } private static void printDeviceList(String weldListInfo, Properties pro) { try { JSONObject json = JSONObject.parseObject(weldListInfo); JSONArray dataList = json.getJSONArray("dataList"); if (dataList == null || dataList.isEmpty()) { System.out.println("设备列表为空"); return; } String prodCode = pro.getProperty("iweld.prodCode", "").trim(); String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim(); int totalDevices = 0; boolean matched = false; System.out.println("当前配置设备在列表中的类型:"); for (int i = 0; i < dataList.size(); i++) { JSONObject block = dataList.getJSONObject(i); JSONArray list = block.getJSONArray("list"); if (list == null) { continue; } totalDevices += list.size(); for (int j = 0; j < list.size(); j++) { JSONObject device = list.getJSONObject(j); String code = device.getString("D01"); if (code == null) { continue; } if (code.equals(prodCode) || code.equals(prodCode2)) { matched = true; System.out.println(" " + code + " -> 完整字段: " + device.toJSONString()); System.out.println(" D03=" + device.getString("D03") + ", D04=" + device.getString("D04") + " (" + deviceTypeName(device.getString("D04")) + ")" + ", 常见在线字段: D02=" + device.get("D02") + ", D05=" + device.get("D05") + ", D06=" + device.get("D06") + ", D07=" + device.get("D07") + ", status=" + device.get("status") + ", online=" + device.get("online") + ", runStatus=" + device.get("runStatus")); } } } System.out.println("设备总数: " + totalDevices); if (!matched) { System.out.println("未在设备列表中找到 prodCode/prodCode2,请核对配置编码"); System.out.println("账号下全部设备(D01=制造编码, D03=自定义名称, D04=类型):"); for (int i = 0; i < dataList.size(); i++) { JSONObject block = dataList.getJSONObject(i); JSONArray list = block.getJSONArray("list"); if (list == null) { continue; } for (int j = 0; j < list.size(); j++) { JSONObject device = list.getJSONObject(j); System.out.println(" D01=" + device.getString("D01") + ", D03=" + device.getString("D03") + ", D04=" + device.getString("D04") + " (" + deviceTypeName(device.getString("D04")) + ")"); } } } } catch (Exception e) { System.out.println("解析设备列表失败: " + e.getMessage()); } } private static String deviceTypeName(String d04) { if ("1".equals(d04)) { return "焊机"; } if ("2".equals(d04)) { return "机器人"; } if ("3".equals(d04)) { return "激光"; } return "未知"; } private static void printWelderParams(String label, IweldCloudUtil.WelderParams[] welders, int index) { if (welders == null || welders.length <= index) { System.out.println(label + ": 无数据"); return; } IweldCloudUtil.WelderParams params = welders[index]; if (!params.hasData()) { System.out.println(label + ": 未解析到电压/电流/送丝速度(请检查接口类型与字段映射)"); return; } System.out.println(label + ": " + params); if (!params.programName.isEmpty()) { System.out.println(label + " 程序名: " + params.programName); } } /** 打印原始 JSON 中 programName 相关字段,便于对比两台设备差异 */ private static void printProgramNameDebug(String response) { try { JSONObject json = JSONObject.parseObject(response); JSONArray dataList = json.getJSONArray("dataList"); if (dataList == null || dataList.isEmpty()) { System.out.println("programName调试: dataList 为空"); return; } JSONObject first = dataList.getJSONObject(0); JSONArray nested = first.getJSONArray("list"); JSONObject item = (nested != null && !nested.isEmpty()) ? nested.getJSONObject(0) : first; System.out.println("programName调试: containsKey=" + item.containsKey("programName") + ", raw=" + item.get("programName") + ", runStatue=" + item.get("runStatue") + ", currentStatus=" + item.get("currentStatus") + ", positionNam=" + item.get("positionNam") + ", keys含program=" + keysContaining(item, "program")); } catch (Exception e) { System.out.println("programName调试失败: " + e.getMessage()); } } private static String keysContaining(JSONObject item, String keyword) { StringBuilder sb = new StringBuilder(); for (String key : item.keySet()) { if (key != null && key.toLowerCase().contains(keyword.toLowerCase())) { if (sb.length() > 0) { sb.append(", "); } sb.append(key).append("=").append(item.get(key)); } } return sb.length() == 0 ? "(无)" : sb.toString(); } private static void sleepBetweenRequests() { try { Thread.sleep(API_INTERVAL_MS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } 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); } }