灵闪初步测试
parent
29a8a3236b
commit
1ac03d25de
@ -0,0 +1,68 @@
|
|||||||
|
package com.zhehekeji.web.service.lbScanCode;
|
||||||
|
|
||||||
|
import com.zhehekeji.common.util.SpringContextUtil;
|
||||||
|
import com.zhehekeji.web.lib.CameraControlModule;
|
||||||
|
import com.zhehekeji.web.lib.CameraDelayTask;
|
||||||
|
import com.zhehekeji.web.lib.TaskDelayExecutor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.concurrent.DelayQueue;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class LBTaskDelayExecutor {
|
||||||
|
|
||||||
|
private static ExecutorService exec = Executors.newFixedThreadPool(1);
|
||||||
|
|
||||||
|
private static DelayQueue<CameraDelayTask> queue = new DelayQueue<>();
|
||||||
|
|
||||||
|
public static void addMp4DelayTask(Integer cameraId, String path, LocalDateTime startTime, LocalDateTime endTime, Long delayTime) {
|
||||||
|
CameraDelayTask cameraDelayTask = new CameraDelayTask(cameraId, startTime, endTime,path, 0,delayTime);
|
||||||
|
queue.add(cameraDelayTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addPicDelayTask(Integer cameraId, String path, Long delayTime) {
|
||||||
|
CameraDelayTask cameraDelayTask = new CameraDelayTask(cameraId, null, null,path, 1,delayTime);
|
||||||
|
queue.add(cameraDelayTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addGyrateCameraTask(Integer cameraId, Long delayTime,Integer ptzId){
|
||||||
|
CameraDelayTask cameraDelayTask = new CameraDelayTask(cameraId, null, null,null, 2,delayTime);
|
||||||
|
cameraDelayTask.setPtzId(ptzId);
|
||||||
|
queue.add(cameraDelayTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void runMp4DownloadExecutor(){
|
||||||
|
exec.execute(new LBTaskDelayExecutor.Consumer());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Consumer implements Runnable {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
CameraControlModule cameraControlModule = SpringContextUtil.getBean(CameraControlModule.class);
|
||||||
|
CameraDelayTask cameraDelayTask = queue.take();
|
||||||
|
if(cameraDelayTask != null){
|
||||||
|
|
||||||
|
if(cameraDelayTask.getType() == 0){
|
||||||
|
|
||||||
|
cameraControlModule.downloadMp4(cameraDelayTask.getCameraId(), cameraDelayTask.getPath(), cameraDelayTask.getStartTime(), cameraDelayTask.getEndTime());
|
||||||
|
}else if(cameraDelayTask.getType() == 1){
|
||||||
|
cameraControlModule.pic(cameraDelayTask.getCameraId(),0, cameraDelayTask.getPath());
|
||||||
|
}else if(cameraDelayTask.getType() == 2){
|
||||||
|
cameraControlModule.toPtz(cameraDelayTask.getPtzId(),cameraDelayTask.getCameraId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,105 @@
|
|||||||
|
package com.zhehekeji.web.service.lbScanCode;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
import static java.lang.Thread.sleep;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class ReentrantLockExample {
|
||||||
|
|
||||||
|
private static Lock lock = new ReentrantLock();
|
||||||
|
private int count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
return count;
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeCmd(OutputStream os) throws IOException {
|
||||||
|
String startCmd = "start";
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
for (int i = 10 ;i < 100; i++){
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
System.out.println("111");
|
||||||
|
readOCR("127.0.0.1",6000);
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String readOCR(String ip,int port){
|
||||||
|
|
||||||
|
Socket socket = new Socket();
|
||||||
|
String code = "NoRead";
|
||||||
|
OutputStream os = null;
|
||||||
|
InputStream is = null;
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
|
||||||
|
socket.connect(new InetSocketAddress(ip, port), 3000);
|
||||||
|
socket.setSoTimeout(3000);
|
||||||
|
os = socket.getOutputStream();
|
||||||
|
is = socket.getInputStream();
|
||||||
|
int i = 0;
|
||||||
|
while ("NoRead".equals(code) && i <= 4) {
|
||||||
|
writeCmd(os);
|
||||||
|
code = read(is);
|
||||||
|
if (code != null) {
|
||||||
|
code = code.replace("\\n", "");
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("sick time out,ip:{},info:{}", ip, e);
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue