You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.3 KiB
132 lines
5.3 KiB
package com.zc.business.controller;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import com.zc.business.enums.UniversalEnum;
|
|
import com.zc.common.core.httpclient.OkHttp;
|
|
import com.zc.common.core.httpclient.exception.HttpException;
|
|
import com.zc.common.core.httpclient.request.RequestParams;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import okhttp3.Response;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.io.IOException;
|
|
import java.net.SocketTimeoutException;
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
* License
|
|
*
|
|
* @author Athena-zhaoxianglong
|
|
*/
|
|
@Api(tags = "广播接口")
|
|
@RestController
|
|
@RequestMapping("/broadcast")
|
|
public class BroadcastController extends BaseController {
|
|
|
|
|
|
private static int numberOfReconnections = UniversalEnum.ONE.getNumber();
|
|
@Resource
|
|
private RedisCache redisCache;
|
|
@Autowired
|
|
private ISysConfigService configService;
|
|
//private static final String ID = "admin";
|
|
//
|
|
//private static final String SECRET = "21232f297a57a5a743894a0e4a801fc3";
|
|
//
|
|
//private static final String CALLBACKURL = "http://10.0.111.11/broadcast/logIn";
|
|
//
|
|
//private final static String URL = HTTP + "10.0.81.106/api/REST-API/";
|
|
//
|
|
//private final static String TOKENKEY = "tokenRoadTestBroadcastPlatform";
|
|
|
|
///**
|
|
// * 广播平台事件回调函数
|
|
// *
|
|
// * @param returnParameters 事件回参
|
|
// * @return 更新结果
|
|
// */
|
|
//@PostMapping(value = "/event/{event}")
|
|
//public AjaxResult logIn(@PathVariable String event, HashMap<String, Object> returnParameters) {
|
|
// Object accessToken = returnParameters.get("accessToken");
|
|
// System.out.println(accessToken);
|
|
// System.out.println("广播回调事件函数");
|
|
//
|
|
//
|
|
//
|
|
// return null;
|
|
//}
|
|
|
|
/*
|
|
* 调用功能
|
|
* */
|
|
@ApiOperation("广播功能调用")
|
|
@PostMapping(value = "/broadcastFunctionCall")
|
|
public JSONObject nearCamListDistance(@RequestBody JSONObject params) throws HttpException, IOException {
|
|
JSONObject broadcast = JSONObject.parseObject(configService.selectConfigByKey("broadcast"));
|
|
JSONObject jsonResult = null;
|
|
OkHttp okHttp = new OkHttp(UniversalEnum.ONE.getNumber());
|
|
RequestParams requestParams = new RequestParams(params);
|
|
String tokenRoadTestBroadcastPlatform = redisCache.getCacheObject(broadcast.getString("TOKENKEY"));
|
|
if (tokenRoadTestBroadcastPlatform == null) {
|
|
tokenRoadTestBroadcastPlatform = getToken();
|
|
}
|
|
try {
|
|
Response response // 请求响应
|
|
= okHttp
|
|
.headers(new HashMap<>())
|
|
.url(broadcast.getString("URL") + params.getString("functionType") + ".do?accessToken=" + tokenRoadTestBroadcastPlatform) // 请求地址
|
|
.data(requestParams) // 请求参数
|
|
.post(); // 请求方法
|
|
if (response.body() != null) {
|
|
jsonResult = JSONObject.parseObject(response.body().string());
|
|
}
|
|
} catch (SocketTimeoutException e) {
|
|
if (numberOfReconnections < UniversalEnum.THREE.getNumber()) {
|
|
numberOfReconnections += UniversalEnum.ONE.getNumber();
|
|
getToken();
|
|
return nearCamListDistance(params);
|
|
} else {
|
|
jsonResult = new JSONObject();
|
|
jsonResult.put("code", UniversalEnum.FOUR_HUNDRED.getValue());
|
|
jsonResult.put("msg", UniversalEnum.BROADCAST_FOUR_HUNDRED.getValue());
|
|
}
|
|
}
|
|
numberOfReconnections = UniversalEnum.ZERO.getNumber();
|
|
return jsonResult;
|
|
}
|
|
|
|
public String getToken() throws HttpException, IOException {
|
|
JSONObject broadcast = JSONObject.parseObject(configService.selectConfigByKey("broadcast"));
|
|
|
|
OkHttp okHttp = new OkHttp();
|
|
RequestParams requestParams = new RequestParams();
|
|
requestParams.put("id", broadcast.getString("ID"));
|
|
requestParams.put("secret", broadcast.getString("SECRET"));
|
|
requestParams.put("callbackUrl", broadcast.getString("CALLBACKURL"));
|
|
Response response // 请求响应
|
|
= okHttp
|
|
.headers(new HashMap<>())
|
|
.url(UniversalEnum.BROADCAST_LOG_URL.getValue()) // 请求地址
|
|
//.url("http://10.0.81.106/api/REST-API/login.do") // 请求地址
|
|
.data(requestParams) // 请求参数
|
|
.post(); // 请求方法
|
|
if (response.body() != null) {
|
|
String accessToken = JSONObject.parseObject(response.body().string()).getString("accessToken");
|
|
redisCache.setCacheObject(broadcast.getString("TOKENKEY"), accessToken, UniversalEnum.FIVE.getNumber(), TimeUnit.MINUTES);
|
|
return accessToken;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|