wlzboy
2025-09-20 b62bc392f3c1658381107be1d5f737a3389e7f5f
ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/GpsSyncTask.java
@@ -4,23 +4,19 @@
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.gps.service.IGpsCollectService;
import com.ruoyi.gps.service.IVehicleGpsService;
import com.ruoyi.gps.service.IVehicleInfoService;
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;
import com.ruoyi.gps.domain.GpsLastPosition;
import com.ruoyi.gps.domain.GpsLastPositionRequest;
import com.ruoyi.gps.domain.GpsLastPositionResponse;
import com.ruoyi.gps.domain.VehicleGps;
import com.ruoyi.gps.domain.VehicleInfo;
/**
 * GPS同步定时任务
@@ -40,21 +36,16 @@
    private IVehicleGpsService vehicleGpsService;
    /**
     * 同步设备列表和GPS位置
     * 同步GPS位置
     */
    public void syncGpsData() {
        try {
            log.info("开始同步GPS数据...");
            // 1. 获取设备列表,这会自动更新车辆信息中的设备ID
            GpsDeviceListResponse response = gpsCollectService.getDeviceList();
            // 更新车辆设备ID
            updateVehicleDeviceIds(response);
            // 2. 获取所有车辆信息
            // 1. 获取所有车辆信息
            List<VehicleInfo> vehicleList = vehicleInfoService.selectVehicleInfoList(new VehicleInfo());
            //在这里获得所有车辆的GPS最后位置
            // 2. 获取所有车辆的GPS最后位置
            GpsLastPositionResponse gpsLastPositionResponse = gpsCollectService.getLastPosition(new GpsLastPositionRequest());
            // 3. 遍历车辆列表,获取每个车辆的GPS位置
@@ -62,11 +53,11 @@
                if (vehicle.getDeviceId() != null && !vehicle.getDeviceId().isEmpty()) {
                    try {
                        // 获取车辆的最后位置
                        gpsLastPositionResponse.getRecords().stream().filter(e->e.getDeviceid().equals(vehicle.getDeviceId())).forEach(record -> {
                        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());
                    }
@@ -77,53 +68,6 @@
        } 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);
                            vehicleInfoService.updateVehicleInfo(vehicleInfo);
                        } else {
                            VehicleInfo newVehicle = new VehicleInfo();
                            newVehicle.setVehicleNo(plateNumber);
                            newVehicle.setDeviceId(deviceId);
                            newVehicle.setStatus("0");
                            vehicleInfoService.insertVehicleInfo(newVehicle);
                        }
                    }
                }
            }
        }
    }
    /**
     * 从设备名称和备注中提取车牌号
     */
    private String extractPlateNumber(String deviceName, String remark) {
        if (StringUtils.isNotEmpty(deviceName)) {
            return deviceName;
        }
        if (StringUtils.isNotEmpty(remark)) {
            return remark;
        }
        return null;
    }
    /**
@@ -140,23 +84,32 @@
            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");
            // 处理到达时间
            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();
            if (deviceTime > 0 && deviceTime < 4102444800L) { // 2100-01-01 00:00:00
                gps.setDeviceReportTime(sdf.format(new Date(deviceTime * 1000L)));
            Date date;
            if (deviceTime > 0) {
                date = new Date(deviceTime);
                date.setTime(date.getTime() - 8 * 60 * 60 * 1000);
            } else {
                log.warn("车辆[{}]的设备时间戳[{}]无效,使用当前时间", vehicle.getVehicleNo(), deviceTime);
                gps.setDeviceReportTime(sdf.format(new Date()));
                date = arrivedDate;
            }
            // 平台处理时间(当前时间)
            gps.setPlatformProcessTime(sdf.format(new Date()));
            gps.setDeviceReportTime(sdf.format(date));
            
            // 采集时间(使用设备上报时间)
            gps.setCollectTime(gps.getDeviceReportTime());
            gps.setCollectTime(sdf.format(new Date()));
            // 保存GPS位置信息
            vehicleGpsService.insertVehicleGps(gps);
@@ -168,5 +121,4 @@
            log.error("更新车辆[{}]GPS位置失败: {}", vehicle.getVehicleNo(), e.getMessage());
        }
    }
}