郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Scheduls.location;
 
import com.alibaba.fastjson.JSONObject;
import com.gk.hotwork.Config.Oauth2.IRedisService;
import com.gk.hotwork.Domain.Exception.BusinessException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
 
 
//@Configuration
//@EnableScheduling
//@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class LocationReceiver {
    private Logger logger = LogManager.getLogger(getClass());
 
    private boolean isopen = false;
 
    @Value("${location.port}")
    private int receiverPort;
    @Autowired
    private IRedisService redisService;
 
    @Scheduled(initialDelay = 10000, fixedDelay = 30000)
    public void start() {
        if (isopen) {
            return;
        }
        try {
            isopen = true;
            DatagramSocket server = new DatagramSocket(receiverPort);
            // 2,准备接收容器
            byte[] container = new byte[1024];
            // 3,封装成包 new DatagramPacket(byte[] b,int length)
            DatagramPacket receiver = new DatagramPacket(container, container.length);
            logger.error("UDP服务端"+receiverPort+"已经开启,等待设备上报数据......");
            while (true) {
                try {
                    //服务器端接收来自客户端的数据
                    server.receive(receiver);
                    InetAddress address = receiver.getAddress();//获取发送端的 IP 对象
                    byte[] data = receiver.getData();//获取接收到的数据
                    int length = receiver.getLength();//获取具体收到数据的长度
                    String dataStr = new String(data, 0, length);
//                    handleData(dataStr);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
 
    public  void handleData(String dataStr) {
        String content = "";
        if (dataStr.startsWith("json")) {
            //括号截取
            int i = dataStr.indexOf("{");
            int j = dataStr.lastIndexOf("}");
            content = dataStr.substring(i, j + 1);
        }
 
        else if (dataStr.startsWith("base64-json")) {
            //,分隔 第4个
            String base64Content = dataStr.split(",")[3];
            content = new String(Base64.getDecoder().decode(base64Content), StandardCharsets.UTF_8);
 
        }
 
        else{
            throw new BusinessException("协议头对应不上");
        }
 
        JSONObject contentObj = JSONObject.parseObject(content);
        String params = contentObj.getString("params");
        String method = contentObj.getString("method");
 
        //根据method判断处理方式
 
        {
 
            String standardMethod = method.toLowerCase();
            if ("location".equals(standardMethod)) {
                //DeviceLocation deviceLocation = JSONObject.parseObject(params, DeviceLocation.class);
                String key = "device:location";
                redisService.setDbIndex(1);
                redisService.setExpiredTime(key,30*60);
                redisService.rightPush(key, params);
            }
            if ("ping".equals(standardMethod)) {
                //DevicePing devicePing = JSONObject.parseObject(params, DevicePing.class);
                String key = "device:ping";
                redisService.setDbIndex(2);
                redisService.setExpiredTime(key,30*60);
                redisService.rightPush(key, params);
            }
            if ("presskey".equals(standardMethod)) {
                //DevicePressKey devicePressKey = JSONObject.parseObject(params, DevicePressKey.class);
                String key = "device:presskey";
                redisService.setDbIndex(3);
                redisService.setExpiredTime(key,30*60);
                redisService.rightPush(key, params);
            }
 
        }
    }
 
 
 
 
 
 
 
 
 
 
}