package com.casic.missiles.client; import com.casic.missiles.core.util.ToolUtil; import com.casic.missiles.modular.system.service.IHCNetService; import com.casic.missiles.modular.system.service.impl.HCNetServiceImpl; import com.casic.missiles.modular.system.util.HttpClientUtils; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.concurrent.ConcurrentHashMap; @Slf4j @Component public class ClientHandler extends ChannelInboundHandlerAdapter { private static IHCNetService ihcNetService; @Autowired public void setIhcNetService(IHCNetService ihcNetService) { this.ihcNetService = ihcNetService; } // @PostConstruct // public void init() { // mongoFileOperationUtil = this; // mongoFileOperationUtil.dsForRW = this.dsForRW; // } public String userId; public ClientHandler(String userId) { this.userId = userId; } public ClientHandler() { } public static ConcurrentHashMap<String, Channel> concurrentHashMap = new ConcurrentHashMap(); ChannelHandlerContext ctx; public void sendMessage(String msgToSend) { if (ctx != null) { ctx.channel().writeAndFlush(msgToSend); // ChannelFuture cf = ctx.writeAndFlush(Unpooled.copiedBuffer(msgToSend, CharsetUtil.UTF_8)); // if (!cf.isSuccess()) { // System.out.println("Send failed: " + cf.cause()); // } } else { //ctx not initialized yet. you were too fast. do something here } } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { this.ctx = ctx; ChannelUtil.addChannel(userId, ctx.channel()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ChannelUtil.removeChannelByName(userId); ctx.close(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); //接收指令和ai消息 log.info("用户:" + userId + "接收到消息-->" + msg); String receiveData = (String) msg; try { if (ToolUtil.isNotEmpty(receiveData)) { //控制命令 if (receiveData.indexOf("^") == 0) { String[] codeArr= receiveData.split(";"); for(int i=0;i<codeArr.length;i++){ if(ToolUtil.isNotEmpty(codeArr[i])){ String[] msgArr = codeArr[i].split(":"); ihcNetService.setBrCommand("192.168.1.8", msgArr[0].substring(1, msgArr[0].length()), Integer.parseInt(msgArr[1])); Thread.sleep(1000); } } }//ai指令 ai:1:1 指令标识、设备ID、AI状态(1是开启,0关闭) else if (receiveData.indexOf("ai") == 0) { String[] msgArr = receiveData.split(":"); String flag = msgArr[2]; HttpClientUtils.setFlag(flag); HttpClientUtils.setDevCode(msgArr[1]); } } } catch (Exception e) { e.printStackTrace(); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); ChannelUtil.removeChannelByName(userId); } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }