refactor(device): 重构设备列表查询接口并优化日志输出- 重写 getDeviceCodes 方法,改为 getInstanceListInfo,优化参数设计

- 移除未使用的 getDeviceInfo 方法
- 在关键步骤添加调试日志输出,便于问题排查
- 更新网络配置,添加新的服务器 IP
This commit is contained in:
yjj38 2025-07-10 11:41:50 +08:00
parent 247db8b28e
commit 175db8f49b
5 changed files with 122 additions and 156 deletions

View File

@ -233,11 +233,9 @@ public class ArmCloudApiClient {
if (code != 200) { if (code != 200) {
String errorMsg = responseJson.optString("msg", "未知错误"); String errorMsg = responseJson.optString("msg", "未知错误");
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg);
} }
} else { } else {
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应中缺少 'code' 字段",null); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应中缺少 'code' 字段",null);
throw new IOException("响应中缺少 'code' 字段");
} }
return responseBodyString; return responseBodyString;
@ -278,57 +276,50 @@ public class ArmCloudApiClient {
} }
/** /**
* 分页查询 ARM 设备列表并提取 deviceCode * 分页查询实例列表信息
* *
* @param page 页码 * @param page 页码
* @param rows 每页条数 * @param rows 每页条数
* @param padAllocationStatus 实例分配状态-2删除失败 -1分配失败 0-未分配1-分配中 2-已分配 3-删除中 * @param armServerCode 服务器编号
* @param deviceStatus 物理机状态0-离线1-在线 * @param deviceCode 板卡编号
* @param armServerCode 服务器编码 * @param padCodes 实例编号数组
* @param deviceCode 物理机编号 * @param groupIds 实例分组ID数组
* @param deviceIp 物理机IP * @param idc 机房Id
* @param armServerStatus 服务器状态0-离线1-在线 * @return 匹配的 padCode 数组
* @param idc 机房ID
* @param deviceIpList 板卡IP列表
* @return 匹配的 deviceCode 数组
* @throws IOException 请求失败或网络错误 * @throws IOException 请求失败或网络错误
*/ */
public String[] getDeviceCodes( public String[] getInstanceListInfo(
int page, int page,
int rows, int rows,
Integer padAllocationStatus,
Integer deviceStatus,
String armServerCode, String armServerCode,
String deviceCode, String deviceCode,
String deviceIp, String[] padCodes,
Integer armServerStatus, Integer[] groupIds,
String idc, String idc) throws IOException {
String[] deviceIpList) throws IOException {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
try { try {
json.put("page", page); json.put("page", page);
json.put("rows", rows); json.put("rows", rows);
if (padAllocationStatus != null) json.put("padAllocationStatus", padAllocationStatus);
if (deviceStatus != null) json.put("deviceStatus", deviceStatus);
if (armServerCode != null) json.put("armServerCode", armServerCode); if (armServerCode != null) json.put("armServerCode", armServerCode);
if (deviceCode != null) json.put("deviceCode", deviceCode); if (deviceCode != null) json.put("deviceCode", deviceCode);
if (deviceIp != null) json.put("deviceIp", deviceIp); if (padCodes != null && padCodes.length > 0) {
if (armServerStatus != null) json.put("armServerStatus", armServerStatus); json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
if (idc != null) json.put("idc", idc);
if (deviceIpList != null && deviceIpList.length > 0) {
json.put("deviceIpList", new JSONArray(Arrays.asList(deviceIpList)));
} }
if (groupIds != null && groupIds.length > 0) {
json.put("groupIds", new JSONArray(Arrays.asList(groupIds)));
}
if (idc != null) json.put("idc", idc);
} catch (JSONException e) { } catch (JSONException e) {
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: JSON 构建失败", e); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: JSON 构建失败", e);
throw new IOException("JSON 构建失败", e); throw new IOException("JSON 构建失败", e);
} }
String jsonBody = json.toString(); String jsonBody = json.toString();
String timestamp = String.valueOf(System.currentTimeMillis()); String timestamp = String.valueOf(System.currentTimeMillis());
String API_PATH = "/openapi/open/device/list"; String API_PATH = "/openapi/open/pad/infos";
if (secretKey == null || secretKey.isEmpty()) { if (secretKey == null || secretKey.isEmpty()) {
throw new IllegalArgumentException("secretKey 不能为空"); throw new IllegalArgumentException("secretKey 不能为空");
@ -338,7 +329,7 @@ public class ArmCloudApiClient {
try { try {
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey); signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
} catch (Exception e) { } catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 签名计算失败", e); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 签名计算失败", e);
throw new IOException("签名计算失败", e); throw new IOException("签名计算失败", e);
} }
@ -374,39 +365,39 @@ public class ArmCloudApiClient {
int code = responseJson.getInt("code"); int code = responseJson.getInt("code");
if (code != 200) { if (code != 200) {
String errorMsg = responseJson.optString("msg", "未知错误"); String errorMsg = responseJson.optString("msg", "未知错误");
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 接口返回错误码 " + code + ", 错误信息: " + errorMsg,null); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg); throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg);
} }
} else { } else {
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 响应中缺少 'code' 字段",null); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应中缺少 'code' 字段", null);
throw new IOException("响应中缺少 'code' 字段"); throw new IOException("响应中缺少 'code' 字段");
} }
// 提取 deviceCode 列表 // 提取 padCode 列表
JSONArray pageDataArray = responseJson.optJSONObject("data") JSONArray pageDataArray = responseJson.optJSONObject("data")
.optJSONArray("pageData"); .optJSONArray("pageData");
if (pageDataArray == null || pageDataArray.length() == 0) { if (pageDataArray == null || pageDataArray.length() == 0) {
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getDeviceCodes: 查询结果为空",null); LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 查询结果为空", null);
return new String[0]; return new String[0];
} }
int length = pageDataArray.length(); int length = pageDataArray.length();
String[] deviceCodes = new String[length]; String[] padCodesResult = new String[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
JSONObject item = pageDataArray.getJSONObject(i); JSONObject item = pageDataArray.getJSONObject(i);
if (!item.has("deviceCode")) { if (!item.has("padCode")) {
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getDeviceCodes: 返回对象缺少 'deviceCode' 字段",null); LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 返回对象缺少 'padCode' 字段", null);
throw new IOException("返回对象缺少 'deviceCode' 字段"); throw new IOException("返回对象缺少 'padCode' 字段");
} }
deviceCodes[i] = item.getString("deviceCode"); padCodesResult[i] = item.getString("padCode");
} }
return deviceCodes; return padCodesResult;
} catch (JSONException e) { } catch (JSONException e) {
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 响应解析失败", e); LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应解析失败", e);
throw new IOException("响应解析失败", e); throw new IOException("响应解析失败", e);
} }
} }

