DeviceAddressProducer.java
3.64 KB
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
package com.glsx.smart.config;
import com.alibaba.fastjson.JSON;
import com.glsx.smart.pojo.pb.InstanceFence.Fence;
import com.glsx.smart.utils.GCJ2WGS;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
/**
* @author think.wang
* @version 1.0.0
* @Title DeviceAddressProducer
* @Package com.glsx.smart.config
* @Description 设备围栏数据kafka推送
* @date 2021/4/11
*/
@Slf4j
@Component
public class DeviceAddressProducer {
@Resource
private Producer producer;
@Autowired
private KafkaConfig kafkaConfig;
/**
* 推送设备经纬度数据
* @param deviceId
* @param lngAndLat
* @param value 1新增 2删除 4更新
*/
public void pushUserDeviceData(String deviceId, String lngAndLat, Integer value) {
//kafka 发送消息数据封装
HashMap<String, Double> hm;
if(value == Fence.OperationType.DEL_VALUE
|| value == Fence.OperationType.GET_VALUE){ //删除和获取不需要经纬度数据
hm = new HashMap<>(2);
hm.put("lat", Double.valueOf(0));
hm.put("lon", Double.valueOf(0));
} else{
GCJ2WGS wg = new GCJ2WGS();
String[] lngLat = lngAndLat.split(":");
hm = wg.delta(Double.valueOf(lngLat[1]), Double.valueOf(lngLat[0]));
}
Fence.GpsLoc gpsLoc = this.buildGpsLoc(hm.get("lon"), hm.get("lat"));
Fence.FenceDeviceFamilyInfo deviceFamilyInfo = this.buildFenceDevice(deviceId, gpsLoc);
Fence.OptFamilyFenceInfo familyFenceInfo = this.buildFamilyFence(value, deviceFamilyInfo);
try {
String topic = kafkaConfig.getDeviceTopic();
log.info("推送kafka设备地址:{} {} {}", deviceId, lngAndLat, value);
log.info("推送kafka topic【{}】的数据:{} {} {}", topic, deviceId, JSON.toJSONString(hm), value);
KeyedMessage<byte[], byte[]> message = new KeyedMessage<>(topic, deviceId.getBytes(), familyFenceInfo.toByteArray());
producer.send(message);
}catch (Exception e){
log.error("消息推送kafka失败:{}", e);
}
}
/**
* 围栏设备经纬度数据封装
* @param lng
* @param lat
* @return
*/
private Fence.GpsLoc buildGpsLoc(double lng, double lat){
Fence.GpsLoc.Builder gpsLocBuilder = Fence.GpsLoc.newBuilder();
gpsLocBuilder.setLng((int)(lng * 1000000));
gpsLocBuilder.setLat((int)(lat * 1000000));
return gpsLocBuilder.build();
}
/**
* 围栏设备数据封装
* @param deviceId
* @param gpsLoc
* @return
*/
private Fence.FenceDeviceFamilyInfo buildFenceDevice(String deviceId, Fence.GpsLoc gpsLoc){
Fence.FenceDeviceFamilyInfo.Builder fenceDeviceBuilder = Fence.FenceDeviceFamilyInfo.newBuilder();
fenceDeviceBuilder.setSn(deviceId);
fenceDeviceBuilder.addGpslocs(gpsLoc);
return fenceDeviceBuilder.build();
}
/**
* 围栏整个数据封装
* @param value
* @param familyInfo
* @return
*/
private Fence.OptFamilyFenceInfo buildFamilyFence(Integer value, Fence.FenceDeviceFamilyInfo familyInfo){
Fence.OptFamilyFenceInfo.Builder familyFenceBuilder = Fence.OptFamilyFenceInfo.newBuilder();
familyFenceBuilder.addFamilyfenceInfos(familyInfo);
familyFenceBuilder.setOperationType(Fence.OperationType.valueOf(value));
return familyFenceBuilder.build();
}
}