远程ar调用api异常处理
This commit is contained in:
@@ -37,7 +37,7 @@ public class ARRemoteCloudJob {
|
||||
private static final String AR_HEALTH_CHECK = "zyp_ar_health_check";
|
||||
|
||||
@XxlJob("zyp-ar-cloud-refresh-token")
|
||||
public void arCloudRefreshToken(){
|
||||
public void arCloudRefreshToken() {
|
||||
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
|
||||
@@ -48,15 +48,21 @@ 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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别获取token失败" : msg);
|
||||
}
|
||||
@@ -67,7 +73,7 @@ public class ARRemoteCloudJob {
|
||||
.eq(Param::getParamKey, "zyp.ar.auth.token")
|
||||
.last("limit 1")
|
||||
);
|
||||
if (!paramService.updateById(paramOne.setParamValue( token))){
|
||||
if (!paramService.updateById(paramOne.setParamValue(token))) {
|
||||
throw new RuntimeException("更新ar云识别token失败");
|
||||
}
|
||||
|
||||
@@ -75,34 +81,39 @@ public class ARRemoteCloudJob {
|
||||
|
||||
|
||||
@XxlJob("zyp-ar-cloud-health-check")
|
||||
public void arHealthCheck(){
|
||||
ARHealthCheckResponse response = arRemoteClient.arHealthCheck();
|
||||
public void 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){
|
||||
if (cacheResult == null) {
|
||||
Param paramOne = paramService.getOne(
|
||||
Wrappers.lambdaQuery(Param.class)
|
||||
.eq(Param::getParamKey, "ar.is.open")
|
||||
.last("limit 1")
|
||||
);
|
||||
|
||||
if (!paramService.updateById(paramOne.setParamValue( String.valueOf( result)))){
|
||||
if (!paramService.updateById(paramOne.setParamValue(String.valueOf(result)))) {
|
||||
throw new RuntimeException("更新ar云识别token失败");
|
||||
}
|
||||
|
||||
redisService.set(AR_HEALTH_CHECK, result);
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
if (!cacheResult.equals(result)){
|
||||
if (!cacheResult.equals(result)) {
|
||||
Param paramOne = paramService.getOne(
|
||||
Wrappers.lambdaQuery(Param.class)
|
||||
.eq(Param::getParamKey, "ar.is.open")
|
||||
.last("limit 1")
|
||||
);
|
||||
|
||||
if (!paramService.updateById(paramOne.setParamValue( String.valueOf( result)))){
|
||||
if (!paramService.updateById(paramOne.setParamValue(String.valueOf(result)))) {
|
||||
throw new RuntimeException("更新ar云识别token失败");
|
||||
}
|
||||
redisService.set(AR_HEALTH_CHECK, result);
|
||||
|
||||
@@ -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)){
|
||||
@@ -81,14 +86,19 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
||||
public Boolean deleteTargetInfo(String targetId) {
|
||||
String token = getToken();
|
||||
|
||||
String targetInfo = findTargetInfo(targetId , token);
|
||||
String targetInfo = findTargetInfo(targetId, token);
|
||||
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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
@@ -100,9 +110,15 @@ 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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
@@ -114,24 +130,30 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
||||
private Boolean targetDetection(String image , String token) {
|
||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
String response = arRemoteClient.targetDetection(
|
||||
token , new ARDetectionTargetRequest()
|
||||
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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
Integer grade = JSONObject.parse(jsonObject.getString("result")).getInteger("grade");
|
||||
if (grade == -1){
|
||||
if (grade == -1) {
|
||||
throw new RuntimeException("图像不符合规范或已损坏");
|
||||
}
|
||||
if (grade > 2){
|
||||
if (grade > 2) {
|
||||
throw new RuntimeException("评分太高,图片难以识别");
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
@@ -141,16 +163,22 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
||||
private List<String> targetSimilar(String image ,String token) {
|
||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
String response = arRemoteClient.targetSimilar(
|
||||
token , new ARSimilarTargetRequest()
|
||||
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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
@@ -167,22 +195,24 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
||||
public String updateTarget(String image, String targetId) {
|
||||
String token = getToken();
|
||||
|
||||
String targetInfo = findTargetInfo(targetId , token);
|
||||
String targetInfo = findTargetInfo(targetId, token);
|
||||
Assert.notNull(targetInfo, "未找到该图片信息");
|
||||
|
||||
List<String> targetIdList = targetSimilar(image , token);
|
||||
if (!targetIdList.isEmpty()){
|
||||
List<String> targetIdList = targetSimilar(image, token);
|
||||
if (!targetIdList.isEmpty()) {
|
||||
throw new RuntimeException("当前图库里存在已有的类似图片");
|
||||
}
|
||||
|
||||
if (!targetDetection(image , token)){
|
||||
if (!targetDetection(image, token)) {
|
||||
throw new RuntimeException("当前图片无法识别");
|
||||
}
|
||||
|
||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
String response = arRemoteClient.updateTarget(
|
||||
getToken() , new ARUpdateTargetRequest()
|
||||
String response;
|
||||
try {
|
||||
response = arRemoteClient.updateTarget(
|
||||
getToken(), new ARUpdateTargetRequest()
|
||||
.setImage(image)
|
||||
.setActive(ARConstant.AR_IMAGE_ACTIVE)
|
||||
.setSize(ARConstant.AR_IMAGE_DEFAULT_SIZE)
|
||||
@@ -192,9 +222,13 @@ 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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
@@ -206,8 +240,10 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
||||
public String searchTarget(String image) {
|
||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
String response = arRemoteClient.searchTarget(
|
||||
getToken() , new ARSearchTargetRequest()
|
||||
String response;
|
||||
try {
|
||||
response = arRemoteClient.searchTarget(
|
||||
getToken(), new ARSearchTargetRequest()
|
||||
.setImage(image)
|
||||
.setNotracking("true")
|
||||
.setTimestamp(timestamp)
|
||||
@@ -215,9 +251,13 @@ 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)){
|
||||
if (!jsonObject.get("statusCode").equals(0)) {
|
||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user