123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package com.warewms.hailiang.connect;
- import cn.hutool.extra.spring.SpringUtil;
- import com.github.rholder.retry.*;
- import com.warewms.hailiang.connect.base.TCPConnectBase;
- import com.warewms.hailiang.domain.Device;
- import com.warewms.hailiang.enums.InkjetPrintersDirectivesEnum;
- import com.warewms.hailiang.util.ParseMsgTools;
- import io.netty.bootstrap.Bootstrap;
- 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 java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicBoolean;
- /**
- * Created with IntelliJ IDEA.
- *
- * @author: liuzhifei
- * Date: 2023/8/9
- * Time: 15:45
- * To change this template use File | Settings | File Templates.
- * Description:喷码机tcp连接类
- **/
- @Slf4j
- public class InkjetPrintersConnect implements TCPConnectBase {
- private final String IP_ADDR = "172.20.27.4";
- private final int PORT = 51236;
- private final String deviceName = "InkjetPrinters";
- private boolean enable = true;
- private ChannelFuture future;
- private Bootstrap bootstrap;
- private EventLoopGroup group;
- private ChannelPipeline pipeline;
- private boolean isInitRoutine = false;
- private String movement;
- @Override
- public void init() throws InterruptedException {
- if (enable) {
- log.info("喷码机正在进行连接");
- group = new NioEventLoopGroup();
- try {
- bootstrap = new Bootstrap();
- bootstrap.group(group).channel(NioSocketChannel.class)
- .option(ChannelOption.TCP_NODELAY, true)
- .handler(new ChannelInitializer<SocketChannel>() {
- @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- 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(ParseMsgTools.StringTohexString(byteBuf.toString(CharsetUtil.US_ASCII)));
- }
- });
- }
- });
- future = bootstrap.connect(IP_ADDR, PORT).sync();
- future.addListener((channelFuture) -> {
- if (channelFuture.isSuccess()) {
- initRoutine();
- SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
- log.info("喷码机连接成功");
- } else {
- log.info("连接失败等待重试!");
- retry();
- }
- });
- } catch (Exception e) {
- log.error("喷码设备连接异常,{},准备重试", e.getMessage());
- retry();
- }
- }
- }
- @Override
- public void retry() {
- Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
- .retryIfResult(Boolean.FALSE::equals)
- .retryIfExceptionOfType(Exception.class)
- .withStopStrategy(StopStrategies.neverStop())
- .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
- .build();
- try {
- retryer.call(new Callable<Boolean>() {
- AtomicBoolean isSuccess = new AtomicBoolean(false);
- @Override
- public Boolean call() throws Exception {
- log.info("喷码机连接重试");
- future = bootstrap.connect(IP_ADDR, PORT).sync();
- future.addListener((channelFuture) -> {
- isSuccess.set(channelFuture.isSuccess());
- if (channelFuture.isSuccess()) {
- initRoutine();
- SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
- log.info("喷码机连接成功");
- } else {
- log.info("连接失败等待重试!");
- }
- });
- Thread.sleep(3000);
- return isSuccess.get();
- }
- });
- } catch (RetryException | ExecutionException e) {
- log.error("喷码机连接故障,msg:",e);
- }
- }
- @Override
- public void close() {
- SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "2"));
- group.shutdownGracefully();
- }
- @Override
- public String getDeviceName() {
- return deviceName;
- }
- @Override
- public ChannelPipeline getChannel() {
- return pipeline;
- }
- @Override
- public void processMessages(String message) {
- log.info("喷码器消息:{}", message);
- if ("f7".equalsIgnoreCase(message)) {
- log.info("喷码机打印成功!");
- }
- if ("6".equals(message)) {
- log.info(movement + "完成");
- }
- }
- /**
- * 初始化打印设置
- */
- private void initRoutine() {
- if (!isInitRoutine) {
- log.info("初始化打印设置");
- movement = "更新喷码机喷码内容";
- pipeline.writeAndFlush(Unpooled.copiedBuffer(ParseMsgTools.hexString2Bytes(InkjetPrintersDirectivesEnum.CODE41.getCode())));
- isInitRoutine = true;
- }
- }
- }
|