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);
|
}
|
|
}
|
}
|
|
|
|
|
|
|
|
|
|
|
}
|