|
|
|
|
@ -0,0 +1,91 @@
|
|
|
|
|
package com.zhehekeji.web.service.hikLightSource;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 视觉控制器光源控制
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class HikControlSocket {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* <${端口号},${开关状态(0:关 1:开)},${get\post}>
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String code = openLight("192.168.8.236", 2002,2,0);
|
|
|
|
|
System.out.println(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String openLight(String ip,int port,int index,int bool){
|
|
|
|
|
Socket socket = new Socket();
|
|
|
|
|
String code = null;
|
|
|
|
|
OutputStream os = null;
|
|
|
|
|
InputStream is = null;
|
|
|
|
|
try {
|
|
|
|
|
socket.connect(new InetSocketAddress(ip,port),3000);
|
|
|
|
|
os = socket.getOutputStream();
|
|
|
|
|
controlCmd(os,bool,index);
|
|
|
|
|
is = socket.getInputStream();
|
|
|
|
|
code = read(is);
|
|
|
|
|
if(code!= null){
|
|
|
|
|
code = code.replace("\\n","");
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("sick time out,ip:{},info:{}",ip,e);
|
|
|
|
|
}finally {
|
|
|
|
|
if(os != null){
|
|
|
|
|
try {
|
|
|
|
|
os.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(is != null){
|
|
|
|
|
try {
|
|
|
|
|
is.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
socket.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* num
|
|
|
|
|
* @param os
|
|
|
|
|
*
|
|
|
|
|
* @param bool 1:打开光源 0:关闭光源
|
|
|
|
|
* @param index 第几个口
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
private static void controlCmd(OutputStream os,int bool,int index) throws IOException {
|
|
|
|
|
String startCmd = "<"+index+","+bool+","+"post>";
|
|
|
|
|
byte [] bytes = startCmd.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void getStatus(OutputStream os,int index) throws IOException {
|
|
|
|
|
String startCmd = "<"+index+","+0+","+"get>";
|
|
|
|
|
byte [] bytes = startCmd.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String read(InputStream inStream) throws IOException {
|
|
|
|
|
BufferedReader bd = new BufferedReader(new InputStreamReader(inStream));
|
|
|
|
|
return bd.readLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|