|
@@ -2,18 +2,29 @@ package com.warewms.hailiang.connect;
|
|
|
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.github.rholder.retry.*;
|
|
|
-import com.warewms.hailiang.connect.handler.CodeReader3Handler;
|
|
|
+import com.jhlabs.image.OilFilter;
|
|
|
+import com.warewms.hailiang.MES.MesService;
|
|
|
+import com.warewms.hailiang.config.CodeReadProperties;
|
|
|
+import com.warewms.hailiang.connect.base.TCPConnectBase;
|
|
|
import com.warewms.hailiang.domain.Device;
|
|
|
+import com.warewms.hailiang.domain.DeviceLog;
|
|
|
+import com.warewms.hailiang.domain.RetroactiveNow;
|
|
|
+import com.warewms.hailiang.enums.DaoJiaoPlcEnum;
|
|
|
+import com.warewms.hailiang.enums.DeviceNameEnum;
|
|
|
+import com.warewms.hailiang.init.PlcConnectServiceRunner;
|
|
|
+import com.warewms.hailiang.service.RetroactiveNowService;
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
-import io.netty.channel.ChannelFuture;
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
-import io.netty.channel.ChannelOption;
|
|
|
-import io.netty.channel.EventLoopGroup;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
+import io.netty.channel.*;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
+import io.netty.util.CharsetUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -33,11 +44,11 @@ public class CodeReader3Connect implements TCPConnectBase {
|
|
|
|
|
|
private final String IP_ADDR = "172.20.27.3";
|
|
|
|
|
|
- private final String deviceName ="CodeReader3";
|
|
|
+ private final String deviceName = "CodeReader3";
|
|
|
|
|
|
private final int PORT = 51236;
|
|
|
|
|
|
- private boolean enable = false;
|
|
|
+ private boolean enable = true;
|
|
|
|
|
|
private ChannelFuture future;
|
|
|
|
|
@@ -45,12 +56,19 @@ public class CodeReader3Connect implements TCPConnectBase {
|
|
|
|
|
|
private EventLoopGroup group;
|
|
|
|
|
|
+ private ChannelPipeline pipeline;
|
|
|
|
|
|
+ @Resource
|
|
|
+ MesService mesService;
|
|
|
+ @Resource
|
|
|
+ RetroactiveNowService retroactiveNowService;
|
|
|
+ @Resource
|
|
|
+ PlcConnectServiceRunner plcConnectServiceRunner;
|
|
|
|
|
|
@Override
|
|
|
public void init() throws InterruptedException {
|
|
|
- if (enable){
|
|
|
- log.info("ip:{},deviceName:{}正在进行连接",IP_ADDR,deviceName);
|
|
|
+ if (enable) {
|
|
|
+ log.info("ip:{},deviceName:{}正在进行连接", IP_ADDR, deviceName);
|
|
|
group = new NioEventLoopGroup();
|
|
|
try {
|
|
|
bootstrap = new Bootstrap();
|
|
@@ -59,21 +77,35 @@ public class CodeReader3Connect implements TCPConnectBase {
|
|
|
.handler(new ChannelInitializer<SocketChannel>() {
|
|
|
@Override
|
|
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
- socketChannel.pipeline().addLast(new CodeReader3Handler());
|
|
|
+ pipeline = socketChannel.pipeline();
|
|
|
+ pipeline.addLast(new ChannelInboundHandlerAdapter() {
|
|
|
+ @Override
|
|
|
+ public void channelInactive(ChannelHandlerContext ctx) {
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "2"));
|
|
|
+ log.error("设备:{}连接已断开!", deviceName);
|
|
|
+ retry();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
|
|
+ ByteBuf byteBuf = (ByteBuf) msg;
|
|
|
+ processMessages(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8)));
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
future = bootstrap.connect(IP_ADDR, PORT).sync();
|
|
|
future.addListener((channelFuture) -> {
|
|
|
if (channelFuture.isSuccess()) {
|
|
|
- SpringUtil.getApplicationContext().publishEvent(new Device(deviceName,"1"));
|
|
|
- log.info("ip:{},deviceName:{}连接成功",IP_ADDR,deviceName);
|
|
|
- }else {
|
|
|
- log.info("ip:{},deviceName:{}连接失败等待重试!",IP_ADDR,deviceName);
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
|
|
|
+ log.info("ip:{},deviceName:{}连接成功", IP_ADDR, deviceName);
|
|
|
+ } else {
|
|
|
+ log.info("ip:{},deviceName:{}连接失败等待重试!", IP_ADDR, deviceName);
|
|
|
retry();
|
|
|
}
|
|
|
});
|
|
|
} catch (Exception e) {
|
|
|
- log.error("ip:{},deviceName:{}连接异常,{},准备重试",IP_ADDR,deviceName, e.getMessage());
|
|
|
+ log.error("ip:{},deviceName:{}连接异常,{},准备重试", IP_ADDR, deviceName, e.getMessage());
|
|
|
retry();
|
|
|
}
|
|
|
}
|
|
@@ -90,17 +122,18 @@ public class CodeReader3Connect implements TCPConnectBase {
|
|
|
try {
|
|
|
retryer.call(new Callable<Boolean>() {
|
|
|
AtomicBoolean isSuccess = new AtomicBoolean(false);
|
|
|
+
|
|
|
@Override
|
|
|
public Boolean call() throws Exception {
|
|
|
- log.info("ip:{},deviceName:{}连接重试",IP_ADDR,deviceName);
|
|
|
+ log.info("ip:{},deviceName:{}连接重试", IP_ADDR, deviceName);
|
|
|
future = bootstrap.connect(IP_ADDR, PORT).sync();
|
|
|
future.addListener((channelFuture) -> {
|
|
|
isSuccess.set(channelFuture.isSuccess());
|
|
|
if (channelFuture.isSuccess()) {
|
|
|
- SpringUtil.getApplicationContext().publishEvent(new Device(deviceName,"1"));
|
|
|
- log.info("ip:{},deviceName:{}连接成功",IP_ADDR,deviceName);
|
|
|
- }else {
|
|
|
- log.info("ip:{},deviceName:{}连接失败等待重试!",IP_ADDR,deviceName);
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
|
|
|
+ log.info("ip:{},deviceName:{}连接成功", IP_ADDR, deviceName);
|
|
|
+ } else {
|
|
|
+ log.info("ip:{},deviceName:{}连接失败等待重试!", IP_ADDR, deviceName);
|
|
|
}
|
|
|
});
|
|
|
Thread.sleep(3000);
|
|
@@ -122,4 +155,33 @@ public class CodeReader3Connect implements TCPConnectBase {
|
|
|
return deviceName;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ChannelPipeline getChannel() {
|
|
|
+ return pipeline;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void processMessages(String message) {
|
|
|
+ try {
|
|
|
+ log.info("倒角机读码器读码结果,{}", message);
|
|
|
+ if (CodeReadProperties.failureReturnInstruction.equals(message)) {
|
|
|
+ //读码失败
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new DeviceLog("Z1_DaoJia_DMQ-1-27.3", deviceName, "未识别到码", "2"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //修改数据库信息
|
|
|
+ RetroactiveNow retroactiveNow = new RetroactiveNow();
|
|
|
+ retroactiveNow.setBatchNo(message);
|
|
|
+ retroactiveNow.setStatus("");
|
|
|
+ retroactiveNowService.updateData(retroactiveNow);
|
|
|
+ //上传MES系统
|
|
|
+ mesService.getBatchNoResult(message, true);
|
|
|
+ //回写PLC读码完成
|
|
|
+ plcConnectServiceRunner.getPlcServer(DeviceNameEnum.DAOJIAOPLC.getDeviceName()).writeBoolean(DaoJiaoPlcEnum.RETURNOUTCOME.getMetadata(),false);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("倒角读码任务执行异常,msg:{}",e.getMessage());
|
|
|
+ SpringUtil.getApplicationContext().publishEvent(new DeviceLog("Z1_DaoJia_DMQ-1-27.3", deviceName, "读码任务出错,msg:"+e.getMessage(), "2"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|