张晓波
2023-10-30 4bc74cce28fd8c01433bed66b4d36da0ba9b3f79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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<ByteBuf>{
 
    @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;
    }
 
}