Commit b6dc18cd by wnb0128

车家智能数据服务 推送kafka--think.wang

1 parent 85d88ef1
...@@ -67,6 +67,12 @@ ...@@ -67,6 +67,12 @@
<artifactId>rest-admin-kafka</artifactId> <artifactId>rest-admin-kafka</artifactId>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.kafka</groupId>-->
<!-- <artifactId>spring-kafka</artifactId>-->
<!-- <version>2.5.6.RELEASE</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.glsx</groupId> <groupId>com.glsx</groupId>
<artifactId>rest-admin-swagger2</artifactId> <artifactId>rest-admin-swagger2</artifactId>
......
package com.glsx.smart.config;
import com.alibaba.fastjson.JSON;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.pojo.DeviceMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @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 KafkaTemplate kafkaTemplate;
/**
* 推送设备经纬度数据
* @param deviceId
* @param lngAndLat
* @param type 1新增 2更新 3删除
*/
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));
}
}
...@@ -6,6 +6,7 @@ import com.glsx.smart.pojo.OrderResponse; ...@@ -6,6 +6,7 @@ import com.glsx.smart.pojo.OrderResponse;
import com.glsx.smart.service.SmartSceneDataService; import com.glsx.smart.service.SmartSceneDataService;
import com.glsx.smart.service.SmartSceneLogService; import com.glsx.smart.service.SmartSceneLogService;
import com.glsx.smart.service.SmartSceneService; import com.glsx.smart.service.SmartSceneService;
import com.glsx.smart.service.UserKafkaService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -39,6 +40,9 @@ public class SceneController { ...@@ -39,6 +40,9 @@ public class SceneController {
@Autowired @Autowired
private SmartSceneDataService sceneDataService; private SmartSceneDataService sceneDataService;
@Autowired
private UserKafkaService userKafkaService;
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public List<SmartScene> getList(@RequestBody SmartScene scene) { public List<SmartScene> getList(@RequestBody SmartScene scene) {
log.info("/scene/getList param: {}", JSON.toJSONString(scene)); log.info("/scene/getList param: {}", JSON.toJSONString(scene));
...@@ -61,6 +65,7 @@ public class SceneController { ...@@ -61,6 +65,7 @@ public class SceneController {
log.info("用户新增调用/scene/add param: {}", JSON.toJSONString(scene)); log.info("用户新增调用/scene/add param: {}", JSON.toJSONString(scene));
boolean flag = sceneService.insert(scene) > 0 ? true : false; boolean flag = sceneService.insert(scene) > 0 ? true : false;
if(flag){ if(flag){
userKafkaService.addDeviceData(scene); //推送数据至kafka
sceneLogService.insertWrapper(scene.getUserId(), sceneLogService.insertWrapper(scene.getUserId(),
"新增场景数据:" + scene.getId(), JSON.toJSONString(scene)); "新增场景数据:" + scene.getId(), JSON.toJSONString(scene));
} }
...@@ -73,6 +78,7 @@ public class SceneController { ...@@ -73,6 +78,7 @@ public class SceneController {
log.info("用户更新调用/scene/update param: {}", JSON.toJSONString(scene)); log.info("用户更新调用/scene/update param: {}", JSON.toJSONString(scene));
boolean flag = sceneService.update(scene) > 0 ? true : false; boolean flag = sceneService.update(scene) > 0 ? true : false;
if(flag){ if(flag){
userKafkaService.addDeviceData(scene); //推送数据至kafka
sceneLogService.insertWrapper(scene.getUserId(), sceneLogService.insertWrapper(scene.getUserId(),
"更新场景数据:" + scene.getId(), JSON.toJSONString(scene)); "更新场景数据:" + scene.getId(), JSON.toJSONString(scene));
} }
......
package com.glsx.smart.controller; package com.glsx.smart.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.glsx.smart.config.DeviceAddressProducer;
import com.glsx.smart.dao.entity.SmartScene; import com.glsx.smart.dao.entity.SmartScene;
import com.glsx.smart.dao.entity.SmartUserRelation; import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.*; import com.glsx.smart.service.*;
...@@ -39,6 +40,9 @@ public class UserKafkaController { ...@@ -39,6 +40,9 @@ public class UserKafkaController {
@Autowired @Autowired
private SmartSceneDataService sceneDataService; private SmartSceneDataService sceneDataService;
@Autowired
private DeviceAddressProducer addressProducer;
/** /**
* 更新用户场景 * 更新用户场景
* *
...@@ -69,7 +73,7 @@ public class UserKafkaController { ...@@ -69,7 +73,7 @@ public class UserKafkaController {
if(CollectionUtils.isNotEmpty(deviceList)){ if(CollectionUtils.isNotEmpty(deviceList)){
for (String deviceId : deviceList) { for (String deviceId : deviceList) {
//更新设备家位置 //更新设备家位置
userKafkaService.pushUserDeviceData(deviceId, lngAndLat, 2); addressProducer.pushUserDeviceData(deviceId, lngAndLat, 2);
} }
} }
} }
...@@ -92,7 +96,7 @@ public class UserKafkaController { ...@@ -92,7 +96,7 @@ public class UserKafkaController {
sceneDataService.getBySceneId(scene.getId())); sceneDataService.getBySceneId(scene.getId()));
if (CollectionUtils.isNotEmpty(deviceList)) { if (CollectionUtils.isNotEmpty(deviceList)) {
for (String deviceId : deviceList) { for (String deviceId : deviceList) {
userKafkaService.pushUserDeviceData(deviceId, null, 3); //删除设备信息 addressProducer.pushUserDeviceData(deviceId, null, 3); //删除设备信息
} }
} }
//删除用户场景数据 //删除用户场景数据
......
...@@ -14,5 +14,4 @@ public interface UserKafkaService { ...@@ -14,5 +14,4 @@ public interface UserKafkaService {
UserAddressSmart getPointUser(Long userId); UserAddressSmart getPointUser(Long userId);
void pushUserDeviceData(String deviceId, String lngAndLat, Integer type);
} }
...@@ -3,17 +3,17 @@ package com.glsx.smart.service.impl; ...@@ -3,17 +3,17 @@ package com.glsx.smart.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.glsx.platform.user.entity.UserAddressSmart; import com.glsx.platform.user.entity.UserAddressSmart;
import com.glsx.platform.user.service.UserAddressSmartService; import com.glsx.platform.user.service.UserAddressSmartService;
import com.glsx.smart.config.DeviceAddressProducer;
import com.glsx.smart.constant.Constants; import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartScene; import com.glsx.smart.dao.entity.SmartScene;
import com.glsx.smart.dao.entity.SmartSceneData; import com.glsx.smart.dao.entity.SmartSceneData;
import com.glsx.smart.dao.entity.SmartUserRelation; import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.dao.mapper.SmartUserRelationMapper; import com.glsx.smart.dao.mapper.SmartUserRelationMapper;
import com.glsx.smart.pojo.DeviceMessage;
import com.glsx.smart.service.UserKafkaService; import com.glsx.smart.service.UserKafkaService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -41,8 +41,8 @@ public class UserKafkaServiceImpl implements UserKafkaService { ...@@ -41,8 +41,8 @@ public class UserKafkaServiceImpl implements UserKafkaService {
@DubboReference(version = "1.0.0") @DubboReference(version = "1.0.0")
private UserAddressSmartService userAddressSmartService; private UserAddressSmartService userAddressSmartService;
@Resource @Autowired
private KafkaTemplate kafkaTemplate; private DeviceAddressProducer addressProducer;
@Resource @Resource
private SmartUserRelationMapper userRelationMapper; private SmartUserRelationMapper userRelationMapper;
...@@ -61,7 +61,7 @@ public class UserKafkaServiceImpl implements UserKafkaService { ...@@ -61,7 +61,7 @@ public class UserKafkaServiceImpl implements UserKafkaService {
String lngAndLat = userAddress.getLng() + ":" + userAddress.getLat(); String lngAndLat = userAddress.getLng() + ":" + userAddress.getLat();
log.info("推送的设备:{} 及其经纬度:{}", deviceList.toString(), lngAndLat); log.info("推送的设备:{} 及其经纬度:{}", deviceList.toString(), lngAndLat);
for (String deviceId : deviceList) { for (String deviceId : deviceList) {
this.pushUserDeviceData(deviceId, lngAndLat, 1); addressProducer.pushUserDeviceData(deviceId, lngAndLat, 1);
} }
} }
...@@ -112,21 +112,4 @@ public class UserKafkaServiceImpl implements UserKafkaService { ...@@ -112,21 +112,4 @@ public class UserKafkaServiceImpl implements UserKafkaService {
return null; 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));
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!