refactor(device): 重构 ArmCloudApiClient 类
- 格式化代码,提高可读性 - 优化 JSON 构建和解析逻辑 - 统一异常处理方式 - 简化部分代码结构,提高维护性
This commit is contained in:
parent
237f07f968
commit
c3467add0e
|
@ -149,100 +149,100 @@ public class ArmCloudApiClient {
|
||||||
* null, // settingProps oaidProps );
|
* null, // settingProps oaidProps );
|
||||||
*/
|
*/
|
||||||
public String updateInstanceProperties(
|
public String updateInstanceProperties(
|
||||||
String[] padCodes,
|
String[] padCodes,
|
||||||
List<PropertyItem> modemPersistProps,
|
List<PropertyItem> modemPersistProps,
|
||||||
List<PropertyItem> modemProps,
|
List<PropertyItem> modemProps,
|
||||||
List<PropertyItem> systemPersistProps,
|
List<PropertyItem> systemPersistProps,
|
||||||
List<PropertyItem> systemProps,
|
List<PropertyItem> systemProps,
|
||||||
List<PropertyItem> settingProps,
|
List<PropertyItem> settingProps,
|
||||||
List<PropertyItem> oaidProps
|
List<PropertyItem> oaidProps
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
if (padCodes == null || padCodes.length == 0) {
|
if (padCodes == null || padCodes.length == 0) {
|
||||||
throw new IllegalArgumentException("padCodes 不能为空");
|
throw new IllegalArgumentException("padCodes 不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查 padCodes 是否有重复项
|
||||||
|
Set<String> padCodeSet = new HashSet<>();
|
||||||
|
for (String code : padCodes) {
|
||||||
|
if (!padCodeSet.add(code)) {
|
||||||
|
throw new IllegalArgumentException("padCodes 包含重复项: " + code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
try {
|
||||||
|
json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
|
||||||
|
putPropertyItems(json, "modemPersistPropertiesList", modemPersistProps);
|
||||||
|
putPropertyItems(json, "modemPropertiesList", modemProps);
|
||||||
|
putPropertyItems(json, "systemPersistPropertiesList", systemPersistProps);
|
||||||
|
putPropertyItems(json, "systemPropertiesList", systemProps);
|
||||||
|
putPropertyItems(json, "settingPropertiesList", settingProps);
|
||||||
|
putPropertyItems(json, "oaidPropertiesList", oaidProps);
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: JSON 构建失败", e);
|
||||||
|
throw new IOException("JSON 构建失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
String jsonBody = json.toString();
|
||||||
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
|
String API_PATH = "/openapi/open/pad/updatePadProperties";
|
||||||
|
|
||||||
|
if (secretKey == null || secretKey.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("secretKey 不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
String signature;
|
||||||
|
try {
|
||||||
|
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 签名计算失败", e);
|
||||||
|
throw new IOException("签名计算失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
RequestBody body = RequestBody.create(
|
||||||
|
MediaType.get("application/json; charset=utf-8"),
|
||||||
|
jsonBody
|
||||||
|
);
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(baseUrl + API_PATH)
|
||||||
|
.addHeader("authver", "2.0")
|
||||||
|
.addHeader("x-ak", accessKey)
|
||||||
|
.addHeader("x-timestamp", timestamp)
|
||||||
|
.addHeader("x-sign", signature)
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
throw new IOException("请求失败: " + response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查 padCodes 是否有重复项
|
ResponseBody responseBody = response.body();
|
||||||
Set<String> padCodeSet = new HashSet<>();
|
if (responseBody == null) {
|
||||||
for (String code : padCodes) {
|
throw new IOException("响应体为空");
|
||||||
if (!padCodeSet.add(code)) {
|
|
||||||
throw new IllegalArgumentException("padCodes 包含重复项: " + code);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
String responseBodyString = responseBody.string();
|
||||||
try {
|
JSONObject responseJson = new JSONObject(responseBodyString);
|
||||||
json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
|
|
||||||
putPropertyItems(json, "modemPersistPropertiesList", modemPersistProps);
|
|
||||||
putPropertyItems(json, "modemPropertiesList", modemProps);
|
|
||||||
putPropertyItems(json, "systemPersistPropertiesList", systemPersistProps);
|
|
||||||
putPropertyItems(json, "systemPropertiesList", systemProps);
|
|
||||||
putPropertyItems(json, "settingPropertiesList", settingProps);
|
|
||||||
putPropertyItems(json, "oaidPropertiesList", oaidProps);
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
// 校验返回码
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: JSON 构建失败", e);
|
if (responseJson.has("code")) {
|
||||||
throw new IOException("JSON 构建失败", e);
|
int code = responseJson.getInt("code");
|
||||||
|
if (code != 200) {
|
||||||
|
String errorMsg = responseJson.optString("msg", "未知错误");
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应中缺少 'code' 字段", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
String jsonBody = json.toString();
|
return responseBodyString;
|
||||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
} catch (JSONException e) {
|
||||||
String API_PATH = "/openapi/open/pad/updatePadProperties";
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应解析失败", e);
|
||||||
|
throw new IOException("响应解析失败", e);
|
||||||
if (secretKey == null || secretKey.isEmpty()) {
|
}
|
||||||
throw new IllegalArgumentException("secretKey 不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
String signature;
|
|
||||||
try {
|
|
||||||
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 签名计算失败", e);
|
|
||||||
throw new IOException("签名计算失败", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestBody body = RequestBody.create(
|
|
||||||
MediaType.get("application/json; charset=utf-8"),
|
|
||||||
jsonBody
|
|
||||||
);
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(baseUrl + API_PATH)
|
|
||||||
.addHeader("authver", "2.0")
|
|
||||||
.addHeader("x-ak", accessKey)
|
|
||||||
.addHeader("x-timestamp", timestamp)
|
|
||||||
.addHeader("x-sign", signature)
|
|
||||||
.post(body)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
|
||||||
if (!response.isSuccessful()) {
|
|
||||||
throw new IOException("请求失败: " + response);
|
|
||||||
}
|
|
||||||
|
|
||||||
ResponseBody responseBody = response.body();
|
|
||||||
if (responseBody == null) {
|
|
||||||
throw new IOException("响应体为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
String responseBodyString = responseBody.string();
|
|
||||||
JSONObject responseJson = new JSONObject(responseBodyString);
|
|
||||||
|
|
||||||
// 校验返回码
|
|
||||||
if (responseJson.has("code")) {
|
|
||||||
int code = responseJson.getInt("code");
|
|
||||||
if (code != 200) {
|
|
||||||
String errorMsg = responseJson.optString("msg", "未知错误");
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应中缺少 'code' 字段",null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return responseBodyString;
|
|
||||||
} catch (JSONException e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "updateInstanceProperties: 响应解析失败", e);
|
|
||||||
throw new IOException("响应解析失败", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -288,118 +288,124 @@ public class ArmCloudApiClient {
|
||||||
* @return 匹配的 padCode 数组
|
* @return 匹配的 padCode 数组
|
||||||
* @throws IOException 请求失败或网络错误
|
* @throws IOException 请求失败或网络错误
|
||||||
*/
|
*/
|
||||||
public String[] getInstanceListInfo(
|
public String[] getInstanceListInfo(
|
||||||
int page,
|
int page,
|
||||||
int rows,
|
int rows,
|
||||||
String armServerCode,
|
String armServerCode,
|
||||||
String deviceCode,
|
String deviceCode,
|
||||||
String[] padCodes,
|
String[] padCodes,
|
||||||
Integer[] groupIds,
|
Integer[] groupIds,
|
||||||
String idc) throws IOException {
|
String idc) 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 (armServerCode != null) json.put("armServerCode", armServerCode);
|
if (armServerCode != null) {
|
||||||
if (deviceCode != null) json.put("deviceCode", deviceCode);
|
json.put("armServerCode", armServerCode);
|
||||||
if (padCodes != null && padCodes.length > 0) {
|
}
|
||||||
json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
|
if (deviceCode != null) {
|
||||||
}
|
json.put("deviceCode", deviceCode);
|
||||||
if (groupIds != null && groupIds.length > 0) {
|
}
|
||||||
json.put("groupIds", new JSONArray(Arrays.asList(groupIds)));
|
if (padCodes != null && padCodes.length > 0) {
|
||||||
}
|
json.put("padCodes", new JSONArray(Arrays.asList(padCodes)));
|
||||||
if (idc != null) json.put("idc", idc);
|
}
|
||||||
|
if (groupIds != null && groupIds.length > 0) {
|
||||||
} catch (JSONException e) {
|
json.put("groupIds", new JSONArray(Arrays.asList(groupIds)));
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: JSON 构建失败", e);
|
}
|
||||||
throw new IOException("JSON 构建失败", e);
|
if (idc != null) {
|
||||||
}
|
json.put("idc", idc);
|
||||||
|
|
||||||
String jsonBody = json.toString();
|
|
||||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
|
||||||
String API_PATH = "/openapi/open/pad/infos";
|
|
||||||
|
|
||||||
if (secretKey == null || secretKey.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("secretKey 不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
String signature;
|
|
||||||
try {
|
|
||||||
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 签名计算失败", e);
|
|
||||||
throw new IOException("签名计算失败", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestBody body = RequestBody.create(
|
|
||||||
MediaType.get("application/json; charset=utf-8"),
|
|
||||||
jsonBody
|
|
||||||
);
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(baseUrl + API_PATH)
|
|
||||||
.addHeader("authver", "2.0")
|
|
||||||
.addHeader("x-ak", accessKey)
|
|
||||||
.addHeader("x-timestamp", timestamp)
|
|
||||||
.addHeader("x-sign", signature)
|
|
||||||
.post(body)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
|
||||||
if (!response.isSuccessful()) {
|
|
||||||
throw new IOException("请求失败: " + response);
|
|
||||||
}
|
|
||||||
|
|
||||||
ResponseBody responseBody = response.body();
|
|
||||||
if (responseBody == null) {
|
|
||||||
throw new IOException("响应体为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
String responseBodyString = responseBody.string();
|
|
||||||
JSONObject responseJson = new JSONObject(responseBodyString);
|
|
||||||
|
|
||||||
// 校验返回码
|
|
||||||
if (responseJson.has("code")) {
|
|
||||||
int code = responseJson.getInt("code");
|
|
||||||
if (code != 200) {
|
|
||||||
String errorMsg = responseJson.optString("msg", "未知错误");
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
|
|
||||||
throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应中缺少 'code' 字段", null);
|
|
||||||
throw new IOException("响应中缺少 'code' 字段");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取 padCode 列表
|
|
||||||
JSONArray pageDataArray = responseJson.optJSONObject("data")
|
|
||||||
.optJSONArray("pageData");
|
|
||||||
|
|
||||||
if (pageDataArray == null || pageDataArray.length() == 0) {
|
|
||||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 查询结果为空", null);
|
|
||||||
return new String[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
int length = pageDataArray.length();
|
|
||||||
String[] padCodesResult = new String[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
JSONObject item = pageDataArray.getJSONObject(i);
|
|
||||||
if (!item.has("padCode")) {
|
|
||||||
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 返回对象缺少 'padCode' 字段", null);
|
|
||||||
throw new IOException("返回对象缺少 'padCode' 字段");
|
|
||||||
}
|
|
||||||
padCodesResult[i] = item.getString("padCode");
|
|
||||||
}
|
|
||||||
|
|
||||||
return padCodesResult;
|
|
||||||
|
|
||||||
} catch (JSONException e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应解析失败", e);
|
|
||||||
throw new IOException("响应解析失败", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (JSONException 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/pad/infos";
|
||||||
|
|
||||||
|
if (secretKey == null || secretKey.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("secretKey 不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
String signature;
|
||||||
|
try {
|
||||||
|
signature = calculateSignature(timestamp, API_PATH, jsonBody, secretKey);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 签名计算失败", e);
|
||||||
|
throw new IOException("签名计算失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
RequestBody body = RequestBody.create(
|
||||||
|
MediaType.get("application/json; charset=utf-8"),
|
||||||
|
jsonBody
|
||||||
|
);
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(baseUrl + API_PATH)
|
||||||
|
.addHeader("authver", "2.0")
|
||||||
|
.addHeader("x-ak", accessKey)
|
||||||
|
.addHeader("x-timestamp", timestamp)
|
||||||
|
.addHeader("x-sign", signature)
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
throw new IOException("请求失败: " + response);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponseBody responseBody = response.body();
|
||||||
|
if (responseBody == null) {
|
||||||
|
throw new IOException("响应体为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
String responseBodyString = responseBody.string();
|
||||||
|
JSONObject responseJson = new JSONObject(responseBodyString);
|
||||||
|
|
||||||
|
// 校验返回码
|
||||||
|
if (responseJson.has("code")) {
|
||||||
|
int code = responseJson.getInt("code");
|
||||||
|
if (code != 200) {
|
||||||
|
String errorMsg = responseJson.optString("msg", "未知错误");
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 接口返回错误码 " + code + ", 错误信息: " + errorMsg, null);
|
||||||
|
throw new IOException("接口返回错误码: " + code + ", 错误信息: " + errorMsg);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应中缺少 'code' 字段", null);
|
||||||
|
throw new IOException("响应中缺少 'code' 字段");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取 padCode 列表
|
||||||
|
JSONArray pageDataArray = responseJson.optJSONObject("data")
|
||||||
|
.optJSONArray("pageData");
|
||||||
|
|
||||||
|
if (pageDataArray == null || pageDataArray.length() == 0) {
|
||||||
|
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 查询结果为空", null);
|
||||||
|
return new String[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
int length = pageDataArray.length();
|
||||||
|
String[] padCodesResult = new String[length];
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
JSONObject item = pageDataArray.getJSONObject(i);
|
||||||
|
if (!item.has("padCode")) {
|
||||||
|
LogFileUtil.logAndWrite(Log.WARN, "ArmCloudApiClient", "getInstanceListInfo: 返回对象缺少 'padCode' 字段", null);
|
||||||
|
throw new IOException("返回对象缺少 'padCode' 字段");
|
||||||
|
}
|
||||||
|
padCodesResult[i] = item.getString("padCode");
|
||||||
|
}
|
||||||
|
|
||||||
|
return padCodesResult;
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ArmCloudApiClient", "getInstanceListInfo: 响应解析失败", e);
|
||||||
|
throw new IOException("响应解析失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue