远程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";
|
private static final String AR_HEALTH_CHECK = "zyp_ar_health_check";
|
||||||
|
|
||||||
@XxlJob("zyp-ar-cloud-refresh-token")
|
@XxlJob("zyp-ar-cloud-refresh-token")
|
||||||
public void arCloudRefreshToken(){
|
public void arCloudRefreshToken() {
|
||||||
|
|
||||||
Long timestamp = System.currentTimeMillis();
|
Long timestamp = System.currentTimeMillis();
|
||||||
|
|
||||||
@@ -48,15 +48,21 @@ public class ARRemoteCloudJob {
|
|||||||
|
|
||||||
String sign = getSign(param, ARConstant.AR_APP_SECRET);
|
String sign = getSign(param, ARConstant.AR_APP_SECRET);
|
||||||
|
|
||||||
String response = arRemoteClient.getToken(
|
String response;
|
||||||
new ARGetTokenRequest()
|
try {
|
||||||
.setApiKey(ARConstant.AR_APP_KEY)
|
response = arRemoteClient.getToken(
|
||||||
.setExpires(ARConstant.EXPIRES_TIME)
|
new ARGetTokenRequest()
|
||||||
.setTimestamp(timestamp)
|
.setApiKey(ARConstant.AR_APP_KEY)
|
||||||
.setSignature(sign)
|
.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);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别获取token失败" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别获取token失败" : msg);
|
||||||
}
|
}
|
||||||
@@ -67,42 +73,47 @@ public class ARRemoteCloudJob {
|
|||||||
.eq(Param::getParamKey, "zyp.ar.auth.token")
|
.eq(Param::getParamKey, "zyp.ar.auth.token")
|
||||||
.last("limit 1")
|
.last("limit 1")
|
||||||
);
|
);
|
||||||
if (!paramService.updateById(paramOne.setParamValue( token))){
|
if (!paramService.updateById(paramOne.setParamValue(token))) {
|
||||||
throw new RuntimeException("更新ar云识别token失败");
|
throw new RuntimeException("更新ar云识别token失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@XxlJob("zyp-ar-cloud-health-check")
|
@XxlJob("zyp-ar-cloud-health-check")
|
||||||
public void arHealthCheck(){
|
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 result = response.getResult().getMessage().equals("pong");
|
||||||
Boolean cacheResult = (Boolean) redisService.get(AR_HEALTH_CHECK);
|
Boolean cacheResult = (Boolean) redisService.get(AR_HEALTH_CHECK);
|
||||||
if (cacheResult == null){
|
if (cacheResult == null) {
|
||||||
Param paramOne = paramService.getOne(
|
Param paramOne = paramService.getOne(
|
||||||
Wrappers.lambdaQuery(Param.class)
|
Wrappers.lambdaQuery(Param.class)
|
||||||
.eq(Param::getParamKey, "ar.is.open")
|
.eq(Param::getParamKey, "ar.is.open")
|
||||||
.last("limit 1")
|
.last("limit 1")
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!paramService.updateById(paramOne.setParamValue( String.valueOf( result)))){
|
if (!paramService.updateById(paramOne.setParamValue(String.valueOf(result)))) {
|
||||||
throw new RuntimeException("更新ar云识别token失败");
|
throw new RuntimeException("更新ar云识别token失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
redisService.set(AR_HEALTH_CHECK, result);
|
redisService.set(AR_HEALTH_CHECK, result);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!cacheResult.equals(result)){
|
if (!cacheResult.equals(result)) {
|
||||||
Param paramOne = paramService.getOne(
|
Param paramOne = paramService.getOne(
|
||||||
Wrappers.lambdaQuery(Param.class)
|
Wrappers.lambdaQuery(Param.class)
|
||||||
.eq(Param::getParamKey, "ar.is.open")
|
.eq(Param::getParamKey, "ar.is.open")
|
||||||
.last("limit 1")
|
.last("limit 1")
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!paramService.updateById(paramOne.setParamValue( String.valueOf( result)))){
|
if (!paramService.updateById(paramOne.setParamValue(String.valueOf(result)))) {
|
||||||
throw new RuntimeException("更新ar云识别token失败");
|
throw new RuntimeException("更新ar云识别token失败");
|
||||||
}
|
}
|
||||||
redisService.set(AR_HEALTH_CHECK, result);
|
redisService.set(AR_HEALTH_CHECK, result);
|
||||||
|
|||||||
@@ -52,21 +52,26 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
//hutool 生成随机字符串
|
//hutool 生成随机字符串
|
||||||
String nonceStr = RandomUtil.randomString(10);
|
String nonceStr = RandomUtil.randomString(10);
|
||||||
|
String response ;
|
||||||
|
try {
|
||||||
String response = arRemoteClient.createTarget(
|
response= arRemoteClient.createTarget(
|
||||||
token , new ARCreateTargetRequest()
|
token , new ARCreateTargetRequest()
|
||||||
.setImage(image)
|
.setImage(image)
|
||||||
.setActive(ARConstant.AR_IMAGE_ACTIVE)
|
.setActive(ARConstant.AR_IMAGE_ACTIVE)
|
||||||
.setName(nonceStr)
|
.setName(nonceStr)
|
||||||
.setSize(ARConstant.AR_IMAGE_DEFAULT_SIZE)
|
.setSize(ARConstant.AR_IMAGE_DEFAULT_SIZE)
|
||||||
.setMeta(image)
|
.setMeta(image)
|
||||||
.setType(ARClueEnum.AR_IMG_TYPE.getName())
|
.setType(ARClueEnum.AR_IMG_TYPE.getName())
|
||||||
.setTimestamp(timestamp)
|
.setTimestamp(timestamp)
|
||||||
.setAppId(ARConstant.AR_APP_ID)
|
.setAppId(ARConstant.AR_APP_ID)
|
||||||
.setAppKey(ARConstant.AR_APP_KEY)
|
.setAppKey(ARConstant.AR_APP_KEY)
|
||||||
// .setSignature(sign)
|
// .setSignature(sign)
|
||||||
);
|
);
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error("ar云识别创建目标失败", e);
|
||||||
|
throw new RuntimeException("ar云识别创建目标失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)){
|
||||||
@@ -81,14 +86,19 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
public Boolean deleteTargetInfo(String targetId) {
|
public Boolean deleteTargetInfo(String targetId) {
|
||||||
String token = getToken();
|
String token = getToken();
|
||||||
|
|
||||||
String targetInfo = findTargetInfo(targetId , token);
|
String targetInfo = findTargetInfo(targetId, token);
|
||||||
Assert.notNull(targetInfo, "未找到该图片信息");
|
Assert.notNull(targetInfo, "未找到该图片信息");
|
||||||
|
|
||||||
long timeInMillis = Calendar.getInstance().getTimeInMillis();
|
long timeInMillis = Calendar.getInstance().getTimeInMillis();
|
||||||
|
String result;
|
||||||
String result = arRemoteClient.delTargetInfo(getToken(), targetId , String.valueOf(timeInMillis), ARConstant.AR_APP_KEY, ARConstant.AR_APP_ID );
|
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);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
@@ -100,9 +110,15 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
private String findTargetInfo(String targetId , String token) {
|
private String findTargetInfo(String targetId , String token) {
|
||||||
long timeInMillis = Calendar.getInstance().getTimeInMillis();
|
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);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
@@ -114,24 +130,30 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
private Boolean targetDetection(String image , String token) {
|
private Boolean targetDetection(String image , String token) {
|
||||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
String response = arRemoteClient.targetDetection(
|
String response;
|
||||||
token , new ARDetectionTargetRequest()
|
try {
|
||||||
.setImage(image)
|
response = arRemoteClient.targetDetection(
|
||||||
.setTimestamp(timestamp)
|
token, new ARDetectionTargetRequest()
|
||||||
.setAppId(ARConstant.AR_APP_ID)
|
.setImage(image)
|
||||||
.setAppKey(ARConstant.AR_APP_KEY)
|
.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);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
Integer grade = JSONObject.parse(jsonObject.getString("result")).getInteger("grade");
|
Integer grade = JSONObject.parse(jsonObject.getString("result")).getInteger("grade");
|
||||||
if (grade == -1){
|
if (grade == -1) {
|
||||||
throw new RuntimeException("图像不符合规范或已损坏");
|
throw new RuntimeException("图像不符合规范或已损坏");
|
||||||
}
|
}
|
||||||
if (grade > 2){
|
if (grade > 2) {
|
||||||
throw new RuntimeException("评分太高,图片难以识别");
|
throw new RuntimeException("评分太高,图片难以识别");
|
||||||
}
|
}
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
@@ -141,16 +163,22 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
private List<String> targetSimilar(String image ,String token) {
|
private List<String> targetSimilar(String image ,String token) {
|
||||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
String response = arRemoteClient.targetSimilar(
|
String response;
|
||||||
token , new ARSimilarTargetRequest()
|
try {
|
||||||
.setImage(image)
|
response = arRemoteClient.targetSimilar(
|
||||||
.setTimestamp(timestamp)
|
token, new ARSimilarTargetRequest()
|
||||||
.setAppId(ARConstant.AR_APP_ID)
|
.setImage(image)
|
||||||
.setAppKey(ARConstant.AR_APP_KEY)
|
.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);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
@@ -167,34 +195,40 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
public String updateTarget(String image, String targetId) {
|
public String updateTarget(String image, String targetId) {
|
||||||
String token = getToken();
|
String token = getToken();
|
||||||
|
|
||||||
String targetInfo = findTargetInfo(targetId , token);
|
String targetInfo = findTargetInfo(targetId, token);
|
||||||
Assert.notNull(targetInfo, "未找到该图片信息");
|
Assert.notNull(targetInfo, "未找到该图片信息");
|
||||||
|
|
||||||
List<String> targetIdList = targetSimilar(image , token);
|
List<String> targetIdList = targetSimilar(image, token);
|
||||||
if (!targetIdList.isEmpty()){
|
if (!targetIdList.isEmpty()) {
|
||||||
throw new RuntimeException("当前图库里存在已有的类似图片");
|
throw new RuntimeException("当前图库里存在已有的类似图片");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!targetDetection(image , token)){
|
if (!targetDetection(image, token)) {
|
||||||
throw new RuntimeException("当前图片无法识别");
|
throw new RuntimeException("当前图片无法识别");
|
||||||
}
|
}
|
||||||
|
|
||||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
String response = arRemoteClient.updateTarget(
|
String response;
|
||||||
getToken() , new ARUpdateTargetRequest()
|
try {
|
||||||
.setImage(image)
|
response = arRemoteClient.updateTarget(
|
||||||
.setActive(ARConstant.AR_IMAGE_ACTIVE)
|
getToken(), new ARUpdateTargetRequest()
|
||||||
.setSize(ARConstant.AR_IMAGE_DEFAULT_SIZE)
|
.setImage(image)
|
||||||
.setMeta(image)
|
.setActive(ARConstant.AR_IMAGE_ACTIVE)
|
||||||
.setTimestamp(timestamp)
|
.setSize(ARConstant.AR_IMAGE_DEFAULT_SIZE)
|
||||||
.setAppId(ARConstant.AR_APP_ID)
|
.setMeta(image)
|
||||||
.setAppKey(ARConstant.AR_APP_KEY),
|
.setTimestamp(timestamp)
|
||||||
targetId
|
.setAppId(ARConstant.AR_APP_ID)
|
||||||
);
|
.setAppKey(ARConstant.AR_APP_KEY),
|
||||||
|
targetId
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("ar云识别更新图片失败", e);
|
||||||
|
throw new RuntimeException("ar云识别更新图片失败");
|
||||||
|
}
|
||||||
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
@@ -206,18 +240,24 @@ public class ARImgIdentifyServiceImpl implements ARImgIdentifyService {
|
|||||||
public String searchTarget(String image) {
|
public String searchTarget(String image) {
|
||||||
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
Long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
String response = arRemoteClient.searchTarget(
|
String response;
|
||||||
getToken() , new ARSearchTargetRequest()
|
try {
|
||||||
.setImage(image)
|
response = arRemoteClient.searchTarget(
|
||||||
.setNotracking("true")
|
getToken(), new ARSearchTargetRequest()
|
||||||
.setTimestamp(timestamp)
|
.setImage(image)
|
||||||
.setAppId(ARConstant.AR_APP_ID)
|
.setNotracking("true")
|
||||||
.setAppKey(ARConstant.AR_APP_KEY)
|
.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);
|
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||||
if (!jsonObject.get("statusCode").equals(0)){
|
if (!jsonObject.get("statusCode").equals(0)) {
|
||||||
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
String msg = ARCodeEnum.getMsg(jsonObject.getInteger("statusCode"));
|
||||||
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
throw new RuntimeException(msg == null ? "ar云识别错误" : msg);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user