1 changed files with 305 additions and 0 deletions
@ -0,0 +1,305 @@ |
|||
package com.zc.business.controller; |
|||
|
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ruoyi.common.core.controller.BaseController; |
|||
import com.ruoyi.common.core.domain.AjaxResult; |
|||
import com.zc.common.core.httpclient.OkHttp; |
|||
import com.zc.common.core.httpclient.exception.HttpException; |
|||
import com.zc.common.core.httpclient.request.RequestParams; |
|||
import okhttp3.Response; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import static com.ruoyi.common.constant.Constants.HTTP; |
|||
|
|||
|
|||
/** |
|||
* License |
|||
* |
|||
* @author Athena-xiepufeng |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/video") |
|||
public class VideoController extends BaseController { |
|||
|
|||
private static final String USERNAME = "jhgskj"; |
|||
private static final String PASSWORD = "jhgskj@2023"; |
|||
private static String TOKEN; |
|||
|
|||
private final static String URL = HTTP + "10.166.147.60:9021"; |
|||
|
|||
public static final Integer UNAUTHORIZED = 401; |
|||
|
|||
|
|||
public static final Integer ERROR = 500; |
|||
|
|||
/** |
|||
* 查询附近相机 |
|||
*/ |
|||
@GetMapping(value = "/nearCamListDistance") |
|||
public JSONObject nearCamListDistance(String devLat, String devLon) throws HttpException, IOException { |
|||
|
|||
JSONObject jsonResult = null; |
|||
|
|||
// 1000 米
|
|||
String distance = "1000"; |
|||
|
|||
OkHttp okHttp = new OkHttp(); |
|||
|
|||
RequestParams requestParams = new RequestParams(); |
|||
requestParams.put("devLat", devLat); |
|||
requestParams.put("devLon", devLon); |
|||
requestParams.put("distance", distance); |
|||
|
|||
Map<String, String> header = new HashMap<>(); |
|||
|
|||
if (VideoController.TOKEN == null) { |
|||
getToken(); |
|||
} |
|||
|
|||
header.put("Authorization", TOKEN); |
|||
|
|||
Response response // 请求响应
|
|||
= okHttp |
|||
.headers(header) |
|||
.url(URL + "/videoInfo/api/nearCamListDistance") // 请求地址
|
|||
.data(requestParams) // 请求参数
|
|||
.get(); // 请求方法
|
|||
|
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
|
|||
if (jsonResult.containsKey("code") && UNAUTHORIZED.equals(jsonResult.getInteger("code"))) { |
|||
getToken(); |
|||
okHttp.get(); |
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return jsonResult; |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 根据桩号查询附近相机 |
|||
*/ |
|||
@GetMapping(value = "/nearCamListPileNum") |
|||
public Object nearCamListPileNum(String pileNum) throws HttpException, IOException { |
|||
|
|||
// 获取济菏运管中心相机信息
|
|||
JSONObject camInfo = getCamByDept("1301730"); |
|||
|
|||
if (!camInfo.containsKey("data")) { |
|||
return camInfo; |
|||
} |
|||
|
|||
JSONArray camData = camInfo.getJSONArray("data"); |
|||
|
|||
List<Object> resultList = new ArrayList<>(); |
|||
|
|||
camData.forEach(item -> { |
|||
JSONObject jsonObject = (JSONObject) item; |
|||
if (isNearbyPileNum(pileNum, jsonObject.getString("pileNum"))) { |
|||
resultList.add(item); |
|||
} |
|||
}); |
|||
|
|||
return AjaxResult.success(resultList); |
|||
} |
|||
|
|||
/** |
|||
* 获取视频流信息 |
|||
*/ |
|||
@GetMapping(value = "/externalVideoStreaming") |
|||
public JSONObject externalVideoStreaming(String camId) throws HttpException, IOException { |
|||
|
|||
JSONObject jsonResult = null; |
|||
|
|||
OkHttp okHttp = new OkHttp(); |
|||
|
|||
RequestParams requestParams = new RequestParams(); |
|||
requestParams.put("type", "1"); |
|||
requestParams.put("camId", camId); |
|||
|
|||
Map<String, String> header = new HashMap<>(); |
|||
|
|||
if (VideoController.TOKEN == null) { |
|||
getToken(); |
|||
} |
|||
|
|||
header.put("Authorization", TOKEN); |
|||
|
|||
Response response // 请求响应
|
|||
= okHttp |
|||
.headers(header) |
|||
.url(URL + "/videoInfo/api/externalVideoStreaming") // 请求地址
|
|||
.data(requestParams) // 请求参数
|
|||
.post(); // 请求方法
|
|||
|
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
|
|||
if (jsonResult.containsKey("code") && UNAUTHORIZED.equals(jsonResult.getInteger("code"))) { |
|||
getToken(); |
|||
okHttp.post(); |
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return jsonResult; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 云平台控制 |
|||
*/ |
|||
@GetMapping(value = "/PTZControl") |
|||
public JSONObject PTZControl(String camId, String cmdType, String speed) throws HttpException, IOException { |
|||
|
|||
JSONObject jsonResult = null; |
|||
|
|||
OkHttp okHttp = new OkHttp(); |
|||
|
|||
RequestParams requestParams = new RequestParams(); |
|||
requestParams.put("msgType", "3"); |
|||
requestParams.put("camId", camId); |
|||
requestParams.put("cmdType", cmdType); |
|||
requestParams.put("speed", speed); |
|||
|
|||
Map<String, String> header = new HashMap<>(); |
|||
|
|||
if (VideoController.TOKEN == null) { |
|||
getToken(); |
|||
} |
|||
|
|||
header.put("Authorization", TOKEN); |
|||
|
|||
Response response // 请求响应
|
|||
= okHttp |
|||
.headers(header) |
|||
.url(URL + "/videoInfo/api/PTZControl") // 请求地址
|
|||
.data(requestParams) // 请求参数
|
|||
.get(); // 请求方法
|
|||
|
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
|
|||
if (jsonResult.containsKey("code") && UNAUTHORIZED.equals(jsonResult.getInteger("code"))) { |
|||
getToken(); |
|||
okHttp.get(); |
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return jsonResult; |
|||
} |
|||
|
|||
|
|||
public void getToken() throws HttpException, IOException { |
|||
|
|||
OkHttp okHttp = new OkHttp(); |
|||
|
|||
RequestParams requestParams = new RequestParams(); |
|||
requestParams.put("username", USERNAME); |
|||
requestParams.put("password", PASSWORD); |
|||
|
|||
Response response // 请求响应
|
|||
= okHttp |
|||
.url(URL + "/apiLogin") // 请求地址
|
|||
.data(requestParams) // 请求参数
|
|||
.post(); // 请求方法
|
|||
|
|||
if (response.body() != null) { |
|||
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|||
if (jsonResult.containsKey("token")) { |
|||
VideoController.TOKEN = jsonResult.getString("token"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 根据组织机构获取摄像机信息 |
|||
* @param deptId 机构id |
|||
* @return |
|||
*/ |
|||
public JSONObject getCamByDept(String deptId) throws HttpException, IOException { |
|||
JSONObject jsonResult = null; |
|||
|
|||
OkHttp okHttp = new OkHttp(); |
|||
|
|||
RequestParams requestParams = new RequestParams(); |
|||
requestParams.put("deptId", deptId); |
|||
|
|||
Map<String, String> header = new HashMap<>(); |
|||
|
|||
if (VideoController.TOKEN == null) { |
|||
getToken(); |
|||
} |
|||
|
|||
header.put("Authorization", TOKEN); |
|||
|
|||
Response response // 请求响应
|
|||
= okHttp |
|||
.headers(header) |
|||
.url(URL + "/system/camera/camList") // 请求地址
|
|||
.data(requestParams) // 请求参数
|
|||
.get(); // 请求方法
|
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
|
|||
if (jsonResult.containsKey("code") && UNAUTHORIZED.equals(jsonResult.getInteger("code"))) { |
|||
getToken(); |
|||
okHttp.get(); |
|||
if (response.body() != null) { |
|||
jsonResult = JSONObject.parseObject(response.body().string()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return jsonResult; |
|||
} |
|||
|
|||
/** |
|||
* 判断是否是附近桩号 |
|||
* @return |
|||
*/ |
|||
private boolean isNearbyPileNum(String centralPileNum, String nearbyPileNum) { |
|||
|
|||
int centralPileNumMetre = pileNumTransformMetre(centralPileNum); |
|||
int nearbyPileNumMetre = pileNumTransformMetre(nearbyPileNum); |
|||
|
|||
return (nearbyPileNumMetre <= centralPileNumMetre + 1000) && (nearbyPileNumMetre >= centralPileNumMetre - 1000); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 转换转换成米 |
|||
* @param pileNum 桩号 |
|||
* @return |
|||
*/ |
|||
private int pileNumTransformMetre(String pileNum) { |
|||
String[] parts = pileNum.split("[+ ]"); |
|||
if (parts.length < 2) { |
|||
return 0; |
|||
} |
|||
int kilometer = Integer.parseInt(parts[0].substring(1)); // 移除开头的字母
|
|||
int meter = Integer.parseInt(parts[1]); |
|||
return kilometer * 1000 + meter; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue