package com.ruoyi.quartz.task; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.commons.lang3.StringUtils; 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.GpsDevice; import com.ruoyi.system.domain.GpsDeviceListResponse; import com.ruoyi.system.domain.GpsGroup; 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. 获取设备列表,这会自动更新车辆信息中的设备ID GpsDeviceListResponse response = gpsCollectService.getDeviceList(); // 更新车辆设备ID updateVehicleDeviceIds(response); // 2. 获取所有车辆信息 List vehicleList = vehicleInfoService.selectVehicleInfoList(new VehicleInfo()); //在这里获得所有车辆的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()); } } /** * 更新车辆设备ID */ private void updateVehicleDeviceIds(GpsDeviceListResponse response) { if (response.getStatus() != 0 || response.getGroups() == null) { return; } for (GpsGroup group : response.getGroups()) { for (GpsDevice device : group.getDevices()) { String deviceName = device.getDevicename(); String remark = device.getRemark(); String deviceId = device.getDeviceid(); if (StringUtils.isNotEmpty(deviceName) || StringUtils.isNotEmpty(remark)) { String plateNumber = extractPlateNumber(deviceName, remark); if (StringUtils.isNotEmpty(plateNumber)) { VehicleInfo vehicleInfo = vehicleInfoService.selectVehicleInfoByPlateNumber(plateNumber); if (vehicleInfo != null) { vehicleInfo.setDeviceId(deviceId); //获得数据字典中的平台编码 vehicleInfo.setPlatformCode("GPS51"); vehicleInfoService.updateVehicleInfo(vehicleInfo); } else { VehicleInfo newVehicle = new VehicleInfo(); newVehicle.setVehicleNo(plateNumber); newVehicle.setDeviceId(deviceId); newVehicle.setStatus("0"); newVehicle.setPlatformCode("GPS51"); vehicleInfoService.insertVehicleInfo(newVehicle); } } } } } } /** * 从设备名称和备注中提取车牌号 */ private String extractPlateNumber(String deviceName, String remark) { if (StringUtils.isNotEmpty(deviceName)) { return deviceName; } if (StringUtils.isNotEmpty(remark)) { return remark; } return null; } /** * 更新车辆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())); //devicetime 这个是一个linux时间戳,要转换成北京时间,再转成yyyy-MM-dd HH:mm:ss格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //getArrivedtime 这个是一个linux时间戳,要转换成北京时间,再转成yyyy-MM-dd HH:mm:ss格式 long arrivedTime = position.getArrivedtime(); Date arrivedDate; // 检查时间戳是否有效(大于0) if (arrivedTime > 0) { arrivedDate = new Date(arrivedTime); // 减去8小时 arrivedDate.setTime(arrivedDate.getTime() - 8 * 60 * 60 * 1000); } else { // 时间戳无效,使用当前时间 arrivedDate = new Date(); } gps.setPlatformProcessTime(sdf.format(arrivedDate)); // 设备上报时间 long deviceTime = position.getDevicetime(); Date date; // 检查时间戳是否有效(大于0) if (deviceTime > 0) { date = new Date(deviceTime); // 减去8小时 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()); } } }