| | |
| | | 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; |
| | |
| | | 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位置 |
| | |
| | | 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()); |
| | | } |
| | |
| | | } 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | |
| | | log.error("更新车辆[{}]GPS位置失败: {}", vehicle.getVehicleNo(), e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |