package com.gzlh.device.plc.client; import com.gzlh.bus.EventConfig; import com.gzlh.config.dto.SerialSetting; import com.gzlh.device.plc.service.PLCService; import com.gzlh.utils.DeviceCache; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Field; import java.util.*; @Slf4j @ChannelHandler.Sharable public class PlcClientHandler extends SimpleChannelInboundHandler { private PlcNettyConfig ledNettyConfig; private PLCService plcService = new PLCService(); public PlcClientHandler(PlcNettyConfig ledNettyConfig) { this.ledNettyConfig = ledNettyConfig; } @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { log.info("PLC 客户端收到消息:" + msg); DeviceCache.put("plc",1); String result = Integer.toBinaryString(Integer.parseInt(msg.substring(6, 8),16)); String oldPlcInfo = DeviceCache.getPlcStatus(); String newPlcInfo = String.format("%8s", result).replace(" ", "0"); // 将plc的设备信息补到8存入缓存 DeviceCache.setPlcStatus(newPlcInfo); // 设备信息发生变化时进入 if (oldPlcInfo!=null&&!newPlcInfo.equals(oldPlcInfo)){ SerialSetting.PlcDTO.StatusDTO status = EventConfig.serialSetting.getPlc().getStatus(); String newStr = new StringBuffer(newPlcInfo).reverse().toString(); String oldStr = new StringBuffer(oldPlcInfo).reverse().toString(); for (int i=0;i { try { plcService.fRedTrigger(newPlcInfo); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } // 雷达外触发时调用方法 if (String.valueOf(i).equals(status.getRadarPoint())){ new Thread(()-> { try { plcService.radarTrigger(); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } // 上到位触发时调用方法 if (String.valueOf(i).equals(status.getUpPoint())){ new Thread(()-> { try { plcService.upTrigger(); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } } } } // for (int i=0;i