refactor(device): 重构设备列表查询接口并优化日志输出- 重写 getDeviceCodes 方法,改为 getInstanceListInfo,优化参数设计
- 移除未使用的 getDeviceInfo 方法 - 在关键步骤添加调试日志输出,便于问题排查 - 更新网络配置,添加新的服务器 IP
This commit is contained in:
parent
247db8b28e
commit
175db8f49b
|
@ -233,11 +233,9 @@ public class ArmCloudApiClient {
|
|||
if (code != 200) {
|
||||
String errorMsg = responseJson.optString("msg", "未知错误");
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
|
||||
throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg);
|
||||
}
|
||||
} else {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应中缺少 'code' 字段",null);
|
||||
throw new IOException("响应中缺少 'code' 字段");
|
||||
}
|
||||
|
||||
return responseBodyString;
|
||||
|
@ -278,57 +276,50 @@ public class ArmCloudApiClient {
|
|||
}
|
||||
|
||||
/**
|
||||
* 分页查询 ARM 设备列表,并提取 deviceCode
|
||||
* 分页查询实例列表信息
|
||||
*
|
||||
* @param page 页码
|
||||
* @param rows 每页条数
|
||||
* @param padAllocationStatus 实例分配状态:-2删除失败 -1分配失败 0-未分配;1-分配中 2-已分配 3-删除中
|
||||
* @param deviceStatus 物理机状态:0-离线;1-在线
|
||||
* @param armServerCode 服务器编码
|
||||
* @param deviceCode 物理机编号
|
||||
* @param deviceIp 物理机IP
|
||||
* @param armServerStatus 服务器状态:0-离线;1-在线
|
||||
* @param idc 机房ID
|
||||
* @param deviceIpList 板卡IP列表
|
||||
* @return 匹配的 deviceCode 数组
|
||||
* @param armServerCode 服务器编号
|
||||
* @param deviceCode 板卡编号
|
||||
* @param padCodes 实例编号数组
|
||||
* @param groupIds 实例分组ID数组
|
||||
* @param idc 机房Id
|
||||
* @return 匹配的 padCode 数组
|
||||
* @throws IOException 请求失败或网络错误
|
||||
*/
|
||||
public String[] getDeviceCodes(
|
||||
public String[] getInstanceListInfo(
|
||||
int page,
|
||||
int rows,
|
||||
Integer padAllocationStatus,
|
||||
Integer deviceStatus,
|
||||
String armServerCode,
|
||||
String deviceCode,
|
||||
String deviceIp,
|
||||
Integer armServerStatus,
|
||||
String idc,
|
||||
String[] deviceIpList) throws IOException {
|
||||
String[] padCodes,
|
||||
Integer[] groupIds,
|
||||
String idc) throws IOException {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("page", page);
|
||||
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 (deviceCode != null) json.put("deviceCode", deviceCode);
|
||||
if (deviceIp != null) json.put("deviceIp", deviceIp);
|
||||
if (armServerStatus != null) json.put("armServerStatus", armServerStatus);
|
||||
if (idc != null) json.put("idc", idc);
|
||||
if (deviceIpList != null && deviceIpList.length > 0) {
|
||||
json.put("deviceIpList", new JSONArray(Arrays.asList(deviceIpList)));
|
||||
if (padCodes != null && padCodes.length > 0) {
|
||||
json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
|
||||
}
|
||||
if (groupIds != null && groupIds.length > 0) {
|
||||
json.put("groupIds", new JSONArray(Arrays.asList(groupIds)));
|
||||
}
|
||||
if (idc != null) json.put("idc", idc);
|
||||
|
||||
} catch (JSONException e) {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: JSON 构建失败", e);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: JSON 构建失败", e);
|
||||
throw new IOException("JSON 构建失败", e);
|
||||
}
|
||||
|
||||
String jsonBody = json.toString();
|
||||
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()) {
|
||||
throw new IllegalArgumentException("secretKey 不能为空");
|
||||
|
@ -338,7 +329,7 @@ public class ArmCloudApiClient {
|
|||
try {
|
||||
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
|
||||
} catch (Exception e) {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 签名计算失败", e);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 签名计算失败", e);
|
||||
throw new IOException("签名计算失败", e);
|
||||
}
|
||||
|
||||
|
@ -374,39 +365,39 @@ public class ArmCloudApiClient {
|
|||
int code = responseJson.getInt("code");
|
||||
if (code != 200) {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 响应中缺少 'code' 字段",null);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应中缺少 'code' 字段", null);
|
||||
throw new IOException("响应中缺少 'code' 字段");
|
||||
}
|
||||
|
||||
// 提取 deviceCode 列表
|
||||
// 提取 padCode 列表
|
||||
JSONArray pageDataArray = responseJson.optJSONObject("data")
|
||||
.optJSONArray("pageData");
|
||||
|
||||
if (pageDataArray == null || pageDataArray.length() == 0) {
|
||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getDeviceCodes: 查询结果为空",null);
|
||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 查询结果为空", null);
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
int length = pageDataArray.length();
|
||||
String[] deviceCodes = new String[length];
|
||||
String[] padCodesResult = new String[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
JSONObject item = pageDataArray.getJSONObject(i);
|
||||
if (!item.has("deviceCode")) {
|
||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getDeviceCodes: 返回对象缺少 'deviceCode' 字段",null);
|
||||
throw new IOException("返回对象缺少 'deviceCode' 字段");
|
||||
if (!item.has("padCode")) {
|
||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 返回对象缺少 'padCode' 字段", null);
|
||||
throw new IOException("返回对象缺少 'padCode' 字段");
|
||||
}
|
||||
deviceCodes[i] = item.getString("deviceCode");
|
||||
padCodesResult[i] = item.getString("padCode");
|
||||
}
|
||||
|
||||
return deviceCodes;
|
||||
return padCodesResult;
|
||||
|
||||
} catch (JSONException e) {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getDeviceCodes: 响应解析失败", e);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应解析失败", e);
|
||||
throw new IOException("响应解析失败", e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -72,6 +73,8 @@ public class ChangeDeviceInfoUtil {
|
|||
String bigoJson = fetchJsonSafely(buildBigoUrl(country, tag), "bigoJson");
|
||||
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);
|
||||
|
||||
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 {
|
||||
String json = null;
|
||||
|
@ -438,7 +411,8 @@ public class ChangeDeviceInfoUtil {
|
|||
|
||||
// 调用接口更新实例属性
|
||||
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(
|
||||
padCodes,
|
||||
null, // modemPersistProps
|
||||
|
|
|
@ -139,6 +139,7 @@ public class TaskUtil {
|
|||
Log.d("TaskUtil", "Built HTTP request for device info download");
|
||||
|
||||
try (Response response = okHttpClient.newCall(request).execute()) {
|
||||
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "TaskUtil", "Response : " + response, null);
|
||||
// 检查响应是否成功
|
||||
if (!response.isSuccessful()) {
|
||||
String errorMessage = "Unexpected response: Code=" + response.code() +
|
||||
|
@ -800,4 +801,3 @@ class Payload {
|
|||
AfInfo afDeviceObject;
|
||||
DeviceInfo other;
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
|
@ -13,5 +13,6 @@
|
|||
<domain includeSubdomains="true">8.217.137.25</domain>
|
||||
<domain includeSubdomains="true">47.238.96.231</domain>
|
||||
<domain includeSubdomains="true">192.168.30.80</domain>
|
||||
<domain includeSubdomains="true">39.103.73.250</domain> <!-- 你的特定 IP 地址 -->
|
||||
</domain-config>
|
||||
</network-security-config>
|
||||
|
|
Loading…
Reference in New Issue