|
|
|
package com.zc.business.controller;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
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.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.SocketTimeoutException;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import static com.ruoyi.common.constant.Constants.HTTP;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* License
|
|
|
|
*
|
|
|
|
* @author Athena-xiepufeng
|
|
|
|
*/
|
|
|
|
@Api(tags = "广播接口")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/broadcast")
|
|
|
|
public class BroadcastController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
|
private static int numberOfReconnections = 1;
|
|
|
|
@Resource
|
|
|
|
private RedisCache redisCache;
|
|
|
|
private static final String ID = "admin";
|
|
|
|
|
|
|
|
private static final String SECRET = "21232f297a57a5a743894a0e4a801fc3";
|
|
|
|
|
|
|
|
private static final String CALLBACKURL = "http://10.0.81.209/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("广播回调事件函数");
|
|
|
|
|
|
|
|
//todo
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 调用功能
|
|
|
|
* */
|
|
|
|
@ApiOperation("广播功能调用")
|
|
|
|
@PostMapping(value = "/broadcastFunctionCall")
|
|
|
|
public JSONObject nearCamListDistance(@RequestBody JSONObject params) throws HttpException, IOException {
|
|
|
|
JSONObject jsonResult = null;
|
|
|
|
OkHttp okHttp = new OkHttp(1);
|
|
|
|
RequestParams requestParams = new RequestParams(params);
|
|
|
|
String tokenRoadTestBroadcastPlatform = redisCache.getCacheObject(TOKENKEY);
|
|
|
|
if (tokenRoadTestBroadcastPlatform == null) {
|
|
|
|
tokenRoadTestBroadcastPlatform = getToken();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp
|
|
|
|
.headers(new HashMap<>())
|
|
|
|
.url(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 >3){
|
|
|
|
numberOfReconnections += 1;
|
|
|
|
getToken();
|
|
|
|
return nearCamListDistance(params);
|
|
|
|
}else {
|
|
|
|
jsonResult = new JSONObject();
|
|
|
|
jsonResult.put("code","400");
|
|
|
|
jsonResult.put("msg","语音广播连接错误");
|
|
|
|
return jsonResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return jsonResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken() throws HttpException, IOException {
|
|
|
|
|
|
|
|
OkHttp okHttp = new OkHttp();
|
|
|
|
RequestParams requestParams = new RequestParams();
|
|
|
|
requestParams.put("id", ID);
|
|
|
|
requestParams.put("secret", SECRET);
|
|
|
|
requestParams.put("callbackUrl", CALLBACKURL);
|
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp
|
|
|
|
.headers(new HashMap<>())
|
|
|
|
.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(TOKENKEY, accessToken, 5, TimeUnit.MINUTES);
|
|
|
|
return accessToken;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
///**
|
|
|
|
|
|
|
|
}
|