|
@@ -1,18 +1,26 @@
|
|
|
package com.its.op.service.its.vds;
|
|
|
|
|
|
+import com.its.op.config.VdsServerConfig;
|
|
|
import com.its.op.dao.repository.its.vds.TbVdsCtlrRepository;
|
|
|
import com.its.op.dto.its.vds.VdsControlDto;
|
|
|
import com.its.op.entity.its.vds.TbVdsCtlr;
|
|
|
import com.its.op.xnettcp.client.VdsCommClientService;
|
|
|
+import com.its.utils.ItsUtils;
|
|
|
import com.its.utils.SysUtils;
|
|
|
-import io.netty.channel.Channel;
|
|
|
-import io.netty.channel.ChannelFuture;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.net.Socket;
|
|
|
+import java.net.SocketAddress;
|
|
|
import java.nio.ByteBuffer;
|
|
|
import java.nio.ByteOrder;
|
|
|
+import java.util.Base64;
|
|
|
import java.util.NoSuchElementException;
|
|
|
import java.util.Optional;
|
|
|
|
|
@@ -22,6 +30,7 @@ import java.util.Optional;
|
|
|
public class VdsControlService {
|
|
|
|
|
|
private final VdsCommClientService vdsCommClientService;
|
|
|
+ private final VdsServerConfig vdsServerConfig;
|
|
|
private final TbVdsCtlrRepository repo;
|
|
|
|
|
|
private static final int crc16_arc[] = {
|
|
@@ -165,7 +174,7 @@ public class VdsControlService {
|
|
|
public VdsControlDto.VdsControlRes controlReset(String id, VdsControlDto.VdsControlResetReq req) {
|
|
|
TbVdsCtlr vds = this.requireOne(id);
|
|
|
ByteBuffer cmdBuffer = getCommand((byte)0x0C, 0, Integer.valueOf(id), 0, 0);
|
|
|
- return requestVdsCommand(cmdBuffer);
|
|
|
+ return requestVdsCommand(cmdBuffer, 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -177,32 +186,82 @@ public class VdsControlService {
|
|
|
public VdsControlDto.VdsControlRes requestStopImage(String id, VdsControlDto.VdsControlStopImageReq req) {
|
|
|
TbVdsCtlr vds = this.requireOne(id);
|
|
|
ByteBuffer cmdBuffer = getCommand((byte)0x16, 0, Integer.valueOf(id), req.getFrameNo(), req.getCameraNo());
|
|
|
- return requestVdsCommand(cmdBuffer);
|
|
|
+ return requestVdsCommand(cmdBuffer, 1);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* VDS 통신서버로 요청 메시지 전송
|
|
|
* @param cmdBuffer
|
|
|
+ * @param cmdType
|
|
|
* @return
|
|
|
*/
|
|
|
- public VdsControlDto.VdsControlRes requestVdsCommand(ByteBuffer cmdBuffer) {
|
|
|
+ public VdsControlDto.VdsControlRes requestVdsCommand(ByteBuffer cmdBuffer, int cmdType) {
|
|
|
VdsControlDto.VdsControlRes result = new VdsControlDto.VdsControlRes(0, "success");
|
|
|
if (cmdBuffer == null) {
|
|
|
result.setResult(2, "VDS 통신 메시지 생성 중 오류가 발생하였습니다.");
|
|
|
return result;
|
|
|
}
|
|
|
- Channel channel = this.vdsCommClientService.getVdsClient().getChannel();
|
|
|
- if (channel == null || !channel.isActive()) {
|
|
|
+log.error("xxxxxxxxxxxxxxxxxxxxxxxx");
|
|
|
+ String saveDir = ItsUtils.createUserDir("/image/vds/");
|
|
|
+ //String saveFileName = saveDir + ItsUtils.getSysTime() + ".jpg";
|
|
|
+ String saveFileName = saveDir + "vdsImage" + ".jpg";
|
|
|
+log.error("{}", saveFileName);
|
|
|
+ String ipAddress = this.vdsServerConfig.getIpAddress();
|
|
|
+ int port = this.vdsServerConfig.getPort();
|
|
|
+ int connectTimeout = 3000; // milli-seconds
|
|
|
+ int readTimeout = 5000; // milli-seconds
|
|
|
+ SocketAddress socketAddress = new InetSocketAddress(ipAddress, port);
|
|
|
+ Socket socket = null;
|
|
|
+ if (cmdType == 1) {
|
|
|
+ byte[] fileContent = new byte[0];
|
|
|
+ try {
|
|
|
+ fileContent = FileUtils.readFileToByteArray(new File(saveFileName));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("{}", e.getMessage());
|
|
|
+ }
|
|
|
+ String image = Base64.getEncoder().encodeToString(fileContent);
|
|
|
+ result.setImage(image);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ socket = new Socket();
|
|
|
+ socket.setSoTimeout(readTimeout); /* InputStream 에서 데이터읽을때의 timeout */
|
|
|
+ socket.connect(socketAddress, connectTimeout); /* socket 연결 자체에대한 timeout */
|
|
|
+
|
|
|
+ byte[] data = cmdBuffer.array();
|
|
|
+ OutputStream os = socket.getOutputStream();
|
|
|
+ os.write(data);
|
|
|
+ os.flush();
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ log.error("X. {}, {}, {}", ipAddress, port, e.getMessage());
|
|
|
result.setResult(3, "VDS 서버와 통신이 실패하였습니다.");
|
|
|
return result;
|
|
|
}
|
|
|
- ChannelFuture f = channel.writeAndFlush(cmdBuffer);
|
|
|
- f.awaitUninterruptibly();
|
|
|
- if (f.isDone() || f.isSuccess()) {
|
|
|
- result.setResult(0, "VDS 서버에 요청 메시지를 정상적으로 전송하였습니다.");
|
|
|
- } else {
|
|
|
- result.setResult(4, "VDS 서버에 요청 메시지 전송이 실패하였습니다.");
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (socket != null) {
|
|
|
+ socket.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ log.error("VdsControlService: requestVdsCommand, socket close IOException");
|
|
|
+ }
|
|
|
}
|
|
|
+//
|
|
|
+// Channel channel = this.vdsCommClientService.getVdsClient().getChannel();
|
|
|
+// if (channel == null || !channel.isActive()) {
|
|
|
+// result.setResult(3, "VDS 서버와 통신이 실패하였습니다.");
|
|
|
+// return result;
|
|
|
+// }
|
|
|
+// ChannelFuture f = channel.writeAndFlush(cmdBuffer);
|
|
|
+// f.awaitUninterruptibly();
|
|
|
+// if (f.isDone() || f.isSuccess()) {
|
|
|
+// result.setResult(0, "VDS 서버에 요청 메시지를 정상적으로 전송하였습니다.");
|
|
|
+// } else {
|
|
|
+// result.setResult(4, "VDS 서버에 요청 메시지 전송이 실패하였습니다.");
|
|
|
+// }
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
}
|