zhangyeguang
2 months ago
6 changed files with 131 additions and 29 deletions
@ -0,0 +1,116 @@ |
|||||
|
package com.jiagutech.ams.scheduled; |
||||
|
|
||||
|
import cn.hutool.core.collection.CollectionUtil; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
||||
|
import com.jiagutech.ams.mapper.DeviceMapper; |
||||
|
import com.jiagutech.ams.mapper.JobMapper; |
||||
|
import com.jiagutech.ams.model.TrackItem; |
||||
|
import com.jiagutech.ams.model.common.PageRequest; |
||||
|
import com.jiagutech.ams.model.common.PageResult; |
||||
|
import com.jiagutech.ams.model.dto.DeviceDTO; |
||||
|
import com.jiagutech.ams.model.dto.JobDTO; |
||||
|
import com.jiagutech.ams.model.request.JobPageRequest; |
||||
|
import com.jiagutech.ams.model.response.JobItem; |
||||
|
import com.jiagutech.ams.service.JobService; |
||||
|
import com.jiagutech.ams.utils.FlightTrackerUtils; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.time.*; |
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.Executors; |
||||
|
import java.util.concurrent.ScheduledExecutorService; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
public class JobScheduled { |
||||
|
private final JobService jobService; |
||||
|
private final JobMapper jobMapper; |
||||
|
private final DeviceMapper deviceMapper; |
||||
|
private final ScheduledExecutorService taskScheduler = Executors.newScheduledThreadPool(1); |
||||
|
|
||||
|
@Scheduled(cron = "0 20 0 * * ?") |
||||
|
public void cronTask() { |
||||
|
log.info("定时任务执行"); |
||||
|
// 获取当天零点
|
||||
|
LocalDate today = LocalDate.now(); |
||||
|
LocalDateTime todayStart = today.atStartOfDay(); |
||||
|
long jobQueryEndTime = todayStart.toEpochSecond(ZoneOffset.ofHours(8)) * 1000; |
||||
|
|
||||
|
// 获取前一天零点
|
||||
|
LocalDate yesterday = today.minusDays(1); |
||||
|
LocalDateTime yesterdayStart = yesterday.atStartOfDay(); |
||||
|
long jobQueryStartTime = yesterdayStart.toEpochSecond(ZoneOffset.ofHours(8)) * 1000; |
||||
|
QueryWrapper<JobDTO> queryWrapper = Wrappers.query(); |
||||
|
queryWrapper.between("start_time", jobQueryStartTime, jobQueryEndTime); |
||||
|
queryWrapper.eq("status", 2); |
||||
|
queryWrapper.eq("area", 0); |
||||
|
List<JobDTO> jobDTOS = jobMapper.selectList(queryWrapper); |
||||
|
for (JobDTO jobDTO : jobDTOS) { |
||||
|
Long startTime = jobDTO.getStartTime(); |
||||
|
Long endTime = System.currentTimeMillis(); |
||||
|
Instant instant1 = Instant.ofEpochMilli(startTime); |
||||
|
Instant instant2 = Instant.ofEpochMilli(endTime); |
||||
|
List<TrackItem> trackList = jobService.getTrackList(jobDTO.getId()); |
||||
|
if (CollectionUtil.isEmpty(trackList)) { |
||||
|
continue; |
||||
|
} |
||||
|
update(jobDTO, trackList); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
public void handleArea(JobDTO jobDTO, int retryCount) { |
||||
|
log.info("job:{}去获取轨迹计算作业面积,当前重试次数:{}", jobDTO.getId(), retryCount); |
||||
|
List<TrackItem> trackList = jobService.getTrackList(jobDTO.getId()); |
||||
|
if (CollectionUtil.isNotEmpty(trackList)) { |
||||
|
update(jobDTO, trackList); |
||||
|
} else { |
||||
|
if (retryCount < 3) { |
||||
|
int[] retryDelays = {60, 120, 300}; |
||||
|
int delay = retryDelays[retryCount]; |
||||
|
taskScheduler.schedule(() -> handleArea(jobDTO, retryCount + 1), delay, TimeUnit.SECONDS); |
||||
|
} else { |
||||
|
log.info("job:{}达到最大重试次数,面积计算未成功,终止任务", jobDTO.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private void update(JobDTO jobDTO, List<TrackItem> trackList) { |
||||
|
jobDTO.setArea(calculateArea(trackList)); |
||||
|
TrackItem trackItem = trackList.get(trackList.size() - 1); |
||||
|
jobDTO.setLat(trackItem.getLat()).setLng(trackItem.getLng()); |
||||
|
DeviceDTO deviceDTO = new DeviceDTO().setId(jobDTO.getDeviceId()).setLat(trackItem.getLat()).setLng(trackItem.getLng()).setUpdateTime(trackItem.getTimestamp()); |
||||
|
deviceMapper.updateById(deviceDTO); |
||||
|
jobMapper.updateById(jobDTO); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private float calculateArea(List<TrackItem> trackList) { |
||||
|
try { |
||||
|
if (CollectionUtil.isEmpty(trackList)) { |
||||
|
return 0.0f; |
||||
|
} |
||||
|
List<FlightTrackerUtils.PointTemp> pointTemps = trackList.stream().map(item -> { |
||||
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(item.getTimestamp()), ZoneId.systemDefault()); |
||||
|
int speed = Math.round(item.getVelocity() * 5 / 18); |
||||
|
return new FlightTrackerUtils.PointTemp(localDateTime, item.getLat(), item.getLng(), speed); |
||||
|
}).toList(); |
||||
|
if (CollectionUtil.isEmpty(pointTemps)) { |
||||
|
return 0.0f; |
||||
|
} |
||||
|
return (float) FlightTrackerUtils.calculateArea(pointTemps); |
||||
|
} catch (Exception e) { |
||||
|
log.error("calculate area error", e); |
||||
|
return 0.0f; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue