远程ar调用api异常处理

This commit is contained in:
2025-10-17 15:49:13 +08:00
parent b1e3945d61
commit 2a3d68407b
2 changed files with 133 additions and 82 deletions

View File

@@ -48,13 +48,19 @@ public class ARRemoteCloudJob {
String sign = getSign(param, ARConstant.AR_APP_SECRET);
String response = arRemoteClient.getToken(
String response;
try {
response = arRemoteClient.getToken(
new ARGetTokenRequest()
.setApiKey(ARConstant.AR_APP_KEY)
.setExpires(ARConstant.EXPIRES_TIME)
.setTimestamp(timestamp)
.setSignature(sign)
);
} catch (Exception e) {
log.error("ar云识别获取token失败", e);
throw new RuntimeException("ar云识别获取token失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)) {
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
@@ -76,7 +82,13 @@ public class ARRemoteCloudJob {
@XxlJob("zyp-ar-cloud-health-check")
public void arHealthCheck() {
ARHealthCheckResponse response = arRemoteClient.arHealthCheck();
ARHealthCheckResponse response;
try {
response = arRemoteClient.arHealthCheck();
} catch (Exception e) {
log.error("ar云识别健康检查失败", e);
throw new RuntimeException("ar云识别健康检查失败");
}
Boolean result = response.getResult().getMessage().equals("pong");
Boolean cacheResult = (Boolean) redisService.get(AR_HEALTH_CHECK);
if (cacheResult == null) {
@@ -93,7 +105,6 @@ public class ARRemoteCloudJob {
redisService.set(AR_HEALTH_CHECK, result);
} else {
if (!cacheResult.equals(result)) {
Param paramOne = paramService.getOne(

View File

@@ -52,9 +52,9 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
Long timestamp = Calendar.getInstance().getTimeInMillis();
//hutool 生成随机字符串
String nonceStr = RandomUtil.randomString(10);
String response = arRemoteClient.createTarget(
String response ;
try {
response= arRemoteClient.createTarget(
token , new ARCreateTargetRequest()
.setImage(image)
.setActive(ARConstant.AR_IMAGE_ACTIVE)
@@ -67,6 +67,11 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
.setAppKey(ARConstant.AR_APP_KEY)
// .setSignature(sign)
);
}catch (Exception e){
log.error("ar云识别创建目标失败", e);
throw new RuntimeException("ar云识别创建目标失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)){
@@ -85,8 +90,13 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
Assert.notNull(targetInfo, "未找到该图片信息");
long timeInMillis = Calendar.getInstance().getTimeInMillis();
String result = arRemoteClient.delTargetInfo(getToken(), targetId , String.valueOf(timeInMillis), ARConstant.AR_APP_KEY, ARConstant.AR_APP_ID );
String result;
try {
result = arRemoteClient.delTargetInfo(getToken(), targetId, String.valueOf(timeInMillis), ARConstant.AR_APP_KEY, ARConstant.AR_APP_ID);
} catch (Exception e) {
log.error("ar云识别删除目标失败", e);
throw new RuntimeException("ar云识别删除目标失败");
}
JSONObject jsonObject = JSONObject.parseObject(result);
if (!jsonObject.get("statusCode").equals(0)) {
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
@@ -100,7 +110,13 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
private String findTargetInfo(String targetId , String token) {
long timeInMillis = Calendar.getInstance().getTimeInMillis();
String result = arRemoteClient.findTargetInfo(token, targetId , String.valueOf(timeInMillis), ARConstant.AR_APP_KEY, ARConstant.AR_APP_ID );
String result;
try {
result = arRemoteClient.findTargetInfo(token, targetId, String.valueOf(timeInMillis), ARConstant.AR_APP_KEY, ARConstant.AR_APP_ID);
} catch (Exception e) {
log.error("ar云识别查询目标失败", e);
throw new RuntimeException("ar云识别查询目标失败");
}
JSONObject jsonObject = JSONObject.parseObject(result);
if (!jsonObject.get("statusCode").equals(0)) {
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
@@ -114,13 +130,19 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
private Boolean targetDetection(String image , String token) {
Long timestamp = Calendar.getInstance().getTimeInMillis();
String response = arRemoteClient.targetDetection(
String response;
try {
response = arRemoteClient.targetDetection(
token, new ARDetectionTargetRequest()
.setImage(image)
.setTimestamp(timestamp)
.setAppId(ARConstant.AR_APP_ID)
.setAppKey(ARConstant.AR_APP_KEY)
);
} catch (Exception e) {
log.error("ar云识别目标检测失败", e);
throw new RuntimeException("ar云识别目标检测失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)) {
@@ -141,13 +163,19 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
private List<String> targetSimilar(String image ,String token) {
Long timestamp = Calendar.getInstance().getTimeInMillis();
String response = arRemoteClient.targetSimilar(
String response;
try {
response = arRemoteClient.targetSimilar(
token, new ARSimilarTargetRequest()
.setImage(image)
.setTimestamp(timestamp)
.setAppId(ARConstant.AR_APP_ID)
.setAppKey(ARConstant.AR_APP_KEY)
);
} catch (Exception e) {
log.error("ar云识别对比相似图片失败", e);
throw new RuntimeException("ar云识别对比相似图片失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)) {
@@ -181,7 +209,9 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
Long timestamp = Calendar.getInstance().getTimeInMillis();
String response = arRemoteClient.updateTarget(
String response;
try {
response = arRemoteClient.updateTarget(
getToken(), new ARUpdateTargetRequest()
.setImage(image)
.setActive(ARConstant.AR_IMAGE_ACTIVE)
@@ -192,6 +222,10 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
.setAppKey(ARConstant.AR_APP_KEY),
targetId
);
} catch (Exception e) {
log.error("ar云识别更新图片失败", e);
throw new RuntimeException("ar云识别更新图片失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)) {
@@ -206,7 +240,9 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
public String searchTarget(String image) {
Long timestamp = Calendar.getInstance().getTimeInMillis();
String response = arRemoteClient.searchTarget(
String response;
try {
response = arRemoteClient.searchTarget(
getToken(), new ARSearchTargetRequest()
.setImage(image)
.setNotracking("true")
@@ -215,6 +251,10 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
.setAppKey(ARConstant.AR_APP_KEY)
);
} catch (Exception e) {
log.error("ar云识别搜索图片失败", e);
throw new RuntimeException("ar云识别搜索图片失败");
}
JSONObject jsonObject = JSONObject.parseObject(response);
if (!jsonObject.get("statusCode").equals(0)) {