package com.thhy.materials.modules.biz.helmet.smoke; import com.alibaba.fastjson.JSONObject; import com.thhy.materials.modules.biz.helmet.entity.TDataSmock; import com.thhy.materials.modules.biz.helmet.mapper.HelmetMapper; import com.thhy.materials.modules.biz.helmet.service.HelmetService; import com.thhy.materials.modules.biz.utils.ApplicationContextUtil; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.CharsetUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @Component public class SmokeMessage extends SimpleChannelInboundHandler{ @Autowired public HelmetService helmetService; private static String[] alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"}; @Override public void channelActive(ChannelHandlerContext ctx) { for(int i=0; i<10; i++) { StringBuilder builder = new StringBuilder(); builder.append("这是第"); builder.append(i); builder.append("条消息, 内容是:"); for(int j=0; j<100; j++) { builder.append(alphabets[i]); } builder.append("......"); builder.append("#"); System.out.println(builder.toString().getBytes().length); ctx.writeAndFlush(Unpooled.copiedBuffer(builder.toString(), CharsetUtil.UTF_8)); } } @Override public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) { System.out.println("客户端接收到消息: " + in.toString(CharsetUtil.UTF_8)); JSONObject jsonObject11 = JSONObject.parseObject(in.toString(CharsetUtil.UTF_8)); System.out.println("客户端接收到消息: " + jsonObject11); TDataSmock tDataSmock = new TDataSmock(); tDataSmock.setGatewayCyc(jsonObject11.get("gateway_cyc").toString()); tDataSmock.setFans(jsonObject11.get("FanS").toString()); tDataSmock.setV1run(jsonObject11.get("V1Run").toString()); tDataSmock.setUv1run(jsonObject11.get("UV1Run").toString()); tDataSmock.setFanrun(jsonObject11.get("FanRun").toString()); tDataSmock.setUv2run(jsonObject11.get("UV2Run").toString()); tDataSmock.setSsvrun(jsonObject11.get("SSVRun").toString()); tDataSmock.setDp(jsonObject11.get("DP").toString()); tDataSmock.setT(jsonObject11.get("T").toString()); tDataSmock.setOc(jsonObject11.get("OC").toString()); tDataSmock.setGatew(jsonObject11.get("gatew").toString()); tDataSmock.setNxrun(jsonObject11.get("NXRun").toString()); tDataSmock.setTimes(jsonObject11.get("time").toString()); tDataSmock.setRtcVolt(jsonObject11.get("RTC_VOLT").toString()); tDataSmock.setV2run(jsonObject11.get("V2Run").toString()); tDataSmock.setDxqvrun(jsonObject11.get("DXQVRun").toString()); tDataSmock.setPress(jsonObject11.get("Press").toString()); tDataSmock.setDc(jsonObject11.get("Dc").toString()); System.out.println("-------"+tDataSmock); getUserService().smockIn(tDataSmock); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } public static HelmetService getUserService(){ ApplicationContext ac = ApplicationContextUtil.getApplicationContext(); HelmetService userService = (HelmetService) ac.getBean("helmetServiceImpl"); return userService; } }