|
|
@@ -6,11 +6,19 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
public class IweldCloudUtilTest {
|
|
|
|
|
|
private static final long API_INTERVAL_MS = IweldCloudUtil.API_MIN_INTERVAL_MS;
|
|
|
+ /** 说明书:prodCode=0 返回全部设备 */
|
|
|
+ private static final String ALL_DEVICES_PROD_CODE = "0";
|
|
|
+ private static final String ROBOT_RUN_INFO_URL =
|
|
|
+ "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getRobotRunInfo";
|
|
|
+ private static final String WELDER_RUN_INFO_URL =
|
|
|
+ "https://api.iweldcloud.com/ApiServer/rest/WeldWebService/getWeldRunInfo";
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
System.out.println("========== IweldCloud 逻辑测试开始 ==========");
|
|
|
@@ -35,100 +43,148 @@ public class IweldCloudUtilTest {
|
|
|
|
|
|
System.out.println("--- 2. 测试 getWeldListInfo(设备列表) ---");
|
|
|
String weldListInfo = IweldCloudUtil.fetchWeldListInfo();
|
|
|
+ Map<String, JSONObject> deviceIndex = new LinkedHashMap<String, JSONObject>();
|
|
|
if (weldListInfo == null) {
|
|
|
System.out.println("getWeldListInfo结果: 失败");
|
|
|
} else {
|
|
|
System.out.println("getWeldListInfo结果: 成功");
|
|
|
System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(weldListInfo));
|
|
|
- printDeviceList(weldListInfo, pro);
|
|
|
+ deviceIndex = 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();
|
|
|
+ System.out.println("--- 3. 机器人实时数据 getRobotRunInfo(prodCode=0 全部设备) ---");
|
|
|
+ String robotRunInfo = fetchAllRunInfo(ROBOT_RUN_INFO_URL);
|
|
|
+ printAllRunInfo(robotRunInfo, deviceIndex, "robot");
|
|
|
|
|
|
- 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");
|
|
|
- }
|
|
|
- }
|
|
|
+ sleepBetweenRequests();
|
|
|
+
|
|
|
+ System.out.println("--- 4. 焊机实时数据 getWeldRunInfo(prodCode=0 全部设备) ---");
|
|
|
+ String welderRunInfo = fetchAllRunInfo(WELDER_RUN_INFO_URL);
|
|
|
+ printAllRunInfo(welderRunInfo, deviceIndex, "welder");
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("测试异常: " + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
+ System.out.println("========== IweldCloud 逻辑测试结束 ==========");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String fetchAllRunInfo(String baseUrl) {
|
|
|
+ String response = IweldCloudUtil.fetchRunInfoForProdCodeWithUrl(ALL_DEVICES_PROD_CODE, baseUrl);
|
|
|
+ System.out.println("prodCode=" + ALL_DEVICES_PROD_CODE + " 拉取结果: "
|
|
|
+ + (response != null ? "成功" : "失败"));
|
|
|
+ if (response == null) {
|
|
|
+ System.out.println("接口状态: 响应为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ System.out.println("接口状态: " + IweldCloudUtil.describeApiStatus(response));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void printAllRunInfo(String runInfo, Map<String, JSONObject> deviceIndex, String apiType) {
|
|
|
+ if (runInfo == null || runInfo.isEmpty()) {
|
|
|
+ System.out.println("无实时数据");
|
|
|
System.out.println();
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- System.out.println("--- 4. 测试参数解析(prodCode) ---");
|
|
|
- IweldCloudUtil.WelderParams[] welders = IweldCloudUtil.parseWelderParamsFromResponse(runInfo);
|
|
|
- printWelderParams("焊机1(prodCode=" + prodCode + ")", welders, 0);
|
|
|
+ System.out.println("原始数据:");
|
|
|
+ System.out.println(runInfo);
|
|
|
+ System.out.println();
|
|
|
|
|
|
- String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim();
|
|
|
- if (!prodCode2.isEmpty()) {
|
|
|
- sleepBetweenRequests();
|
|
|
+ try {
|
|
|
+ JSONObject json = JSONObject.parseObject(runInfo);
|
|
|
+ System.out.println("格式化JSON:");
|
|
|
+ System.out.println(json.toJSONString());
|
|
|
+ System.out.println();
|
|
|
+
|
|
|
+ JSONArray items = flattenDataItems(json.getJSONArray("dataList"));
|
|
|
+ System.out.println("实时记录条数: " + items.size());
|
|
|
+ for (int i = 0; i < items.size(); i++) {
|
|
|
+ JSONObject item = items.getJSONObject(i);
|
|
|
+ String code = readDeviceCode(item);
|
|
|
+ JSONObject listDevice = deviceIndex.get(code);
|
|
|
+ String name = listDevice != null ? listDevice.getString("D03") : "";
|
|
|
+ String type = listDevice != null ? deviceTypeName(listDevice.getString("D04")) : apiType;
|
|
|
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);
|
|
|
+ System.out.println("[" + (i + 1) + "] 编码=" + (code.isEmpty() ? "(无)" : code)
|
|
|
+ + ", 名称=" + (name == null || name.isEmpty() ? "(无)" : name)
|
|
|
+ + ", 类型=" + type);
|
|
|
+ printItemFields(item);
|
|
|
+ IweldCloudUtil.WelderParams params = parseSingleItem(item);
|
|
|
+ if (params != null && params.hasData()) {
|
|
|
+ System.out.println(" 解析参数: " + params);
|
|
|
}
|
|
|
- IweldCloudUtil.WelderParams[] welders2 = IweldCloudUtil.parseWelderParamsFromResponse(runInfo2);
|
|
|
- printWelderParams("焊机2(prodCode2=" + prodCode2 + ")", welders2, 0);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("解析实时数据失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
|
|
|
- if (welders != null && welders2 != null && welders2[0].hasData()) {
|
|
|
- welders[1] = welders2[0];
|
|
|
+ private static JSONArray flattenDataItems(JSONArray dataList) {
|
|
|
+ JSONArray items = new JSONArray();
|
|
|
+ if (dataList == null) {
|
|
|
+ return items;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < dataList.size(); i++) {
|
|
|
+ JSONObject wrapper = dataList.getJSONObject(i);
|
|
|
+ JSONArray nested = wrapper.getJSONArray("list");
|
|
|
+ if (nested != null && !nested.isEmpty()) {
|
|
|
+ for (int j = 0; j < nested.size(); j++) {
|
|
|
+ items.add(nested.getJSONObject(j));
|
|
|
}
|
|
|
- System.out.println();
|
|
|
- System.out.println("合并后双焊机参数:");
|
|
|
- printWelderParams("焊机1", welders, 0);
|
|
|
- printWelderParams("焊机2", welders, 1);
|
|
|
} else {
|
|
|
- printWelderParams("焊机2(dataList第2条)", welders, 1);
|
|
|
+ items.add(wrapper);
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("测试异常: " + e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
+ return items;
|
|
|
+ }
|
|
|
|
|
|
- System.out.println();
|
|
|
- System.out.println("========== IweldCloud 逻辑测试结束 ==========");
|
|
|
+ private static String readDeviceCode(JSONObject item) {
|
|
|
+ if (item == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String code = item.getString("prodCode");
|
|
|
+ if (code != null && !code.isEmpty()) {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ code = item.getString("D01");
|
|
|
+ return code != null ? code : "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void printItemFields(JSONObject item) {
|
|
|
+ if (item == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String key : item.keySet()) {
|
|
|
+ System.out.println(" " + key + "=" + item.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static IweldCloudUtil.WelderParams parseSingleItem(JSONObject item) {
|
|
|
+ JSONObject wrapper = new JSONObject();
|
|
|
+ JSONArray dataList = new JSONArray();
|
|
|
+ dataList.add(item);
|
|
|
+ wrapper.put("dataList", dataList);
|
|
|
+ IweldCloudUtil.WelderParams[] welders = IweldCloudUtil.parseWelderParamsFromResponse(wrapper.toJSONString());
|
|
|
+ if (welders == null || welders.length == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return welders[0];
|
|
|
}
|
|
|
|
|
|
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", "(未配置)"));
|
|
|
+ System.out.println("prodCode(配置): " + pro.getProperty("iweld.prodCode", "0"));
|
|
|
+ System.out.println("prodCode2(配置): " + pro.getProperty("iweld.prodCode2", "(未配置)"));
|
|
|
+ System.out.println("本次实时查询: prodCode=" + ALL_DEVICES_PROD_CODE + "(全部设备)");
|
|
|
String runInfoUrl = pro.getProperty("iweld.run.info.url", "").trim();
|
|
|
if (runInfoUrl.isEmpty()) {
|
|
|
runInfoUrl = pro.getProperty("iweld.robot.run.info.url", "(默认getRobotRunInfo)");
|
|
|
@@ -136,40 +192,20 @@ public class IweldCloudUtilTest {
|
|
|
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) {
|
|
|
+ private static Map<String, JSONObject> printDeviceList(String weldListInfo, Properties pro) {
|
|
|
+ Map<String, JSONObject> deviceIndex = new LinkedHashMap<String, JSONObject>();
|
|
|
try {
|
|
|
JSONObject json = JSONObject.parseObject(weldListInfo);
|
|
|
JSONArray dataList = json.getJSONArray("dataList");
|
|
|
if (dataList == null || dataList.isEmpty()) {
|
|
|
System.out.println("设备列表为空");
|
|
|
- return;
|
|
|
+ return deviceIndex;
|
|
|
}
|
|
|
|
|
|
String prodCode = pro.getProperty("iweld.prodCode", "").trim();
|
|
|
String prodCode2 = pro.getProperty("iweld.prodCode2", "").trim();
|
|
|
int totalDevices = 0;
|
|
|
- boolean matched = false;
|
|
|
- System.out.println("当前配置设备在列表中的类型:");
|
|
|
+ System.out.println("账号下全部设备(D01=制造编码, D03=自定义名称, D04=类型):");
|
|
|
for (int i = 0; i < dataList.size(); i++) {
|
|
|
JSONObject block = dataList.getJSONObject(i);
|
|
|
JSONArray list = block.getJSONArray("list");
|
|
|
@@ -180,47 +216,22 @@ public class IweldCloudUtilTest {
|
|
|
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"));
|
|
|
+ if (code != null && !code.isEmpty()) {
|
|
|
+ deviceIndex.put(code, device);
|
|
|
}
|
|
|
+ boolean configured = code != null && (code.equals(prodCode) || code.equals(prodCode2));
|
|
|
+ System.out.println(" D01=" + code
|
|
|
+ + ", D03=" + device.getString("D03")
|
|
|
+ + ", D04=" + device.getString("D04")
|
|
|
+ + " (" + deviceTypeName(device.getString("D04")) + ")"
|
|
|
+ + (configured ? " [当前配置]" : ""));
|
|
|
}
|
|
|
}
|
|
|
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());
|
|
|
}
|
|
|
+ return deviceIndex;
|
|
|
}
|
|
|
|
|
|
private static String deviceTypeName(String d04) {
|
|
|
@@ -236,58 +247,6 @@ public class IweldCloudUtilTest {
|
|
|
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);
|