UserKafkaServiceImpl.java
4.44 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
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
package com.glsx.smart.service.impl;
import com.alibaba.fastjson.JSON;
import com.glsx.platform.user.entity.UserAddressSmart;
import com.glsx.platform.user.service.UserAddressSmartService;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartScene;
import com.glsx.smart.dao.entity.SmartSceneData;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.dao.mapper.SmartUserRelationMapper;
import com.glsx.smart.pojo.DeviceMessage;
import com.glsx.smart.service.UserKafkaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author think.wang
* @version 1.0.0
* @Title UserKafkaServiceImpl
* @Package com.glsx.smart.service.impl
* @Description 用户经纬度kafka推送
* @date 2021/4/10
*/
@Slf4j
@Service
public class UserKafkaServiceImpl implements UserKafkaService {
@Value("${smart_device_type_logger}")
private String logger;
@Value("${smart_device_type_gps}")
private String gps;
@DubboReference(version = "1.0.0")
private UserAddressSmartService userAddressSmartService;
@Resource
private KafkaTemplate kafkaTemplate;
@Resource
private SmartUserRelationMapper userRelationMapper;
@Override
public void addDeviceData(SmartScene scene) {
//先获取需要推送设备
List<String> deviceList = this.getSendDeviceList(scene.getDataList());
if(deviceList.isEmpty()){
return;
}
UserAddressSmart userAddress = this.getPointUser(scene.getUserId());
if(userAddress == null){ //用户不存在家位置直接返回
return;
}
String lngAndLat = userAddress.getLng() + ":" + userAddress.getLat();
log.info("推送的设备:{} 及其经纬度:{}", deviceList.toString(), lngAndLat);
for (String deviceId : deviceList) {
this.pushUserDeviceData(deviceId, lngAndLat, 1);
}
}
/**
* 获取推送设备列表
* @param dataList
* @return
*/
@Override
public List<String> getSendDeviceList(List<SmartSceneData> dataList) {
//先获取需要推送设备
List<String> deviceList = new ArrayList<>();
int dataLen = dataList.size();
for (int i = 0; i < dataLen; i++) {
SmartSceneData data = dataList.get(i);
if(Constants.SCENE_CONDITION_STATUS.equals(data.getParamKey()) && data.getType() == 1){
//获取设备类型
String deviceType = JSON.parseObject(data.getParamValue()).getString("deviceType");
if(logger.equals(deviceType) || gps.equals(deviceType)){
deviceList.add(data.getDeviceId());
}
}
}
return deviceList;
}
/**
* 获取车家智能用户家位置
* @param userId
* @return
*/
@Override
public UserAddressSmart getPointUser(Long userId) {
SmartUserRelation userRelation = userRelationMapper.selectByPrimaryKey(userId);
List<UserAddressSmart> userAddressList =
userAddressSmartService.getByUserId(userRelation.getUserId().intValue());
if(userAddressList.isEmpty()){
return null;
}
//获取用户经纬度
int len = userAddressList.size();
for (int i = 0; i < len; i++) {
UserAddressSmart addressSmart = userAddressList.get(i);
if(addressSmart.getAddType().equals(1)){ //用户是否存在家位置
return addressSmart;
}
}
return null;
}
/**
* 推送设备经纬度数据
* @param deviceId
* @param lngAndLat
* @param type 1新增 2更新 3删除
*/
@Override
public void pushUserDeviceData(String deviceId, String lngAndLat, Integer type) {
//kafka 发送消息数据封装
DeviceMessage message = new DeviceMessage();
message.setDeviceId(deviceId);
message.setLngAndLat(lngAndLat);
message.setType(type);
log.info("推送kafka topic【{}】的数据:{}", Constants.DEVICE_DATA_TOPIC, message.toString());
//推送kafka
kafkaTemplate.send(Constants.DEVICE_DATA_TOPIC, JSON.toJSONString(message));
}
}