Commit e10f4036 by 杨林龙

优化设备激活信息保存逻辑

1 parent a8261f7b
...@@ -44,6 +44,7 @@ public class DeviceActiveController implements DeviceActiveClient { ...@@ -44,6 +44,7 @@ public class DeviceActiveController implements DeviceActiveClient {
* @param snList 设备号集合 * @param snList 设备号集合
* @return * @return
*/ */
@Override
@PostMapping("/saveActiveInfoFlush") @PostMapping("/saveActiveInfoFlush")
@ApiOperation(value = "手动更新设备激活定位点") @ApiOperation(value = "手动更新设备激活定位点")
@SysLog(module = "设备激活", value = "手动更新设备激活定位点", action = OperateType.QUERY, saveLog = false) @SysLog(module = "设备激活", value = "手动更新设备激活定位点", action = OperateType.QUERY, saveLog = false)
......
...@@ -7,6 +7,7 @@ import cn.com.glsx.stdinterface.modules.entity.VehicleDevice; ...@@ -7,6 +7,7 @@ import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper; import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService; import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.utils.DateUtils; import com.glsx.plat.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -44,6 +45,7 @@ public class AsyncTasks { ...@@ -44,6 +45,7 @@ public class AsyncTasks {
return; return;
} }
lastGpsBsList.forEach(gpsBsData -> { lastGpsBsList.forEach(gpsBsData -> {
log.info("gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
VehicleDevice vehicleDevice = new VehicleDevice(); VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn()); vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation(); GpsLocation gpsLocation = gpsBsData.getGpsLocation();
...@@ -105,7 +107,7 @@ public class AsyncTasks { ...@@ -105,7 +107,7 @@ public class AsyncTasks {
vehicleDevice.setSn(gpsBsData.getSn()); vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation(); GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据 // 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) { if (ObjectUtil.isNotNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN)); vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS); vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng()); vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
...@@ -116,7 +118,7 @@ public class AsyncTasks { ...@@ -116,7 +118,7 @@ public class AsyncTasks {
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice); vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else { } else {
GpsLocation bsLocation = gpsBsData.getBsLocation(); GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) { if (ObjectUtil.isNotNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN)); vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS); vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng()); vehicleDevice.setActiveLocationLng(bsLocation.getLng());
......
package cn.com.glsx.stdinterface.modules.service; package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.devicemonitor.api.dto.response.GpsBsData;
import cn.com.glsx.devicemonitor.api.dto.response.GpsLocation;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper; import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.multithread.AsyncTasks;
import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService; import cn.com.glsx.stdinterface.modules.remote.RemoteGpsBsService;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.glsx.plat.common.multithread.ListSplitIterator; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -14,7 +21,7 @@ import org.springframework.util.StopWatch; ...@@ -14,7 +21,7 @@ import org.springframework.util.StopWatch;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.Objects;
/** /**
* 设备激活 service * 设备激活 service
...@@ -31,9 +38,6 @@ public class DeviceActiveService { ...@@ -31,9 +38,6 @@ public class DeviceActiveService {
@Autowired @Autowired
private VehicleDeviceMapper vehicleDeviceMapper; private VehicleDeviceMapper vehicleDeviceMapper;
@Autowired
private AsyncTasks asyncTasks;
/** /**
* 保存设备激活信息 feign 调用 * 保存设备激活信息 feign 调用
* *
...@@ -44,36 +48,8 @@ public class DeviceActiveService { ...@@ -44,36 +48,8 @@ public class DeviceActiveService {
log.info("saveActiveInfo snList is null"); log.info("saveActiveInfo snList is null");
return; return;
} }
saveActiveInfoSync(snList); saveActiveInfoByFeign(snList);
}
/**
* 多线程补充设备激活经纬度数据和激活地址
*
* @param dataList 设备号集合大小
*/
public void saveActiveInfoSync(List<String> dataList) {
log.info("###save active info data start###");
if (CollectionUtils.isEmpty(dataList)) {
return;
}
StopWatch sw = new StopWatch();
sw.start("saveActiveInfoSync");
ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(dataList, 100);
CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size());
while (listSplitIterator.hasNext()) {
List<String> snList = listSplitIterator.next();
asyncTasks.saveActiveInfo(snList, countDownLatch, remoteGpsBsService, vehicleDeviceMapper);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
} }
sw.stop();
log.info("###save active info data end" + sw.getLastTaskTimeMillis() + "ms###");
}
/** /**
* 保存设备激活信息 by 定时任务调用 * 保存设备激活信息 by 定时任务调用
...@@ -100,16 +76,8 @@ public class DeviceActiveService { ...@@ -100,16 +76,8 @@ public class DeviceActiveService {
} }
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start("saveActiveInfoSyncByTask"); sw.start("saveActiveInfoSyncByTask");
ListSplitIterator<String> listSplitIterator = new ListSplitIterator<>(dataList, 50); for (String sn : dataList) {
CountDownLatch countDownLatch = new CountDownLatch(listSplitIterator.size()); saveActiveInfoByTask(sn);
while (listSplitIterator.hasNext()) {
List<String> snList = listSplitIterator.next();
asyncTasks.saveActiveInfoByTask(snList, countDownLatch, remoteGpsBsService, vehicleDeviceMapper);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
} }
sw.stop(); sw.stop();
log.info("###save active info by task data end" + sw.getLastTaskTimeMillis() + "ms###"); log.info("###save active info by task data end" + sw.getLastTaskTimeMillis() + "ms###");
...@@ -137,4 +105,105 @@ public class DeviceActiveService { ...@@ -137,4 +105,105 @@ public class DeviceActiveService {
log.info("allUnActiveInfoSnList size===" + allUnActiveInfoSnList.size()); log.info("allUnActiveInfoSnList size===" + allUnActiveInfoSnList.size());
saveActiveInfoByTask(allUnActiveInfoSnList); saveActiveInfoByTask(allUnActiveInfoSnList);
} }
public void saveActiveInfoByFeign(List<String> snList) {
if (CollectionUtil.isEmpty(snList)) {
log.info("snList is null");
return;
}
try {
log.info("saveActiveInfoByFeign 开始同步激活信息,snList===" + JSONUtil.toJsonStr(snList));
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getLastGpsBsListBySns(snList);
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfoByFeign lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
log.info("Feign gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (Objects.nonNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (Objects.nonNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfoByFeign异常信息===" + e.getMessage(), e);
} finally {
log.info(Thread.currentThread().getName() + "关闭资源");
}
}
/**
* 通过sn同步设备激活信息
*
* @param sn 设备号
*/
public void saveActiveInfoByTask(String sn) {
if (StringUtils.isEmpty(sn)) {
log.info("sn is null");
return;
}
try {
log.info("saveActiveInfoByTask 开始同步激活信息,sn===" + sn);
List<GpsBsData> lastGpsBsList = remoteGpsBsService.getFirstGpsBsBatch(Lists.newArrayList(sn));
if (CollectionUtil.isEmpty(lastGpsBsList)) {
log.info("saveActiveInfoByTask lastGpsBsList is null");
return;
}
lastGpsBsList.forEach(gpsBsData -> {
log.info("Task gpsBsData===" + JSONUtil.toJsonStr(gpsBsData));
VehicleDevice vehicleDevice = new VehicleDevice();
vehicleDevice.setSn(gpsBsData.getSn());
GpsLocation gpsLocation = gpsBsData.getGpsLocation();
// 默认保存GPS定位数据 没有GPS定位数据 保存BS定位数据
if (ObjectUtil.isNotNull(gpsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(gpsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.GPS);
vehicleDevice.setActiveLocationLng(gpsLocation.getLng());
vehicleDevice.setActiveLocationLat(gpsLocation.getLat());
vehicleDevice.setActiveLocationAddress(gpsLocation.getAddress());
// 保存激活信息
log.info("active device info by task gps:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
} else {
GpsLocation bsLocation = gpsBsData.getBsLocation();
if (ObjectUtil.isNotNull(bsLocation)) {
vehicleDevice.setActiveLocationDate(DateUtils.stringToDate(bsLocation.getDateTime(), DateUtils.NORMAL_DATE_TIME_PATTERN));
vehicleDevice.setActiveLocationType(Constants.LocationType.BS);
vehicleDevice.setActiveLocationLng(bsLocation.getLng());
vehicleDevice.setActiveLocationLat(bsLocation.getLat());
vehicleDevice.setActiveLocationAddress(bsLocation.getAddress());
// 保存激活信息
log.info("active device info by task bs:" + JSONUtil.toJsonStr(vehicleDevice));
vehicleDeviceMapper.updateVehicleDeviceBysn(vehicleDevice);
}
}
});
} catch (Exception e) {
log.info("saveActiveInfoByTask异常信息===" + e.getMessage(), e);
} finally {
log.info(Thread.currentThread().getName() + "关闭资源");
}
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!