package com.ruoyi.quartz.task;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import com.ruoyi.system.domain.GpsLastPosition;
|
import com.ruoyi.system.domain.GpsLastPositionRequest;
|
import com.ruoyi.system.domain.GpsLastPositionResponse;
|
import com.ruoyi.system.domain.VehicleGps;
|
import com.ruoyi.system.domain.VehicleInfo;
|
import com.ruoyi.system.service.IGpsCollectService;
|
import com.ruoyi.system.service.IVehicleGpsService;
|
import com.ruoyi.system.service.IVehicleInfoService;
|
|
/**
|
* GPS同步定时任务
|
*/
|
@Component("gpsSyncTask")
|
public class GpsSyncTask {
|
|
private static final Logger log = LoggerFactory.getLogger(GpsSyncTask.class);
|
|
@Autowired
|
private IGpsCollectService gpsCollectService;
|
|
@Autowired
|
private IVehicleInfoService vehicleInfoService;
|
|
@Autowired
|
private IVehicleGpsService vehicleGpsService;
|
|
/**
|
* 同步GPS位置
|
*/
|
public void syncGpsData() {
|
try {
|
log.info("开始同步GPS数据...");
|
|
// 1. 获取所有车辆信息
|
List<VehicleInfo> vehicleList = vehicleInfoService.selectVehicleInfoList(new VehicleInfo());
|
|
// 2. 获取所有车辆的GPS最后位置
|
GpsLastPositionResponse gpsLastPositionResponse = gpsCollectService.getLastPosition(new GpsLastPositionRequest());
|
|
// 3. 遍历车辆列表,获取每个车辆的GPS位置
|
for (VehicleInfo vehicle : vehicleList) {
|
if (vehicle.getDeviceId() != null && !vehicle.getDeviceId().isEmpty()) {
|
try {
|
// 获取车辆的最后位置
|
gpsLastPositionResponse.getRecords().stream()
|
.filter(e -> e.getDeviceid().equals(vehicle.getDeviceId()))
|
.forEach(record -> {
|
updateVehicleGpsPositions(vehicle, record);
|
});
|
} catch (Exception e) {
|
log.error("获取车辆[{}]GPS位置失败: {}", vehicle.getVehicleNo(), e.getMessage());
|
}
|
}
|
}
|
|
log.info("GPS数据同步完成");
|
} catch (Exception e) {
|
log.error("GPS数据同步失败: {}", e.getMessage());
|
}
|
}
|
|
/**
|
* 更新车辆GPS位置信息
|
*/
|
private void updateVehicleGpsPositions(VehicleInfo vehicle, GpsLastPosition position) {
|
try {
|
VehicleGps gps = new VehicleGps();
|
gps.setVehicleId(vehicle.getVehicleId());
|
gps.setDeviceId(vehicle.getDeviceId());
|
gps.setLongitude(position.getCallon());
|
gps.setLatitude(position.getCallat());
|
gps.setAltitude(position.getAltitude());
|
gps.setSpeed(position.getSpeed());
|
gps.setDirection(Double.valueOf(position.getCourse()));
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
// 处理到达时间
|
long arrivedTime = position.getArrivedtime();
|
Date arrivedDate;
|
if (arrivedTime > 0) {
|
arrivedDate = new Date(arrivedTime);
|
arrivedDate.setTime(arrivedDate.getTime() - 8 * 60 * 60 * 1000);
|
} else {
|
arrivedDate = new Date();
|
}
|
gps.setPlatformProcessTime(sdf.format(arrivedDate));
|
|
// 设备上报时间
|
long deviceTime = position.getDevicetime();
|
Date date;
|
if (deviceTime > 0) {
|
date = new Date(deviceTime);
|
date.setTime(date.getTime() - 8 * 60 * 60 * 1000);
|
} else {
|
date = arrivedDate;
|
}
|
gps.setDeviceReportTime(sdf.format(date));
|
|
// 采集时间(使用设备上报时间)
|
gps.setCollectTime(sdf.format(new Date()));
|
|
// 保存GPS位置信息
|
vehicleGpsService.insertVehicleGps(gps);
|
|
log.info("车辆[{}]GPS位置已更新: 经度={}, 纬度={}, 设备时间={}, 处理时间={}",
|
vehicle.getVehicleNo(), position.getCallon(), position.getCallat(),
|
gps.getDeviceReportTime(), gps.getPlatformProcessTime());
|
} catch (Exception e) {
|
log.error("更新车辆[{}]GPS位置失败: {}", vehicle.getVehicleNo(), e.getMessage());
|
}
|
}
|
}
|