From c8fdce19b999204d931b53d1b22cbf06ae71e964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAPTOP-S9HJSOEB=5C=E6=98=8A=E5=A4=A9?= Date: Fri, 5 Jul 2024 09:54:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=81=AF=E5=85=89=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=97=B6=E9=97=B4=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E4=BF=AE=E6=94=B9=EF=BC=88=E4=BF=9D=E8=AF=81?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E6=97=B6=E9=97=B4=E5=8F=AA=E6=9C=89=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=93=BE=E6=8E=A5=EF=BC=89=20=E9=9A=8F=E8=A1=8C?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=BF=AE=E6=94=B9=20=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/StreetController.java | 40 ++++++---- .../lib/hik/HikCameraControlModuleImpl.java | 3 +- .../zhehekeji/web/service/InitService.java | 2 +- .../web/service/LightSourceService.java | 14 ++-- .../zhehekeji/web/service/OrderService.java | 58 +++++++++++--- .../com/zhehekeji/web/service/PlcService.java | 56 ++++++++++---- .../zhehekeji/web/service/StockService.java | 4 +- .../hikLightSource/HikControlSocket.java | 43 +++++++++++ .../web/service/ksec/KsecNettyClient.java | 75 ++++++++++++------- web/src/main/resources/application-prod.yml | 4 +- 10 files changed, 219 insertions(+), 80 deletions(-) diff --git a/web/src/main/java/com/zhehekeji/web/controller/StreetController.java b/web/src/main/java/com/zhehekeji/web/controller/StreetController.java index c66bfbf..58aad9f 100644 --- a/web/src/main/java/com/zhehekeji/web/controller/StreetController.java +++ b/web/src/main/java/com/zhehekeji/web/controller/StreetController.java @@ -3,6 +3,7 @@ package com.zhehekeji.web.controller; import com.github.pagehelper.PageInfo; import com.zhehekeji.core.pojo.Result; import com.zhehekeji.web.config.ConfigProperties; +import com.zhehekeji.web.entity.LightSource; import com.zhehekeji.web.entity.Street; import com.zhehekeji.web.pojo.street.StreetSearch; import com.zhehekeji.web.pojo.street.StreetVO; @@ -77,42 +78,55 @@ public class StreetController { @GetMapping("/lightSource/open") @ApiOperation(value = "开启光源") public Result openLightSource(){ - ClientChanel.keys().forEach(k->{ - LCTransmission lcTransmission = new LCTransmission(k,1); - ClientChanel.write(lcTransmission.toString(),k); + List lightSources = lightSourceService.getLightSource(); + + lightSources.forEach(lightSource -> { + LightSourceService.lightControllerFactory(configProperties.getLightSource(),lightSource,1); + + }); + return Result.success(); } @GetMapping("/lightSource/close") @ApiOperation(value = "关闭光源") public Result closeLightSource(){ - ClientChanel.keys().forEach(k->{ - LCTransmission lcTransmission = new LCTransmission(k,0); - ClientChanel.write(lcTransmission.toString(),k); + + List lightSources = lightSourceService.getLightSource(); + + lightSources.forEach(lightSource -> { + LightSourceService.lightControllerFactory(configProperties.getLightSource(),lightSource,0); + }); + return Result.success(); } @GetMapping("/lightSource/open/{streetId}") @ApiOperation(value = "开启单个巷道光源") public Result openStreetLightSource(@PathVariable Integer streetId){ - Street street = streetService.streetById(streetId); - LCTransmission lcTransmission = new LCTransmission(street.getPlcId(),1); - ClientChanel.write(lcTransmission.toString(),street.getPlcId()); + List lightSources = lightSourceService.getLightSourceByStreetId(streetId); + lightSources.forEach(lightSource -> { + LightSourceService.lightControllerFactory(configProperties.getLightSource(),lightSource,1); + + }); + return Result.success(); } @GetMapping("/lightSource/close/{streetId}") @ApiOperation(value = "关闭单个巷道光源") public Result closeStreetLightSource(@PathVariable Integer streetId){ - Street street = streetService.streetById(streetId); - LCTransmission lcTransmission = new LCTransmission(street.getPlcId(),0); - ClientChanel.write(lcTransmission.toString(),street.getPlcId()); + List lightSources = lightSourceService.getLightSourceByStreetId(streetId); + lightSources.forEach(lightSource -> { + LightSourceService.lightControllerFactory(configProperties.getLightSource(),lightSource,0); + + }); + return Result.success(); } - } diff --git a/web/src/main/java/com/zhehekeji/web/lib/hik/HikCameraControlModuleImpl.java b/web/src/main/java/com/zhehekeji/web/lib/hik/HikCameraControlModuleImpl.java index 13a7f1a..392c37b 100644 --- a/web/src/main/java/com/zhehekeji/web/lib/hik/HikCameraControlModuleImpl.java +++ b/web/src/main/java/com/zhehekeji/web/lib/hik/HikCameraControlModuleImpl.java @@ -297,8 +297,7 @@ public class HikCameraControlModuleImpl implements CameraControlModule { } public void downloadMp4(Integer cameraId, String path, LocalDateTime start, LocalDateTime end) { - start = start.minusMinutes(1); - end = end.minusMinutes(1); + start = start.minusSeconds(10); PathUtil.checkDirc(path); HCNetSDK.NET_DVR_TIME startTime = new HCNetSDK.NET_DVR_TIME(); startTime.setTime(start.getYear(), start.getMonthValue(), start.getDayOfMonth(), start.getHour(), start.getMinute(), start.getSecond()); diff --git a/web/src/main/java/com/zhehekeji/web/service/InitService.java b/web/src/main/java/com/zhehekeji/web/service/InitService.java index bb46bdf..63dad19 100644 --- a/web/src/main/java/com/zhehekeji/web/service/InitService.java +++ b/web/src/main/java/com/zhehekeji/web/service/InitService.java @@ -105,7 +105,7 @@ public class InitService implements ApplicationRunner { ksecNettyClient.createClient(ksec); }catch (Exception e){ log.error("kesc connect error,url:{},port:{}",ksec.getIp(),ksec.getPort()); - ksecNettyClient.reconnect(0); + //ksecNettyClient.reconnect(0); } }else { log.error("ksec no config"); diff --git a/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java b/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java index 90aa5a8..0434882 100644 --- a/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java +++ b/web/src/main/java/com/zhehekeji/web/service/LightSourceService.java @@ -7,10 +7,6 @@ import com.zhehekeji.web.mapper.LightSourceMapper; import com.zhehekeji.web.service.damLightSource.JYDAMEquip; import com.zhehekeji.web.service.damLightSource.JYDamHelper; import com.zhehekeji.web.service.hikLightSource.HikControlSocket; -import com.zhehekeji.web.service.light.RelaySource; -import com.zhehekeji.web.service.light.damLightSource.JYDAMEquip; -import com.zhehekeji.web.service.light.damLightSource.JYDamHelper; -import com.zhehekeji.web.service.light.hikLightSource.HikControlSocket; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -28,6 +24,14 @@ public class LightSourceService { @Resource private LightSourceMapper lightSourceMapper; + + public static void lightControllerOpen(ConfigProperties.LightSource lightSource,LightSource lightInfo) { + lightControllerFactory(lightSource, lightInfo, 1); + } + + public static void lightControllerClose(ConfigProperties.LightSource lightSource,LightSource lightInfo) { + lightControllerFactory(lightSource, lightInfo, 0); + } public static void lightControllerFactory(ConfigProperties.LightSource lightSource,LightSource lightInfo, Integer stat){ if(lightSource.getType() == 1){ @@ -44,8 +48,6 @@ public class LightSourceService { HikControlSocket.lightController(lightInfo.getIp(),lightInfo.getPort(),lightSource.getIndex(),stat); }else if (lightSource.getType() == 3){ LightSourceService.lightController(lightInfo.getIp(),lightInfo.getPort(),stat); - }else if (lightSource.getType() == 4){ - RelaySource.lightController(lightInfo.getIp(),lightInfo.getPort(),stat); } } diff --git a/web/src/main/java/com/zhehekeji/web/service/OrderService.java b/web/src/main/java/com/zhehekeji/web/service/OrderService.java index f89ac26..57cb215 100644 --- a/web/src/main/java/com/zhehekeji/web/service/OrderService.java +++ b/web/src/main/java/com/zhehekeji/web/service/OrderService.java @@ -47,8 +47,16 @@ public class OrderService { } - orderVO.setGoodsLocation(location(orderVO,streetMap.get(orderVO.getStreetId()))); - if (orderVO.getPicPaths() != null && orderVO.getPicPaths().split(",").length > 0){ + orderVO.setGoodsLocation(location(orderVO, streetMap.get(orderVO.getStreetId()))); + if(orderVO.getStartTime() != null && orderVO.getEndTime() != null){ + Duration duration = Duration.between(orderVO.getStartTime(), orderVO.getEndTime()); + Long seconds = duration.getSeconds(); + int minutes = seconds.intValue() / 60; + int remainingSeconds = seconds.intValue() % 60; + String timeLength = String.format("%02d:%02d", minutes,remainingSeconds); + orderVO.setTimeLength(timeLength); + } + if (orderVO.getPicPaths() != null && orderVO.getPicPaths().split(",").length > 0) { orderVO.setPics(orderVO.getPicPaths().split(",")); } // if(orderVO.getIntoStockPic() != null ) { @@ -67,21 +75,51 @@ public class OrderService { return order.getId(); } - public String location(OrderVO orderVO,Street street){ + public String location(OrderVO orderVO, Street street) { + + String location = ""; - if(orderVO.getLeftRight1() != null){ - String leftRightS = orderVO.getLeftRight1() == 1 ? "左" : "右"; - location = leftRightS; + + if (orderVO.getColumn1() != null && orderVO.getColumn1() != 0) { + if (orderVO.getLeftRight1() != null) { + String leftRightS = orderVO.getLeftRight1() == 1 ? "左侧" : "右侧"; + location = leftRightS; + } + if (orderVO.getInOut1() != null) { + + String side = orderVO.getInOut1() == 1 ? "浅货位" : "深货位"; + location = location + side; + } + location = location + orderVO.getRow1() + "层" + orderVO.getColumn1() + "列"; + } else { + location = "库外"; } - if(orderVO.getInOut1() != null){ - String side = orderVO.getInOut1() == 1?"浅":"深"; - location = location + "-" + side +"-"; + + //转 + + location = location + " 转 "; + + + if (orderVO.getColumn2() != null && orderVO.getColumn2() != 0) { + if (orderVO.getLeftRight2() != null) { + String leftRightS = orderVO.getLeftRight2() == 1 ? "左侧" : "右侧"; + location = location + leftRightS; + } + if (orderVO.getInOut2() != null) { + + String side = orderVO.getInOut2() == 1 ? "浅货位" : "深货位"; + location = location + side; + } + + location = location + orderVO.getRow2() + "层" + orderVO.getColumn2() + "列"; + } else { + location =location + "库外"; } //里外 现在无法判断 这个项目全是 里 - return location +orderVO.getRow1()+"层"+orderVO.getColumn1()+"列"; + return location; } diff --git a/web/src/main/java/com/zhehekeji/web/service/PlcService.java b/web/src/main/java/com/zhehekeji/web/service/PlcService.java index 003a160..7f4d195 100644 --- a/web/src/main/java/com/zhehekeji/web/service/PlcService.java +++ b/web/src/main/java/com/zhehekeji/web/service/PlcService.java @@ -104,6 +104,15 @@ public class PlcService { public void orderStart(PlcCmdInfo plcCmdInfo) { Street street = streetService.getStreetByPlcId(plcCmdInfo.getPlcId()); + List lightSources = lightSourceMapper.selectList(new QueryWrapper().eq("street_id",street.getId())); + lightSources.forEach(lightSource -> { + try { + LightSourceService.lightControllerOpen(configProperties.getLightSource(),lightSource); + }catch (Exception e){ + log.error("open light"+lightSource.getIp()+" error",e); + } + + }); if (street != null) { if (plcCmdInfo.getLeftRight1() == 1) { if (plcCmdInfo.getRow1() > street.getLeftRow() && plcCmdInfo.getColumn1() > street.getLeftColumn()) { @@ -116,10 +125,22 @@ public class PlcService { return; } } + + Integer cameraId = getCameraByPlcCmd(plcCmdInfo,plcCmdInfo.getLeftRight1()); + + String code = "C2-" + plcCmdInfo.getLeftRightStr(0) + plcCmdInfo.getInOutStr(0); + gyrateCameraByCode(cameraId,code); + + + Long delayTime = configProperties.getCameraConfig().getC1DelayCaptureTime(); + + String path = PathUtil.createFileNameByRowColumn("jpg",cameraId,plcCmdInfo.getRow1(),plcCmdInfo.getColumn1()); + path = cameraCapture(cameraId,true,delayTime,path); plcCmdInfo.setStreetId(street.getId()); Order order = new Order(); order.setOrderNum(plcCmdInfo.getOrderNum()); order.setStatus(0); + order.setPicPaths(path); order.setStartTime(LocalDateTime.now()); order.setStreetId(street.getId()); order.setInOut1(plcCmdInfo.getSide1()); @@ -177,6 +198,16 @@ public class PlcService { String path = cameraVideo(street.getCamera2Id(), order.getStartTime(), endDownLoadTime); update.setVideoPath2(path); } + + List lightSources = lightSourceMapper.selectList(new QueryWrapper().eq("street_id",street.getId())); + lightSources.forEach(lightSource -> { + try { + LightSourceService.lightControllerClose(configProperties.getLightSource(),lightSource); + }catch (Exception e){ + log.error("open light"+lightSource.getIp()+" error",e); + } + + }); orderMapper.updateById(update); } @@ -272,7 +303,7 @@ public class PlcService { */ Integer cameraId = getCameraByPlcCmd(plcCmdInfo,orderInfo.getLeftRight()); gyrateCameraByCode(cameraId,orderInfo.getCmdCode()); - if(needCapture){ + if(needCapture && code.startsWith("C4")){ Boolean delay = true; Integer row = 0; Integer column = 0; @@ -287,24 +318,12 @@ public class PlcService { sep = plcCmdInfo.getSeparation2(); } long delayTime = 0; - if(code.startsWith("C1")){ - delayTime = configProperties.getCameraConfig().getC1DelayCaptureTime(); - }else if(code.startsWith("C2")){ - if(sep == 1){ - delayTime = configProperties.getCameraConfig().getC2DelayCaptureTime(); - }else { - delayTime = configProperties.getCameraConfig().getC2OutDelayCaptureTime(); - } - }else if(code.startsWith("C3")){ - delayTime = configProperties.getCameraConfig().getC3DelayCaptureTime(); - - }else if(code.startsWith("C4")){ + //该项目只有c1,c4,并且c1由b1拍照 if(sep == 1){ delayTime = configProperties.getCameraConfig().getC4DelayCaptureTime(); }else { delayTime = configProperties.getCameraConfig().getC4OutDelayCaptureTime(); } - } path = PathUtil.createFileNameByRowColumn("jpg",cameraId,row,column); path = cameraCapture(cameraId,delay,delayTime,path); } @@ -331,7 +350,7 @@ public class PlcService { List lightSources = lightSourceMapper.selectList(new QueryWrapper().eq("street_id",street.getId())); lightSources.forEach(lightSource -> { try { - HikControlSocket.openLight(lightSource.getIp(),lightSource.getPort(),configProperties.getLightSource().getIndex(),1); + LightSourceService.lightControllerOpen(configProperties.getLightSource(),lightSource); }catch (Exception e){ log.error("open light"+lightSource.getIp()+" error",e); } @@ -541,7 +560,14 @@ public class PlcService { long end = System.currentTimeMillis(); long s = end - startTime; log.info("time:{}millisecond", s); + lightSources.forEach(lightSource -> { + try { + LightSourceService.lightControllerClose(configProperties.getLightSource(),lightSource); + }catch (Exception e){ + log.error("open light"+lightSource.getIp()+" error",e); + } + }); return true; } diff --git a/web/src/main/java/com/zhehekeji/web/service/StockService.java b/web/src/main/java/com/zhehekeji/web/service/StockService.java index 8ae8c7d..deafabe 100644 --- a/web/src/main/java/com/zhehekeji/web/service/StockService.java +++ b/web/src/main/java/com/zhehekeji/web/service/StockService.java @@ -73,13 +73,13 @@ public class StockService { return new ArrayList<>(); } //List shelves = streetService.check(street); - List stocks = stockMapper.selectList(new QueryWrapper().eq("street_id",streetId).orderByAsc("shelve_id", "`row`", "`column`")); + List stocks = stockMapper.selectList(new QueryWrapper().eq("street_id",streetId).orderByAsc("street_id", "`row`", "`column`")); return stocks; } public PageInfo page(PageSearch pageSearch) { PageHelper.startPage(pageSearch.getPageNum(), pageSearch.getPageSize()); - List stocks = stockMapper.selectList(new QueryWrapper().orderByAsc("shelve_id", "`row`", "`column`")); + List stocks = stockMapper.selectList(new QueryWrapper().orderByAsc("street_id", "`row`", "`column`")); return new PageInfo<>(stocks); } diff --git a/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java b/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java index 17c3313..04af57b 100644 --- a/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java +++ b/web/src/main/java/com/zhehekeji/web/service/hikLightSource/HikControlSocket.java @@ -14,6 +14,49 @@ import java.nio.charset.StandardCharsets; @Slf4j public class HikControlSocket { + + public static int lightController(String ip,int port,int index,int bool){ + Socket socket = new Socket(); + int status = 0; + OutputStream os = null; + InputStream is = null; + try { + socket.connect(new InetSocketAddress(ip,port),3000); + //socket.setSoTimeout(10000); + os = socket.getOutputStream(); + Thread.sleep(100); + controlCmd(os,bool,index); + //is = socket.getInputStream(); + //String s = read(socket.getInputStream()); + //log.info("hik receieve:{}",s); + socket.close(); + } catch (IOException e) { + log.error("hik contro 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 status; + + } + } + /** * * <${端口号},${开关状态(0:关 1:开)},${get\post}> diff --git a/web/src/main/java/com/zhehekeji/web/service/ksec/KsecNettyClient.java b/web/src/main/java/com/zhehekeji/web/service/ksec/KsecNettyClient.java index f99361d..8644af3 100644 --- a/web/src/main/java/com/zhehekeji/web/service/ksec/KsecNettyClient.java +++ b/web/src/main/java/com/zhehekeji/web/service/ksec/KsecNettyClient.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.annotation.Resource; +import java.util.concurrent.atomic.AtomicBoolean; @Slf4j @Component @@ -29,23 +30,43 @@ public class KsecNettyClient { */ private static int RECONNECT_NUM = 10; - private static Channel channel; + private static Bootstrap client ;; // 单例Bootstrap + + + private static final AtomicBoolean reconnecting = new AtomicBoolean(false); // 标记是否正在重连 + private static Channel channel; +//次数 + static int num = 0; public void createClient(ConfigProperties.KSEC ksec) throws InterruptedException { - String lotnum = FileUtil.getText("lastLotnum"); - if(lotnum != null){ - KsecDecoder.setLastLotnum(lotnum); - } - if (StringUtils.isEmpty(ksec.getIp()) || ksec.getPort() == null) { - return; - } - Bootstrap client = new Bootstrap(); - client.group(group); - client.channel(NioSocketChannel.class); - KsecInfo heart = KsecInfo.heart(); - client.handler(new KescFilter(heart, plcService,this)); - // 连接服务端 - channel = client.connect(ksec.getIp(), ksec.getPort()).sync().channel(); + + String lotnum = FileUtil.getText("lastLotnum"); + if (lotnum != null) { + KsecDecoder.setLastLotnum(lotnum); + } + if (StringUtils.isEmpty(ksec.getIp()) || ksec.getPort() == null) { + return; + } + if (client == null) { + Bootstrap c = new Bootstrap(); + c.group(group); + c.channel(NioSocketChannel.class); + KsecInfo heart = KsecInfo.heart(); + c.handler(new KescFilter(heart, plcService, this)); + client = c; + } + try { + + channel = client.connect(ksec.getIp(), ksec.getPort()).sync().channel(); + + num = 0; + }catch (Exception e){ + log.error("createClient error",e); + + if (ksec.getReconnectNum() == -1 || num < ksec.getReconnectNum() && !reconnecting.get()) { + reconnect(0); + } + } } /** @@ -54,12 +75,7 @@ public class KsecNettyClient { * @param upId */ public void reconnect(Integer upId) { - if (channel != null) { - channel.disconnect(); - channel.close(); - } - Boolean isConnected = false; - int num = 0; + ConfigProperties.KSEC ksec = configProperties.getKsec(); if (ksec == null) { log.error("reconnect ,upPc is null ,id:{}", upId); @@ -70,7 +86,6 @@ public class KsecNettyClient { } catch (InterruptedException e) { e.printStackTrace(); } - while (ksec.getReconnectNum() == -1 || num < ksec.getReconnectNum() && !isConnected) { try { Thread.sleep(ksec.getReconnectInterval()); @@ -78,22 +93,24 @@ public class KsecNettyClient { e.printStackTrace(); } try { + //关闭之前连接 + if (channel != null) { + channel.disconnect(); + channel.close(); + } + //重连次数 + num++; + log.error("reconnect error num:{}", num); createClient(ksec); } catch (Exception e) { //没连上 继续 - log.error("reconnect error num:{}", num); - //关闭当前链接 - num++; // try{ // Thread.sleep(ksec.getReconnectInterval()); // }catch (Exception ex){ // throw new RuntimeException(ex); // } - continue; } - isConnected = true; - } - if (isConnected) { + if (reconnecting.get()) { log.info("plc reconnect success"); } else { log.error("plc reconnect error .upPcId:{},reconnect num:{},ip:{},port:{}", upId, num, ksec.getIp(), ksec.getPort()); diff --git a/web/src/main/resources/application-prod.yml b/web/src/main/resources/application-prod.yml index 4707bde..6d69521 100644 --- a/web/src/main/resources/application-prod.yml +++ b/web/src/main/resources/application-prod.yml @@ -64,9 +64,9 @@ ksec: platformIp: 127.0.0.1 platformPort: 3000 #断点重连的次数:-1->不断重连 -# reconnectNum: -1 + reconnectNum: -1 # #断点重连的时间间隔(单位:ms) -# reconnectInterval: 10000 + reconnectInterval: 5 # 服务端IP IP: 127.0.0.1 # 服务端TCP端口