Browse Source

3800转1800

master
王寅 8 months ago
parent
commit
d29bfdb44b
3 changed files with 178 additions and 106 deletions
  1. +11
    -4
      inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java
  2. +114
    -84
      inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java
  3. +53
    -18
      inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java

+ 11
- 4
inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java View File

@ -15,6 +15,7 @@ import com.inspect.ivs.vo.IvsPresetVo;
import com.inspect.ivs.vo.IvsPtzVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -26,7 +27,8 @@ public class IvsControlController {
private final RedisService redisService;
private final IvsCommonService ivsCommonService;
@Value("${ivs.version:3800}")
private String version;
public IvsControlController(
RedisService redisService,
IvsCommonService ivsCommonService) {
@ -73,7 +75,12 @@ public class IvsControlController {
} else if (command.equals("goto")) {
JSONObject paramJson = new JSONObject();
paramJson.put("cameraCode", ivsPresetVo.getCameraCode());
paramJson.put("controlCode", "11");
log.debug("============================VERSION {}", version);
if(version.equals("1800")){
paramJson.put("controlCode", 11);
} else {
paramJson.put("controlCode", "11");
}
paramJson.put("controlPara1", ivsPresetVo.getPreset());
paramJson.put("controlPara2", "");
ivsCommonService.postJson(paramJson, IvsConst.URI_PTZ_CONTROL, IvsResult.class);
@ -96,9 +103,9 @@ public class IvsControlController {
@GetMapping({"presetList"})
public IvsPresetListView presetList(IvsVo ivsVo) {
log.info("[IVS] PRESET LIST: {}", ivsVo);
log.debug("[IVS] PRESET LIST: {}", ivsVo);
IvsPresetListView ivsPresetListView = ivsCommonService.get(UriUtils.parseForReplace(IvsConst.URI_PRESET_LIST, ivsVo), IvsPresetListView.class);
log.info("[IVS] END");
log.debug("[IVS] END");
return ivsPresetListView;
}
}

+ 114
- 84
inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java View File

@ -1,5 +1,6 @@
package com.inspect.ivs.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.constant.Color;
import com.inspect.base.core.domain.Response;
@ -14,9 +15,9 @@ import com.inspect.ivs.vo.IvsChanSnapVo;
import com.inspect.ivs.vo.IvsDevChanListVo;
import com.inspect.ivs.vo.IvsDevChanSnapVo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
@ -33,6 +34,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -45,7 +48,8 @@ public class IvsDeviceController {
@Value("${spring.profiles.active}")
private String activeProfile;
@Value("${ivs.version:3800}")
private String version;
private final IvsCommonService ivsCommonService;
public IvsDeviceController(IvsCommonService ivsCommonService) {
@ -54,53 +58,68 @@ public class IvsDeviceController {
@GetMapping({"channelsnap"})
public ResponseEntity<InputStreamResource> channelSnap(IvsDevChanSnapVo ivsDevChanSnapVo) throws Exception {
int index = 0;
final int TRY_TIMES = 3;
final long mDelay = 3000;
while (index < TRY_TIMES) {
log.info(Color.MAGENTA + "[IVS] SNAPSHOT: {}, TRY TIMES: {}" + Color.END, ivsDevChanSnapVo.getCameraCode(), index);
try {
IvsSnapshotView ivsSnapshotView = ivsCommonService.get(UriUtils.parse(IvsConst.URI_PLATFORM_SNAPSHOT, ivsDevChanSnapVo), IvsSnapshotView.class);
IvsChanSnapVo ivsChanSnapVo = new IvsChanSnapVo(ivsDevChanSnapVo.getCameraCode(),
ivsDevChanSnapVo.getDomainCode(),
ivsSnapshotView.getTaskID()
);
if ("prod".equals(activeProfile)) {
log.info("[IVS] prod env get stream delay 9000ms");
Thread.sleep(9000L);
} else {
Thread.sleep(2000L);
}
if (version.equals("1800")) {
byte[] bytes = Ivs1800channelSnap(ivsDevChanSnapVo);
String filePath = "/home/1.JPG"; // 替换为你想保存图片的本地路径
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(bytes);
log.info("图片已成功保存到本地:" + filePath);
} catch (IOException e) {
e.printStackTrace();
log.info("保存图片时出现错误:" + e.getMessage());
}
// byte[] bytes = readStream(content);
InputStream inputStream = new ByteArrayInputStream(bytes);
return ResponseEntity.ok().body(new InputStreamResource(inputStream));
} else {
int index = 0;
final int TRY_TIMES = 3;
final long mDelay = 3000;
while (index < TRY_TIMES) {
log.info(Color.MAGENTA + "[IVS] SNAPSHOT: {}, TRY TIMES: {}" + Color.END, ivsDevChanSnapVo.getCameraCode(), index);
try {
IvsSnapshotView ivsSnapshotView = ivsCommonService.get(UriUtils.parse(IvsConst.URI_PLATFORM_SNAPSHOT, ivsDevChanSnapVo), IvsSnapshotView.class);
IvsChanSnapVo ivsChanSnapVo = new IvsChanSnapVo(ivsDevChanSnapVo.getCameraCode(),
ivsDevChanSnapVo.getDomainCode(),
ivsSnapshotView.getTaskID()
);
if ("prod".equals(activeProfile)) {
log.info("[IVS] prod env get stream delay 9000ms");
Thread.sleep(9000L);
} else {
Thread.sleep(2000L);
}
IvsPlatformSnapshotView snapShotView = ivsCommonService.postJson(
ivsChanSnapVo,
IvsConst.URI_SNAPSHOT_LIST,
IvsPlatformSnapshotView.class);
if (snapShotView != null && snapShotView.getSnapshotInfoList() != null
&& snapShotView.getSnapshotInfoList().getSnapshotInfos() != null
&& !snapShotView.getSnapshotInfoList().getSnapshotInfos().isEmpty()) {
String cameraCode = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getCameraCode();
String pictureUrl = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getPictureUrl();
log.info(Color.MAGENTA + "[IVS] TRYS: {}, SNAPSHOT cameraCode: {}, pictureUrl: {}" + Color.END, index, cameraCode, pictureUrl);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpResponse httpResponse = httpClient.execute(new HttpGet(pictureUrl));
InputStream content = httpResponse.getEntity().getContent();
byte[] bytes = readStream(content);
InputStream inputStream = new ByteArrayInputStream(bytes);
log.info(Color.MAGENTA + "[IVS] tries: {}, pictureUrl: {}, size: {} END" + Color.END, index, pictureUrl, bytes.length);
return ResponseEntity.ok().body(new InputStreamResource(inputStream));
IvsPlatformSnapshotView snapShotView = ivsCommonService.postJson(
ivsChanSnapVo,
IvsConst.URI_SNAPSHOT_LIST,
IvsPlatformSnapshotView.class);
if (snapShotView != null && snapShotView.getSnapshotInfoList() != null
&& snapShotView.getSnapshotInfoList().getSnapshotInfos() != null
&& !snapShotView.getSnapshotInfoList().getSnapshotInfos().isEmpty()) {
String cameraCode = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getCameraCode();
String pictureUrl = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getPictureUrl();
log.info(Color.MAGENTA + "[IVS] TRYS: {}, SNAPSHOT cameraCode: {}, pictureUrl: {}" + Color.END, index, cameraCode, pictureUrl);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpResponse httpResponse = httpClient.execute(new HttpGet(pictureUrl));
InputStream content = httpResponse.getEntity().getContent();
byte[] bytes = readStream(content);
InputStream inputStream = new ByteArrayInputStream(bytes);
log.info(Color.MAGENTA + "[IVS] tries: {}, pictureUrl: {}, size: {} END" + Color.END, index, pictureUrl, bytes.length);
return ResponseEntity.ok().body(new InputStreamResource(inputStream));
}
}
} catch (Exception e) {
log.warn(Color.MAGENTA + "[IVS] SNAPSHOT EXCEPTION: {}" + Color.END, e.getMessage());
}
} catch (Exception e) {
log.warn(Color.MAGENTA + "[IVS] SNAPSHOT EXCEPTION: {}" + Color.END, e.getMessage());
}
index++;
Thread.sleep(mDelay);
index++;
Thread.sleep(mDelay);
}
return ResponseEntity.ok().body(null);
}
return ResponseEntity.ok().body(null);
}
public byte[] readStream(InputStream inStream) throws Exception {
@ -115,6 +134,17 @@ public class IvsDeviceController {
return outStream.toByteArray();
}
public byte[] Ivs1800channelSnap(IvsDevChanSnapVo ivsDevChanSnapVo) throws Exception {
log.info("++++++++++++++++++++++++执行1800channelsnap截图接口++++++++++++++++++++++++++++++");
String url = "https://172.27.144.61:18531/snapshot/manualsnapshot";
Map<String, Object> controlMap = new HashMap<>();
controlMap.put("cameraCode", ivsDevChanSnapVo.getCameraCode());
String requestMsg = JSON.toJSONString(controlMap);
// byte[] bytes = ivsCommonService.sendSSLPost(url, requestMsg);
// log.info("++++++++++++++++++++++++执行1800channelsnap截图接口++++++++++++++++++++++++++++++ :{}"+bytes);
return ivsCommonService.sendSSLPost(url, requestMsg);
}
// @GetMapping({"channelsnap"})
// public Response channelSnap(IvsDevChanSnapVo sipbDeviceChannelSnapVo, HttpServletResponse httpServletResponse) throws Exception {
@ -184,42 +214,42 @@ public class IvsDeviceController {
return sum;
}
public static void main(String[] args) {
Timer timer = new Timer();
// 创建一个Callable任务
Callable<Integer> task = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 1; i <= 10; i++) {
System.out.println("i:" + i);
sum += i;
}
return sum;
}
};
// 将Callable任务包装成FutureTask
FutureTask<Integer> futureTask = new FutureTask<>(task);
// 设置定时任务
timer.schedule(new TimerTask() {
@Override
public void run() {
futureTask.run();
}
}, 1000);
try {
// 获取定时任务的返回结果
int result = futureTask.get();
System.out.println("定时任务的返回结果是:" + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
timer.cancel();
System.out.println("定时任务的返回结果是");
}
// public static void main(String[] args) {
// Timer timer = new Timer();
//
// // 创建一个Callable任务
// Callable<Integer> task = new Callable<Integer>() {
// @Override
// public Integer call() throws Exception {
// int sum = 0;
// for (int i = 1; i <= 10; i++) {
// System.out.println("i:" + i);
// sum += i;
// }
// return sum;
// }
// };
//
// // 将Callable任务包装成FutureTask
// FutureTask<Integer> futureTask = new FutureTask<>(task);
//
// // 设置定时任务
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// futureTask.run();
// }
// }, 1000);
//
// try {
// // 获取定时任务的返回结果
// int result = futureTask.get();
// System.out.println("定时任务的返回结果是:" + result);
// } catch (InterruptedException | ExecutionException e) {
// e.printStackTrace();
// }
//
// timer.cancel();
// System.out.println("定时任务的返回结果是");
// }
}

+ 53
- 18
inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java View File

@ -8,10 +8,15 @@ import com.inspect.base.redis.service.RedisService;
import com.inspect.ivs.base.feign.domain.IvsResult;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.domain.ErrorCode;
import com.inspect.ivs.util.SslUtils;
import com.inspect.ivs.util.WebClientUtils;
import java.net.HttpCookie;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpResponse;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -23,6 +28,9 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import static cn.hutool.poi.excel.sax.AttributeName.t;
@Component
public class IvsCommonService {
@ -59,33 +67,22 @@ public class IvsCommonService {
}
public <T> T get(String uri, Class<T> clazz) {
// String cookie = this.getCookie();
// log.info(Color.GREEN + "[IVS] GET cookie: {}, ivs address: {}" + Color.END, cookie, getAddress().concat(uri));
// String res = HttpUtil.createGet(getAddress().concat(uri))
// .cookie(new HttpCookie[]{new HttpCookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie)})
// .execute()
// .body();
// return checkResultCodeAndConvertResult(JSONObject.parseObject(res), clazz);
// qinyl modify for adapting zte ivs server
JSONObject jsonObject = this.getResultJson(uri);
String cookie = this.getCookie();
String res = HttpUtil.createGet(getAddress().concat(uri))
.cookie(new HttpCookie[]{new HttpCookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie)})
.execute()
.body();
//log.info("[IVS] GET URI: {}, RES: {}", uri, res);
return checkResultCodeAndConvertResult(jsonObject, clazz);
return checkResultCodeAndConvertResult(JSONObject.parseObject(res), clazz);
}
public JSONObject getResultJson(String uri) {
String cookie = this.getCookie();
log.info(Color.GREEN + "[IVS] GET cookie: {}, ivs address: {}" + Color.END, cookie, getAddress().concat(uri));
JSONObject result = this.http(HttpMethod.GET, uri)
.cookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie)
.retrieve()
.bodyToMono(JSONObject.class)
.block();
if (result == null) {
log.info(Color.RED + "[IVS] getResultJson result is null!!!" + Color.END);
} else {
log.info(Color.GREEN + "[IVS] getResultJson result: {}" + Color.END, result.toJSONString());
}
String resultCode = result.getString(IvsConst.RESULT_CODE);
if (IvsConst.HTTP_STATUS_LOGIN_EXPIRE.equals(resultCode)) {
(new Thread(() -> {
@ -170,6 +167,44 @@ public class IvsCommonService {
return this.postJson(requestParam, uri, IvsResult.class);
}
public byte[] sendSSLPost(String url, String param) throws Exception {
String Cookie = this.getCookie();
StringBuilder result = new StringBuilder();
String urlNameString = url;
// 定义一个通用的连接对象
HttpURLConnection conn= null;
try {
URL console = new URL(urlNameString);
SslUtils.ignoreSsl();
conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// conn.setRequestProperty("Accept-Charset", "utf-8");
// conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Cookie", Cookie);
// conn.setDoOutput(true);
conn.setDoInput(true);
try (InputStream is = conn.getInputStream()){
return readStream(is);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject


Loading…
Cancel
Save