IweldCloudUtilTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package com.mes.util;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import java.io.BufferedReader;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.Properties;
  8. public class IweldCloudUtilTest {
  9. private static final long API_INTERVAL_MS = IweldCloudUtil.API_MIN_INTERVAL_MS;
  10. public static void main(String[] args) {
  11. System.out.println("========== IweldCloud 逻辑测试开始 ==========");
  12. try {
  13. Properties pro = loadConfig();
  14. printConfig(pro);
  15. System.out.println();
  16. System.out.println("--- 1. 测试 Login ---");
  17. Boolean loginOk = IweldCloudUtil.login();
  18. System.out.println("Login结果: " + (loginOk ? "成功" : "失败"));
  19. System.out.println("sessionKey: " + mask(IweldCloudUtil.getSessionKey()));
  20. System.out.println();
  21. if (!loginOk) {
  22. System.out.println("Login失败,停止后续测试");
  23. return;
  24. }
  25. sleepBetweenRequests();
  26. System.out.println("--- 2. 测试 getWeldListInfo(设备列表) ---");
  27. String weldListInfo = IweldCloudUtil.fetchWeldListInfo();
  28. if (weldListInfo == null) {
  29. System.out.println("getWeldListInfo结果: 失败");
  30. } else {
  31. System.out.println("getWeldListInfo结果: 成功");
  32. System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(weldListInfo));
  33. printDeviceList(weldListInfo, pro);
  34. }
  35. System.out.println();
  36. sleepBetweenRequests();
  37. System.out.println("--- 3. 测试实时数据接口 ---");
  38. String prodCode = pro.getProperty("iweld.prodCode", "0").trim();
  39. Boolean fetchOk = IweldCloudUtil.fetchRobotRunInfo();
  40. String runInfo = IweldCloudUtil.getRobotRunInfo();
  41. System.out.println("prodCode=" + prodCode + " 拉取结果: " + (fetchOk ? "成功" : "失败"));
  42. if (fetchOk && runInfo != null && !runInfo.isEmpty()) {
  43. System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(runInfo));
  44. } else {
  45. System.out.println("接口状态: 响应为空");
  46. runInfo = tryAlternateRunInfoApi(pro, prodCode);
  47. }
  48. System.out.println("原始数据:");
  49. System.out.println(runInfo);
  50. System.out.println();
  51. if (runInfo != null && !runInfo.isEmpty()) {
  52. try {
  53. JSONObject json = JSONObject.parseObject(runInfo);
  54. System.out.println("格式化JSON:");
  55. System.out.println(json.toJSONString());
  56. } catch (Exception e) {
  57. System.out.println("响应不是有效JSON");
  58. }
  59. }
  60. System.out.println();
  61. System.out.println("--- 4. 测试参数解析(prodCode) ---");
  62. IweldCloudUtil.WelderParams[] welders = IweldCloudUtil.parseWelderParamsFromResponse(runInfo);
  63. printWelderParams("焊机1(prodCode=" + prodCode + ")", welders, 0);
  64. String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim();
  65. if (!prodCode2.isEmpty()) {
  66. sleepBetweenRequests();
  67. System.out.println();
  68. System.out.println("--- 5. 测试参数解析(prodCode2) ---");
  69. String runInfo2 = IweldCloudUtil.fetchRunInfoForProdCode(prodCode2);
  70. System.out.println("prodCode2=" + prodCode2 + " 拉取结果: "
  71. + (runInfo2 != null ? "成功" : "失败"));
  72. if (runInfo2 != null) {
  73. System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(runInfo2));
  74. System.out.println("原始数据:");
  75. System.out.println(runInfo2);
  76. printProgramNameDebug(runInfo2);
  77. }
  78. // 再试焊机实时接口,对比云平台“在线”是否来自另一套状态
  79. sleepBetweenRequests();
  80. String welderUrl = "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getWeldRunInfo";
  81. String runInfo2Welder = IweldCloudUtil.fetchRunInfoForProdCodeWithUrl(prodCode2, welderUrl);
  82. System.out.println("prodCode2 走 getWeldRunInfo: "
  83. + (runInfo2Welder != null ? "成功" : "失败/无效"));
  84. if (runInfo2Welder != null) {
  85. System.out.println("焊机接口原始数据:");
  86. System.out.println(runInfo2Welder);
  87. printProgramNameDebug(runInfo2Welder);
  88. }
  89. IweldCloudUtil.WelderParams[] welders2 = IweldCloudUtil.parseWelderParamsFromResponse(runInfo2);
  90. printWelderParams("焊机2(prodCode2=" + prodCode2 + ")", welders2, 0);
  91. if (welders != null && welders2 != null && welders2[0].hasData()) {
  92. welders[1] = welders2[0];
  93. }
  94. System.out.println();
  95. System.out.println("合并后双焊机参数:");
  96. printWelderParams("焊机1", welders, 0);
  97. printWelderParams("焊机2", welders, 1);
  98. } else {
  99. printWelderParams("焊机2(dataList第2条)", welders, 1);
  100. }
  101. } catch (Exception e) {
  102. System.out.println("测试异常: " + e.getMessage());
  103. e.printStackTrace();
  104. }
  105. System.out.println();
  106. System.out.println("========== IweldCloud 逻辑测试结束 ==========");
  107. }
  108. private static void printConfig(Properties pro) {
  109. System.out.println("accessKey: " + mask(pro.getProperty("iweld.accessKey", "")));
  110. System.out.println("loginUrl: " + pro.getProperty("iweld.login.url"));
  111. System.out.println("device.type: " + pro.getProperty("iweld.device.type", "robot"));
  112. System.out.println("prodCode: " + pro.getProperty("iweld.prodCode", "0"));
  113. System.out.println("prodCode2: " + pro.getProperty("iweld.prodCode2", "(未配置)"));
  114. String runInfoUrl = pro.getProperty("iweld.run.info.url", "").trim();
  115. if (runInfoUrl.isEmpty()) {
  116. runInfoUrl = pro.getProperty("iweld.robot.run.info.url", "(默认getRobotRunInfo)");
  117. }
  118. System.out.println("runInfoUrl: " + runInfoUrl);
  119. }
  120. private static String tryAlternateRunInfoApi(Properties pro, String prodCode) {
  121. String currentType = pro.getProperty("iweld.device.type", "robot").trim();
  122. String alternateType = "robot".equalsIgnoreCase(currentType) ? "welder" : "robot";
  123. System.out.println();
  124. System.out.println("提示: 当前接口拉取失败,尝试备用接口(" + alternateType + ")...");
  125. sleepBetweenRequests();
  126. String alternateUrl = "welder".equalsIgnoreCase(alternateType)
  127. ? "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getWeldRunInfo"
  128. : "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getRobotRunInfo";
  129. String response = IweldCloudUtil.fetchRunInfoForProdCodeWithUrl(prodCode, alternateUrl);
  130. if (response != null) {
  131. System.out.println("备用接口拉取成功,建议将 iweld.device.type 改为 " + alternateType);
  132. System.out.println("或设置 iweld.run.info.url=" + alternateUrl);
  133. System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(response));
  134. return response;
  135. }
  136. System.out.println("备用接口也失败,请检查 prodCode 是否正确、设备是否在线");
  137. return null;
  138. }
  139. private static void printDeviceList(String weldListInfo, Properties pro) {
  140. try {
  141. JSONObject json = JSONObject.parseObject(weldListInfo);
  142. JSONArray dataList = json.getJSONArray("dataList");
  143. if (dataList == null || dataList.isEmpty()) {
  144. System.out.println("设备列表为空");
  145. return;
  146. }
  147. String prodCode = pro.getProperty("iweld.prodCode", "").trim();
  148. String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim();
  149. int totalDevices = 0;
  150. boolean matched = false;
  151. System.out.println("当前配置设备在列表中的类型:");
  152. for (int i = 0; i < dataList.size(); i++) {
  153. JSONObject block = dataList.getJSONObject(i);
  154. JSONArray list = block.getJSONArray("list");
  155. if (list == null) {
  156. continue;
  157. }
  158. totalDevices += list.size();
  159. for (int j = 0; j < list.size(); j++) {
  160. JSONObject device = list.getJSONObject(j);
  161. String code = device.getString("D01");
  162. if (code == null) {
  163. continue;
  164. }
  165. if (code.equals(prodCode) || code.equals(prodCode2)) {
  166. matched = true;
  167. System.out.println(" " + code + " -> 完整字段: " + device.toJSONString());
  168. System.out.println(" D03=" + device.getString("D03")
  169. + ", D04=" + device.getString("D04")
  170. + " (" + deviceTypeName(device.getString("D04")) + ")"
  171. + ", 常见在线字段: D02=" + device.get("D02")
  172. + ", D05=" + device.get("D05")
  173. + ", D06=" + device.get("D06")
  174. + ", D07=" + device.get("D07")
  175. + ", status=" + device.get("status")
  176. + ", online=" + device.get("online")
  177. + ", runStatus=" + device.get("runStatus"));
  178. }
  179. }
  180. }
  181. System.out.println("设备总数: " + totalDevices);
  182. if (!matched) {
  183. System.out.println("未在设备列表中找到 prodCode/prodCode2,请核对配置编码");
  184. System.out.println("账号下全部设备(D01=制造编码, D03=自定义名称, D04=类型):");
  185. for (int i = 0; i < dataList.size(); i++) {
  186. JSONObject block = dataList.getJSONObject(i);
  187. JSONArray list = block.getJSONArray("list");
  188. if (list == null) {
  189. continue;
  190. }
  191. for (int j = 0; j < list.size(); j++) {
  192. JSONObject device = list.getJSONObject(j);
  193. System.out.println(" D01=" + device.getString("D01")
  194. + ", D03=" + device.getString("D03")
  195. + ", D04=" + device.getString("D04")
  196. + " (" + deviceTypeName(device.getString("D04")) + ")");
  197. }
  198. }
  199. }
  200. } catch (Exception e) {
  201. System.out.println("解析设备列表失败: " + e.getMessage());
  202. }
  203. }
  204. private static String deviceTypeName(String d04) {
  205. if ("1".equals(d04)) {
  206. return "焊机";
  207. }
  208. if ("2".equals(d04)) {
  209. return "机器人";
  210. }
  211. if ("3".equals(d04)) {
  212. return "激光";
  213. }
  214. return "未知";
  215. }
  216. private static void printWelderParams(String label, IweldCloudUtil.WelderParams[] welders, int index) {
  217. if (welders == null || welders.length <= index) {
  218. System.out.println(label + ": 无数据");
  219. return;
  220. }
  221. IweldCloudUtil.WelderParams params = welders[index];
  222. if (!params.hasData()) {
  223. System.out.println(label + ": 未解析到电压/电流/送丝速度(请检查接口类型与字段映射)");
  224. return;
  225. }
  226. System.out.println(label + ": " + params);
  227. if (!params.programName.isEmpty()) {
  228. System.out.println(label + " 程序名: " + params.programName);
  229. }
  230. }
  231. /** 打印原始 JSON 中 programName 相关字段,便于对比两台设备差异 */
  232. private static void printProgramNameDebug(String response) {
  233. try {
  234. JSONObject json = JSONObject.parseObject(response);
  235. JSONArray dataList = json.getJSONArray("dataList");
  236. if (dataList == null || dataList.isEmpty()) {
  237. System.out.println("programName调试: dataList 为空");
  238. return;
  239. }
  240. JSONObject first = dataList.getJSONObject(0);
  241. JSONArray nested = first.getJSONArray("list");
  242. JSONObject item = (nested != null && !nested.isEmpty()) ? nested.getJSONObject(0) : first;
  243. System.out.println("programName调试: containsKey=" + item.containsKey("programName")
  244. + ", raw=" + item.get("programName")
  245. + ", runStatue=" + item.get("runStatue")
  246. + ", currentStatus=" + item.get("currentStatus")
  247. + ", positionNam=" + item.get("positionNam")
  248. + ", keys含program=" + keysContaining(item, "program"));
  249. } catch (Exception e) {
  250. System.out.println("programName调试失败: " + e.getMessage());
  251. }
  252. }
  253. private static String keysContaining(JSONObject item, String keyword) {
  254. StringBuilder sb = new StringBuilder();
  255. for (String key : item.keySet()) {
  256. if (key != null && key.toLowerCase().contains(keyword.toLowerCase())) {
  257. if (sb.length() > 0) {
  258. sb.append(", ");
  259. }
  260. sb.append(key).append("=").append(item.get(key));
  261. }
  262. }
  263. return sb.length() == 0 ? "(无)" : sb.toString();
  264. }
  265. private static void sleepBetweenRequests() {
  266. try {
  267. Thread.sleep(API_INTERVAL_MS);
  268. } catch (InterruptedException e) {
  269. Thread.currentThread().interrupt();
  270. }
  271. }
  272. private static Properties loadConfig() throws Exception {
  273. Properties pro = new Properties();
  274. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  275. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  276. pro.load(br);
  277. br.close();
  278. is.close();
  279. return pro;
  280. }
  281. private static String mask(String value) {
  282. if (value == null || value.isEmpty()) {
  283. return "(空)";
  284. }
  285. if (value.length() <= 6) {
  286. return "******";
  287. }
  288. return value.substring(0, 3) + "******" + value.substring(value.length() - 3);
  289. }
  290. }