上饶市公安局wvp平台
zhongrj
2023-03-07 c3d3acc1f7f02caee2cc7dfe47ef8217ffcb7965
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.genersoft.iot.vmp.netty.handle;
 
import com.genersoft.iot.vmp.netty.business.entity.TalkBackEquipment;
import com.genersoft.iot.vmp.netty.business.service.TalkBackEquipmentService;
import com.genersoft.iot.vmp.netty.config.MyDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import okio.Utf8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import static com.genersoft.iot.vmp.netty.config.MyDecoder.hexToByte;
import static com.genersoft.iot.vmp.netty.config.MyDecoder.hexTohort;
import static com.genersoft.iot.vmp.netty.util.Hex.getDouble;
import static com.genersoft.iot.vmp.netty.util.Hex.hexStr2Str;
 
/**
 * updHandler udp 服务端数据接收处理
 * @author zhongrj
 * @date 2023-02-21
 */
@Component
public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private static UdpServerHandler udpServerHandler;
 
    @Autowired
    private TalkBackEquipmentService talkBackEquipmentService;
 
 
    @PostConstruct
    public void init() {
        udpServerHandler = this;
        udpServerHandler.talkBackEquipmentService = this.talkBackEquipmentService;
    }
 
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
        // 读取收到的数据
        ByteBuf buf = (ByteBuf) datagramPacket.copy().content();
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);
        String body = new String(req, CharsetUtil.UTF_8);
 
        System.out.println("【UDP】>>>>>> 收到客户端的数据:"+body);
        String s = bytesToHexString(req);
        System.out.println("s111111 = " + s);
        //获取字符串的长度
        int length = s.length();
        System.out.println("数据长度>>>>>> = " + length);
        //数据解析
        dataHandler(s);
    }
 
    /**
     * 数据解析
     * @param s 16位字符串
     */
    private void dataHandler(String s) {
//        s = "AAAACCCC220000000033373933343732393100000000000000000000000052D50451F77D5D406C04E275FD723C400000015F0000000507E70306103B0A";
        //创建设备对象
        TalkBackEquipment backEquipment = new TalkBackEquipment();
        if (s.substring(4,8).equals("CCCC")) {
            backEquipment.setStatus(1);
        }
        //断开连接
        if (s.substring(4,8).equals("FFFF")) {
            backEquipment.setStatus(0);
        }
        //截取字符串
        backEquipment.setTerminalNumber(hexStr2Str(s.substring(20, 60)));
 
        backEquipment.setLongitude(Double.toString(getDouble(s.substring(60, 76))));
 
        backEquipment.setLatitude(Double.toString(getDouble(s.substring(76, 92))));
 
        backEquipment.setSpeed(Short.toString(hexTohort(s.substring(92, 96))));
        backEquipment.setDirection(Short.toString(hexTohort(s.substring(96, 100))));
        backEquipment.setElevation(Short.toString(hexTohort(s.substring(100, 104))));
        backEquipment.setPrecisions(Short.toString(hexTohort(s.substring(104, 108))));
 
        String year = Short.toString(hexTohort(s.substring(108, 112)));
 
        String month = Short.toString(hexToByte(s.substring(112, 114)));
        if (Integer.parseInt(month)<10){
            month = "0" + month;
        }
        String day = Short.toString(hexToByte(s.substring(114, 116)));
        if (Integer.parseInt(day)<10){
            day = "0" + day;
        }
        String hour = Short.toString(hexToByte(s.substring(116, 118)));
        if (Integer.parseInt(hour)<10){
            hour = "0" + hour;
        }
        String minute = Short.toString(hexToByte(s.substring(118, 120)));
        if (Integer.parseInt(minute)<10){
            minute = "0" + minute;
        }
        String second = Short.toString(hexToByte(s.substring(120, 122)));
        if (Integer.parseInt(second)<10){
            second = "0" + second;
        }
        String time = year + "-" + month + "-" + day +" " + hour+ ":"+minute + ":" + second;
        System.out.println("接收时间: time = " + time);
        //时间拼接
        try {
            backEquipment.setReceiveTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        backEquipment.setUpdateTime(new Date());
        // 新增
        udpServerHandler.talkBackEquipmentService.save(backEquipment);
    }
 
    /**
     * 转换
     * @param bArray
     * @return
     */
    public String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
 
    /**
     * 保存设备数据
     * @param body
     */
    @Async
    public void saveTalkBackEquipmentInfo(String body) {
        TalkBackEquipment equipmentRecord = new TalkBackEquipment();
        equipmentRecord.setName("ces");
        equipmentRecord.setCode("cs123456");
        equipmentRecord.setStatus(1);
        equipmentRecord.setUnitName("CSDW");
        equipmentRecord.setUnitCode("CSDW123456");
        equipmentRecord.setPoliceName("JINGYUAN");
        equipmentRecord.setPoliceCode("JINGYUAN123456");
        equipmentRecord.setTerminalNumber("788888888888");
        equipmentRecord.setLongitude("128.124124412");
        equipmentRecord.setLatitude("27.1224521421");
        equipmentRecord.setSpeed("10");
        equipmentRecord.setDirection("10");
        equipmentRecord.setElevation("10");
        equipmentRecord.setPrecisions("10");
        equipmentRecord.setReceiveTime(new Date());
        equipmentRecord.setCreateTime(new Date());
        equipmentRecord.setUpdateTime(new Date());
 
    }
 
    /**
     * 捕获异常
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
        logger.error("UdpServerHandler exceptionCaught"+cause.getMessage());
        System.out.println("UdpServerHandler exceptionCaught"+cause.getMessage());
        cause.printStackTrace();
        ctx.close();
    }
 
    /**
     * 消息没有结束的时候触发
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}