Commit 7ee160d2 by 杨林龙

优化设备激活定时任务填充数据方案

1 parent 705f1fa3
......@@ -48,7 +48,7 @@ public class DeviceActiveController implements DeviceActiveClient {
@ApiOperation(value = "手动更新设备激活定位点")
@SysLog(module = "设备激活", value = "手动更新设备激活定位点", action = OperateType.QUERY, saveLog = false)
public R saveActiveInfoFlush(@RequestBody List<String> snList) {
deviceActiveService.saveActiveInfo(snList);
deviceActiveService.saveActiveInfoByTask(snList);
return R.ok();
}
......
......@@ -5,6 +5,7 @@ import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Collection;
import java.util.List;
@Mapper
......@@ -25,4 +26,12 @@ public interface DeviceMapper extends CommonBaseMapper<Device> {
* @return
*/
Device getDeviceBySn(@Param("sn") String sn);
/**
* 根据设备号查找设备信息
*
* @param sns 设备号
* @return
*/
List<Device> selectBySns(@Param("sns") Collection<String> sns);
}
\ No newline at end of file
......@@ -74,4 +74,57 @@ public class AsyncTasks {
log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
}
}
/**
* 异步填充设备激活经纬度和激活地址信息
*
* @param snList
* @param countDownLatch
* @param remoteGpsBsService
*/
@Async
public void saveActiveInfoByTask(List<String> snList, CountDownLatch countDownLatch, RemoteGpsBsService remoteGpsBsService, VehicleDeviceMapper vehicleDeviceMapper) {
if (CollectionUtils.isEmpty(snList)) {
log.info("countDownLatch查询数据为空关闭资源");
countDownLatch.countDown();
return;
}
try {
log.info("开始分批同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
VehicleDevice vehicleDevice = vehicleDeviceMapper.selectBindingDeviceBySn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(gpsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtil.parseDateTime(bsLocation.getDateTime()));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
}
}
// 保存激活信息
log.info("active device info:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateByPrimaryKeySelective(vehicleDevice);
});
} catch (Exception e) {
log.info("saveActiveInfo异常信息===" + e.getMessage());
} finally {
countDownLatch.countDown();
log.info(Thread.currentThread().getName() + "countDownLatch关闭资源");
}
}
}
package cn.com.glsx.stdinterface.modules.remote;
import cn.com.glsx.devicemonitor.api.dto.request.GpsBsFirstBatchRequest;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.enums.DeviceTypeRelationEnum;
import cn.com.glsx.devicemonitor.api.feign.GpsBsFeignService;
import cn.com.glsx.stdinterface.modules.entity.Device;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.DeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.service.VehicleDeviceService;
import cn.hutool.core.date.DateUtil;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
......@@ -12,6 +20,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
......@@ -29,6 +38,13 @@ public class RemoteGpsBsService {
@Autowired
private GpsBsFeignService gpsBsFeignService;
@Autowired
private DeviceMapper deviceMapper;
@Autowired
private VehicleDeviceMapper vehicleDeviceMapper;
/**
* 通过经纬度获取地址
*
......@@ -55,8 +71,9 @@ public class RemoteGpsBsService {
* @return
*/
public Map<String, GpsBsData> getLastGpsBsMapBySns(List<String> snList) {
Map<String, Integer> snsMap = Maps.newHashMap();
snList.forEach(sn -> snsMap.put(sn, DeviceTypeRelationEnum.GPS.getBizDeviceType()));
List<Device> deviceList = deviceMapper.selectBySns(snList);
//注意这里过滤了设备类型为空的数据,如果设备不在线,要检查设备类型数据是否同步或设置
Map<String, Integer> snsMap = deviceList.stream().filter(m -> m.getDeviceType() != null).collect(Collectors.toMap(Device::getSn, Device::getDeviceType, (o, n) -> n));
R<List<GpsBsData>> listR = gpsBsFeignService.getLastGpsBsBySns(snsMap);
List<GpsBsData> gpsBsDataList = listR.getDataList(new TypeToken<List<GpsBsData>>() {
}.getType());
......@@ -75,11 +92,33 @@ public class RemoteGpsBsService {
* @return
*/
public List<GpsBsData> getLastGpsBsListBySns(List<String> snList) {
Map<String, Integer> snsMap = Maps.newHashMap();
snList.forEach(sn -> snsMap.put(sn, DeviceTypeRelationEnum.GPS.getBizDeviceType()));
List<Device> deviceList = deviceMapper.selectBySns(snList);
//注意这里过滤了设备类型为空的数据,如果设备不在线,要检查设备类型数据是否同步或设置
Map<String, Integer> snsMap = deviceList.stream().filter(m -> m.getDeviceType() != null).collect(Collectors.toMap(Device::getSn, Device::getDeviceType, (o, n) -> n));
R<List<GpsBsData>> listR = gpsBsFeignService.getLastGpsBsBySns(snsMap);
List<GpsBsData> gpsBsDataList = listR.getDataList(new TypeToken<List<GpsBsData>>() {
}.getType());
return gpsBsDataList;
}
/**
* 根据设备号和激活时间获取激活定位点经纬度数据
*
* @return
*/
public List<GpsBsData> getFirstGpsBsBatch(List<String> snList) {
List<GpsBsFirstBatchRequest> gpsBsFirstBatchRequestList = Lists.newArrayList();
List<VehicleDevice> vehicleDeviceList = vehicleDeviceMapper.selectBindingDevices(snList);
vehicleDeviceList.stream().filter(vehicleDevice -> vehicleDevice.getCreatedDate() != null).forEach(vehicleDevice -> {
GpsBsFirstBatchRequest gpsBsFirstBatchRequest = new GpsBsFirstBatchRequest();
gpsBsFirstBatchRequest.setSn(vehicleDevice.getSn());
gpsBsFirstBatchRequest.setActiveDate(DateUtil.formatDateTime(vehicleDevice.getCreatedDate()));
gpsBsFirstBatchRequest.setDeviceTypeRelationEnum(DeviceTypeRelationEnum.GPS);
gpsBsFirstBatchRequestList.add(gpsBsFirstBatchRequest);
});
R<List<GpsBsData>> firstGpsBsBatchR = gpsBsFeignService.getFirstGpsBsBatch(gpsBsFirstBatchRequestList);
List<GpsBsData> firstGpsBsDataList = firstGpsBsBatchR.getDataList(new TypeToken<List<GpsBsData>>() {
}.getType());
return firstGpsBsDataList;
}
}
......@@ -35,13 +35,13 @@ public class DeviceActiveService {
private AsyncTasks asyncTasks;
/**
* 保存设备激活信息
* 保存设备激活信息 feign 调用
*
* @param snList 设备号集合
*/
public void saveActiveInfo(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("snList is null");
log.info("saveActiveInfo snList is null");
return;
}
saveActiveInfoSync(snList);
......@@ -74,6 +74,47 @@ public class DeviceActiveService {
log.info("###save active info data end" + sw.getLastTaskTimeMillis() + "ms###");
}
/**
* 保存设备激活信息 by 定时任务调用
*
* @param snList 设备号集合
*/
public void saveActiveInfoByTask(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("saveActiveInfoByTask snList is null");
return;
}
saveActiveInfoSyncByTask(snList);
}
/**
* 多线程补充设备激活经纬度数据和激活地址 by 定时任务调用
*
* @param dataList 设备号集合大小
*/
public void saveActiveInfoSyncByTask(List<String> dataList) {
log.info("###save active info by task data start###");
if (CollectionUtils.isEmpty(dataList)) {
return;
}
StopWatch sw = new StopWatch();
sw.start("saveActiveInfoSyncByTask");
ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(dataList, 100);
CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size());
while (listSplitIterator.hasNext()) {
List<String> snList = listSplitIterator.next();
asyncTasks.saveActiveInfoByTask(snList, countDownLatch, remoteGpsBsService, vehicleDeviceMapper);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
sw.stop();
log.info("###save active info by task data end" + sw.getLastTaskTimeMillis() + "ms###");
}
/**
* 获取7天内没有激活数据的设备号
*
......@@ -94,6 +135,6 @@ public class DeviceActiveService {
public void saveActiveInfoFlushAll() {
List<String> allUnActiveInfoSnList = vehicleDeviceMapper.getAllUnActiveInfoSnList();
log.info("allUnActiveInfoSnList size===" + allUnActiveInfoSnList.size());
saveActiveInfo(allUnActiveInfoSnList);
saveActiveInfoByTask(allUnActiveInfoSnList);
}
}
......@@ -4,10 +4,13 @@ import cn.com.glsx.stdinterface.dto.VehicleDeviceDto;
import cn.com.glsx.stdinterface.modules.converter.VehicleDeviceConverter;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 车辆设备关系服务
*
......
......@@ -2,7 +2,7 @@
spring.application.name=ne-shield-standard-interfaces
#\u65E5\u5FD7\u8DEF\u5F84\uFF08logback-spring.xml\u8BFB\u53D6\uFF0C\u4E0D\u8981\u52A0\u5165\u914D\u7F6E\u4E2D\u5FC3\uFF09
logging.path=/data/java_log/jetty/${spring.application.name}/${spring.application.name}.log
logging.path=/Users/yanglinlong/data/${spring.application.name}/${spring.application.name}.log
#nacos\u914D\u7F6E\u4E2D\u5FC3
spring.cloud.nacos.config.server-addr=192.168.3.206:8848
......
......@@ -53,4 +53,15 @@
sn = #{sn}
and del_flag = 0
</select>
<select id="selectBySns" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_device
where del_flag = 0
and sn in
<foreach collection="sns" separator="," open="(" close=")" item="item">
#{item}
</foreach>
</select>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!