|
@@ -3,15 +3,18 @@ package com.its.api.its.service.cctv;
|
|
import com.its.api.its.model.dto.cctv.CctvControlDto;
|
|
import com.its.api.its.model.dto.cctv.CctvControlDto;
|
|
import com.its.api.its.model.entity.cctv.TbCctvCtlr;
|
|
import com.its.api.its.model.entity.cctv.TbCctvCtlr;
|
|
import com.its.api.its.repository.cctv.TbCctvCtlrRepository;
|
|
import com.its.api.its.repository.cctv.TbCctvCtlrRepository;
|
|
-import com.its.api.utils.SysUtils;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.*;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.DataOutputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.Base64;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -30,85 +33,124 @@ public class CctvControlService {
|
|
.orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
|
|
.orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
|
|
}
|
|
}
|
|
|
|
|
|
- public CctvControlDto.CctvControlPtzRes controlPtzProbeDigital(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
- CctvControlDto.CctvControlPtzRes result = new CctvControlDto.CctvControlPtzRes(req.getCommand(), req.getAction(), 0, "success");
|
|
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPresetProbeDigital(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPresetReq req) {
|
|
|
|
+ CctvControlDto.CctvControlRes result = new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 0, "success");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPtzProbeDigital(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
+ CctvControlDto.CctvControlRes result = new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 0, "success");
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public static class ZenoCommand {
|
|
|
|
- private byte[] cmd;
|
|
|
|
- public ZenoCommand(byte[] cmd) {
|
|
|
|
- this.cmd = cmd;
|
|
|
|
- }
|
|
|
|
- public byte[] getCmd() {
|
|
|
|
- if (this.cmd == null) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- byte cmd[] = new byte[this.cmd.length];
|
|
|
|
- System.arraycopy(this.cmd, 0, cmd, 0, this.cmd.length);
|
|
|
|
- return cmd;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * ZENO Preset control
|
|
|
|
+ * @param ipAddr
|
|
|
|
+ * @param userId
|
|
|
|
+ * @param userPswd
|
|
|
|
+ * @param req
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPresetZeno(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPresetReq req) {
|
|
|
|
+ CctvControlDto.CctvControlRes result = new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 0, "success");
|
|
|
|
+ byte[] controlData = this.zenoControl.getPresetCmd(req.getAction(), req.getNo(), req.getSpeed(), req.getTime(), req.getName());
|
|
|
|
+ if (controlData == null) {
|
|
|
|
+ result.setResult(9, "unknown command");
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
+ return requestHttpPostZeno(result, controlData, ipAddr, userId, userPswd);
|
|
}
|
|
}
|
|
|
|
|
|
- public byte[] getZenoCommand(CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
- return this.zenoControl.getCmd(req.getCommand(), req.getAction(), req.getSpeed());
|
|
|
|
|
|
+ /**
|
|
|
|
+ * ZENO PTZ control
|
|
|
|
+ * @param ipAddr
|
|
|
|
+ * @param userId
|
|
|
|
+ * @param userPswd
|
|
|
|
+ * @param req
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPtzZeno(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
+ CctvControlDto.CctvControlRes result = new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 0, "success");
|
|
|
|
+ byte[] controlData = this.zenoControl.getPtzCmd(req.getCommand(), req.getAction(), req.getSpeed());
|
|
|
|
+ if (controlData == null) {
|
|
|
|
+ result.setResult(9, "unknown command");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ return requestHttpPostZeno(result, controlData, ipAddr, userId, userPswd);
|
|
}
|
|
}
|
|
- public CctvControlDto.CctvControlPtzRes controlPtzZeno(String ipAddr, String userId, String userPswd, CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
- CctvControlDto.CctvControlPtzRes result = new CctvControlDto.CctvControlPtzRes(req.getCommand(), req.getAction(), 0, "success");
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * ZENO Request HTTP Post
|
|
|
|
+ * @param result
|
|
|
|
+ * @param controlData
|
|
|
|
+ * @param ipAddr
|
|
|
|
+ * @param userId
|
|
|
|
+ * @param userPswd
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public CctvControlDto.CctvControlRes requestHttpPostZeno(CctvControlDto.CctvControlRes result, byte[] controlData, String ipAddr, String userId, String userPswd) {
|
|
|
|
+ String encoding = Base64.getEncoder().encodeToString((userId + ":" + userPswd).getBytes());
|
|
String apiUrl = "http://" + ipAddr + "/cgi-bin/admin/uartctrl.cgi";
|
|
String apiUrl = "http://" + ipAddr + "/cgi-bin/admin/uartctrl.cgi";
|
|
- byte[] controlData = getZenoCommand(req);
|
|
|
|
- log.error("{}, {}", apiUrl, SysUtils.byteArrayToHex(controlData));
|
|
|
|
|
|
+ //log.info("{}, {}", apiUrl, SysUtils.byteArrayToHex(controlData));
|
|
|
|
+
|
|
|
|
+ HttpURLConnection connection = null;
|
|
try {
|
|
try {
|
|
|
|
+ // POST /cgi-bin/admin/uartctrl.cgi HTTP/1.1
|
|
|
|
+ // User-Agent: Mozilla/3.0 (compatible; Indy Library)
|
|
|
|
+ // Accept: text/html, */*
|
|
|
|
+ // Indy 로 할경우
|
|
|
|
+ // [POST /cgi-bin/admin/uartctrl.cgi HTTP/1.1
|
|
|
|
+ // User-Agent: Java/1.8.0_25
|
|
|
|
+ // Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
|
|
URL url = new URL(apiUrl);
|
|
URL url = new URL(apiUrl);
|
|
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
+ connection = (HttpURLConnection)url.openConnection();
|
|
connection.setConnectTimeout(5000); //서버에 연결되는 Timeout 시간 설정
|
|
connection.setConnectTimeout(5000); //서버에 연결되는 Timeout 시간 설정
|
|
connection.setReadTimeout(5000); // InputStream 읽어 오는 Timeout 시간 설정
|
|
connection.setReadTimeout(5000); // InputStream 읽어 오는 Timeout 시간 설정
|
|
- //connection.addRequestProperty("x-api-key", RestTestCommon.API_KEY); //key값 설정
|
|
|
|
|
|
|
|
- connection.setDoOutput(true); //POST 데이터를 OutputStream으로 넘겨 주겠다는 설정
|
|
|
|
- connection.setRequestMethod("POST");
|
|
|
|
-
|
|
|
|
- //json 으로 message 를 전달하고자 할 때
|
|
|
|
- connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
+ connection.setDoOutput(true); //POST 데이터를 OutputStream 으로 넘겨 주겠다는 설정
|
|
connection.setDoInput(true);
|
|
connection.setDoInput(true);
|
|
-
|
|
|
|
- connection.setUseCaches(false);
|
|
|
|
- connection.setDefaultUseCaches(false);
|
|
|
|
-
|
|
|
|
- PrintWriter writer = null;
|
|
|
|
- OutputStream output = connection.getOutputStream();
|
|
|
|
- writer = new PrintWriter(new OutputStreamWriter(output, "utf-8"), true);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- // Send binary file.
|
|
|
|
- /*writer.append("--" + boundary).append("\r\n");
|
|
|
|
- writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append("\r\n");
|
|
|
|
- writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append("\r\n");
|
|
|
|
- writer.append("Content-Transfer-Encoding: binary").append("\r\n");
|
|
|
|
- writer.append("\r\n").flush();
|
|
|
|
-*/
|
|
|
|
- OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
|
|
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
|
+ connection.setRequestProperty("Content-Type", "application/octet-stream");
|
|
|
|
+ connection.setRequestProperty("Accept", "text/html, */*");
|
|
|
|
+ connection.setRequestProperty("Accept-Encoding", "identity");
|
|
|
|
+ connection.setRequestProperty("Authorization", "Basic " + encoding);
|
|
|
|
+ //connection.setRequestProperty("charset", "UTF-8");
|
|
|
|
+ //connection.setRequestProperty("Content-Length", Integer.toString(controlData.length));
|
|
|
|
+ connection.setRequestProperty("User-Agent", "Mozilla/3.0");
|
|
|
|
+
|
|
|
|
+ //connection.setUseCaches(false);
|
|
|
|
+ //connection.setDefaultUseCaches(false);
|
|
|
|
+
|
|
|
|
+ DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
|
|
|
+ wr.write(controlData);
|
|
wr.flush();
|
|
wr.flush();
|
|
|
|
+ wr.close();
|
|
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
- //Stream을 처리해줘야 하는 귀찮음이 있음.
|
|
|
|
- BufferedReader br = new BufferedReader(
|
|
|
|
- new InputStreamReader(connection.getInputStream(), "utf-8"));
|
|
|
|
String line;
|
|
String line;
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
while ((line = br.readLine()) != null) {
|
|
while ((line = br.readLine()) != null) {
|
|
sb.append(line).append("\n");
|
|
sb.append(line).append("\n");
|
|
}
|
|
}
|
|
br.close();
|
|
br.close();
|
|
- System.out.println("" + sb.toString());
|
|
|
|
|
|
+ log.info("HTTP_OK: CCTV, {}, {}", ipAddr, sb);
|
|
|
|
+ result.setResult(0, sb.toString());
|
|
} else {
|
|
} else {
|
|
- System.out.println(connection.getResponseMessage());
|
|
|
|
|
|
+ log.error("HTTP_FAIL: {}", connection.getResponseMessage());
|
|
|
|
+ if (connection.getResponseMessage() == null) {
|
|
|
|
+ result.setResult(2, "cctv network fail.");
|
|
|
|
+ } else {
|
|
|
|
+ result.setResult(3, connection.getResponseMessage());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} catch (Exception e){
|
|
} catch (Exception e){
|
|
- log.error("{}", e);
|
|
|
|
|
|
+ log.error("Exception: {}", e.getMessage());
|
|
|
|
+ result.setResult(4, e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ if (connection != null) {
|
|
|
|
+ connection.disconnect();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -118,7 +160,7 @@ public class CctvControlService {
|
|
* @param req
|
|
* @param req
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public CctvControlDto.CctvControlPtzRes controlPtz(Long id, CctvControlDto.CctvControlPtzReq req) {
|
|
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPtz(Long id, CctvControlDto.CctvControlPtzReq req) {
|
|
log.info("{}", req);
|
|
log.info("{}", req);
|
|
TbCctvCtlr cctv = this.requireOne(id);
|
|
TbCctvCtlr cctv = this.requireOne(id);
|
|
if (StringUtils.equals("1", cctv.getCctvType())) {
|
|
if (StringUtils.equals("1", cctv.getCctvType())) {
|
|
@@ -129,6 +171,27 @@ public class CctvControlService {
|
|
// 프로브디지털
|
|
// 프로브디지털
|
|
return this.controlPtzProbeDigital(cctv.getCctvCtlrIp(), "admin", "1234", req);
|
|
return this.controlPtzProbeDigital(cctv.getCctvCtlrIp(), "admin", "1234", req);
|
|
}
|
|
}
|
|
- return new CctvControlDto.CctvControlPtzRes(req.getCommand(), req.getAction(), 1, "unknown cctv type");
|
|
|
|
|
|
+ return new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 1, "unknown cctv type");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * CCTV Preset 컨트롤
|
|
|
|
+ * @param id
|
|
|
|
+ * @param req
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public CctvControlDto.CctvControlRes controlPreset(Long id, CctvControlDto.CctvControlPresetReq req) {
|
|
|
|
+ log.info("{}", req);
|
|
|
|
+ TbCctvCtlr cctv = this.requireOne(id);
|
|
|
|
+ if (StringUtils.equals("1", cctv.getCctvType())) {
|
|
|
|
+ // 제노 CCTV
|
|
|
|
+ return this.controlPresetZeno(cctv.getCctvCtlrIp(), "root", "pass", req);
|
|
|
|
+ }
|
|
|
|
+ else if (StringUtils.equals("2", cctv.getCctvType())) {
|
|
|
|
+ // 프로브디지털
|
|
|
|
+ return this.controlPresetProbeDigital(cctv.getCctvCtlrIp(), "admin", "1234", req);
|
|
|
|
+ }
|
|
|
|
+ return new CctvControlDto.CctvControlRes(req.getCommand(), req.getAction(), 1, "unknown cctv type");
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|