View File

@ -24,6 +24,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -72,6 +73,8 @@ public class ChangeDeviceInfoUtil {
String bigoJson = fetchJsonSafely(buildBigoUrl(country, tag), "bigoJson"); String bigoJson = fetchJsonSafely(buildBigoUrl(country, tag), "bigoJson");
String afJson = fetchJsonSafely(buildAfUrl(country, tag), "afJson"); String afJson = fetchJsonSafely(buildAfUrl(country, tag), "afJson");
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Received bigoJson: " + bigoJson, null);
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Received afJson: " + afJson, null);
fallBackToNetworkData(bigoJson, afJson); fallBackToNetworkData(bigoJson, afJson);
logDeviceObjects(); logDeviceObjects();
@ -107,37 +110,7 @@ public class ChangeDeviceInfoUtil {
} }
} }
public static void getDeviceInfo(String taskId, String androidId) {
if (taskId == null || androidId == null || taskId.isBlank() || androidId.isBlank()) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Invalid task", null);
return;
}
executorService.submit(() -> {
String response = "";
try {
response = executeQuerySafely(androidId, taskId);
} catch (Exception e) {
e.printStackTrace();
}
if (response == null || response.isBlank()) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query", null);
return;
}
if (isValidResponse(response)) {
try {
synchronized (ChangeDeviceInfoUtil.class) { // 防止并发访问
parseAndSetDeviceObjects(response);
}
} catch (JSONException e) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error parsing JSON", e);
}
} else {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during query", null);
}
});
}
private static String fetchJsonSafely(String url, String logKey) throws IOException { private static String fetchJsonSafely(String url, String logKey) throws IOException {
String json = null; String json = null;
@ -438,7 +411,8 @@ public class ChangeDeviceInfoUtil {
// 调用接口更新实例属性 // 调用接口更新实例属性
try { try {
String[] padCodes = client.getDeviceCodes(1, 100, null, null, null, null, null, null, null, null); String[] padCodes = client.getInstanceListInfo(1, 100, null, null, null, null, null);
LogFileUtil.logAndWrite(Log.DEBUG, "ChangeDeviceInfoUtil", "padCodes: " + Arrays.toString(padCodes), null);
String response = client.updateInstanceProperties( String response = client.updateInstanceProperties(
padCodes, padCodes,
null, // modemPersistProps null, // modemPersistProps

View File

@ -139,6 +139,7 @@ public class TaskUtil {
Log.d("TaskUtil", "Built HTTP request for device info download"); Log.d("TaskUtil", "Built HTTP request for device info download");
try (Response response = okHttpClient.newCall(request).execute()) { try (Response response = okHttpClient.newCall(request).execute()) {
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "TaskUtil", "Response : " + response, null);
// 检查响应是否成功 // 检查响应是否成功
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
String errorMessage = "Unexpected response: Code=" + response.code() + String errorMessage = "Unexpected response: Code=" + response.code() +
@ -800,4 +801,3 @@ class Payload {
AfInfo afDeviceObject; AfInfo afDeviceObject;
DeviceInfo other; DeviceInfo other;
} }

View File

@ -13,5 +13,6 @@
<domain includeSubdomains="true">8.217.137.25</domain> <domain includeSubdomains="true">8.217.137.25</domain>
<domain includeSubdomains="true">47.238.96.231</domain> <domain includeSubdomains="true">47.238.96.231</domain>
<domain includeSubdomains="true">192.168.30.80</domain> <domain includeSubdomains="true">192.168.30.80</domain>
<domain includeSubdomains="true">39.103.73.250</domain> <!-- 你的特定 IP 地址 -->
</domain-config> </domain-config>
</network-security-config> </network-security-config>