247 lines
12 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.task;
import java.util.Date;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhgd.redis.lock.RedisRepository;
import com.zhgd.xmgl.base.HikvisionEventsPictureRq;
import com.zhgd.xmgl.call.HikvisionCall;
import com.zhgd.xmgl.modules.baotou.entity.CarDangerDetect;
import com.zhgd.xmgl.modules.baotou.entity.CarDangerDetectRecord;
import com.zhgd.xmgl.modules.baotou.service.ICarDangerDetectRecordService;
import com.zhgd.xmgl.modules.baotou.service.ICarDangerDetectService;
import com.zhgd.xmgl.modules.car.entity.CarInfo;
import com.zhgd.xmgl.modules.car.entity.CarInfoApprovalFlow;
import com.zhgd.xmgl.modules.car.entity.CarMeasureSpeedData;
import com.zhgd.xmgl.modules.car.entity.CarMeasureSpeedDev;
import com.zhgd.xmgl.modules.car.enums.CarInfoCarModuleTypeEnum;
import com.zhgd.xmgl.modules.car.mapper.CarInfoApprovalFlowMapper;
import com.zhgd.xmgl.modules.car.mapper.CarInfoMapper;
import com.zhgd.xmgl.modules.car.service.ICarMeasureSpeedDataService;
import com.zhgd.xmgl.modules.car.service.ICarMeasureSpeedDevService;
import com.zhgd.xmgl.modules.car.service.impl.CarInfoServiceImpl;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
import com.zhgd.xmgl.util.HikvisionUtil;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@RequestMapping("/xmgl/task")
@RestController
public class CarInfoTask {
@Lazy
@Autowired
HikvisionCall hikvisionCall;
@Lazy
@Autowired
private CarInfoServiceImpl carInfoService;
@Lazy
@Autowired
private CarInfoMapper carInfoMapper;
@Lazy
@Autowired
private ProjectMapper projectMapper;
@Lazy
@Autowired
private CarInfoApprovalFlowMapper carInfoApprovalFlowMapper;
@Lazy
@Autowired
private RedisRepository redisRepository;
@Lazy
@Autowired
private ICarMeasureSpeedDevService carMeasureSpeedDevService;
@Autowired
private ICarMeasureSpeedDataService carMeasureSpeedDataService;
@Lazy
@Autowired
private ICarDangerDetectRecordService carDangerDetectRecordService;
@Lazy
@Autowired
private ICarDangerDetectService carDangerDetectService;
/**
* 定时删除/更新过期的车辆,并下发新的预约时间的车辆到海康
*/
@Scheduled(cron = "*/30 * * * * ?")
public void deleteOrUpdateExpiredCar() {
//删除过期
carInfoApprovalFlowMapper.delete(new LambdaQueryWrapper<CarInfoApprovalFlow>()
.lt(CarInfoApprovalFlow::getReserveEndTime, DateUtil.now())
.eq(CarInfoApprovalFlow::getIsBlack, 0)
);
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>().eq(Project::getSyncHikvision, 1));
for (Project project : projects) {
//过期或者未来车
List<CarInfo> cars = carInfoMapper.selectList(new LambdaQueryWrapper<CarInfo>()
.ne(CarInfo::getCarModuleType, CarInfoCarModuleTypeEnum.GD.getValue())
.eq(CarInfo::getIsBlack, 0)
.eq(CarInfo::getProjectSn, project.getProjectSn())
.and(w -> w.lt(CarInfo::getReserveEndTime, DateUtil.now()).or().gt(CarInfo::getReserveStartTime, DateUtil.now()))
);
if (CollUtil.isEmpty(cars)) {
return;
}
for (CarInfo carInfo : cars) {
carInfoService.deleteOrUpdateCar(project, carInfo, false);
}
}
}
/**
* 定时到海康拉取车辆超速园区卡口违章事件查询v2
*/
@Scheduled(cron = "* */2 * * * ?")
@SchedulerLock(name = "getCarMeasureSpeedData", lockAtMostFor = 1000 * 60, lockAtLeastFor = 1000 * 60)
@RequestMapping("getCarMeasureSpeedData")
public void getCarMeasureSpeedData() {
String start;
String queryTimeKey = "GET_ISC_CAR_MEASURE_SPEED_DATA_TIME";
Object o = redisRepository.get(queryTimeKey);
if (o != null) {
start = o.toString();
} else {
start = DateUtil.format(DateUtil.offsetDay(new Date(), -1), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
}
String end = DateUtil.format(DateUtil.offsetMinute(new Date(), -2), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
redisRepository.set(queryTimeKey, end);
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
.eq(Project::getSyncHikvision, 1));
Map<String, List<CarMeasureSpeedDev>> projectMap = carMeasureSpeedDevService.list().stream().collect(Collectors.groupingBy(CarMeasureSpeedDev::getProjectSn));
Map<String, List<CarDangerDetect>> dangerDetectProjectMap = carDangerDetectService.list().stream().collect(Collectors.groupingBy(CarDangerDetect::getProjectSn));
for (Project project : projects) {
try {
JSONArray listJa = getSpeedDataList(start, project);
if (CollUtil.isEmpty(listJa)) {
continue;
}
Map<String, CarMeasureSpeedDev> devMap = Optional.ofNullable(projectMap.get(project.getProjectSn())).map(m -> m.stream().collect(Collectors.toMap(CarMeasureSpeedDev::getDevSn, Function.identity()))).orElse(null);
List<CarMeasureSpeedData> speedDatas = buildSpeedData(listJa, devMap, project);
if (CollUtil.isNotEmpty(speedDatas)) {
carMeasureSpeedDataService.saveBatch(speedDatas);
}
Map<String, CarDangerDetect> carDangerDevMap = Optional.ofNullable(dangerDetectProjectMap.get(project.getProjectSn())).map(m -> m.stream().collect(Collectors.toMap(CarDangerDetect::getHardwareId, Function.identity()))).orElse(null);
List<CarDangerDetectRecord> carDangers = buildCarDangerData(listJa, carDangerDevMap, project);
if (CollUtil.isNotEmpty(carDangers)) {
carDangerDetectRecordService.saveBatch(carDangers);
}
} catch (Exception e) {
log.error("定时到海康拉取车辆超速错误:", e);
}
}
}
private List<CarDangerDetectRecord> buildCarDangerData(JSONArray listJa, Map<String, CarDangerDetect> carDangerDevMap, Project project) throws Exception {
List<CarDangerDetectRecord> datas = new ArrayList<>();
if (CollUtil.isNotEmpty(listJa)) {
for (int i = 0; i < listJa.size(); i++) {
JSONObject jo = listJa.getJSONObject(i);
Integer illegalType = jo.getInteger("illegalType");
if (!Objects.equals(illegalType, 1) && !Objects.equals(illegalType, 7)) {
continue;
}
String monitoringId = jo.getString("monitoringId");
CarDangerDetect dev = carDangerDevMap.get(monitoringId);
if (dev == null) {
continue;
}
String plateNo = jo.getString("plateNo");
BigDecimal speed = jo.getBigDecimal("speed");
Date time = DateUtil.parse(jo.getString("crossTime"));
CarDangerDetectRecord data = new CarDangerDetectRecord();
data.setProjectSn(dev.getProjectSn());
data.setHardwareName(dev.getHardwareName());
data.setHardwareId(dev.getHardwareId());
String platePicUrl = jo.getString("carPicUri");
String svrIndex = jo.getString("aswSyscode");
if (StringUtils.isNotBlank(platePicUrl)) {
data.setImageUrl(hikvisionCall.saveToLocal(HikvisionCall.getHikvisionEventsPicture(new HikvisionEventsPictureRq(svrIndex, platePicUrl), project.getArtemisConfigHost(), project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret())));
}
data.setAlarmDesc(dev.getHardwareName());
data.setAlarmType(1);
data.setLocation(dev.getHardwareName());
data.setCreateTime(DateUtil.formatDateTime(time));
data.setIsPushed(false);
data.setCarNumber(plateNo);
data.setCurrentSpeed(speed);
datas.add(data);
}
}
return datas;
}
private List<CarMeasureSpeedData> buildSpeedData(JSONArray listJa, Map<String, CarMeasureSpeedDev> devMap, Project project) throws Exception {
List<CarMeasureSpeedData> datas = new ArrayList<>();
if (CollUtil.isNotEmpty(listJa)) {
for (int i = 0; i < listJa.size(); i++) {
JSONObject jo = listJa.getJSONObject(i);
Integer illegalType = jo.getInteger("illegalType");
if (!Objects.equals(illegalType, 1) && !Objects.equals(illegalType, 7)) {
continue;
}
String monitoringId = jo.getString("monitoringId");
CarMeasureSpeedDev dev = devMap.get(monitoringId);
if (dev == null) {
continue;
}
String plateNo = jo.getString("plateNo");
BigDecimal speed = jo.getBigDecimal("speed");
Date time = DateUtil.parse(jo.getString("crossTime"));
CarMeasureSpeedData data = new CarMeasureSpeedData();
data.setDevSn(monitoringId);
data.setCarNumber(plateNo);
data.setIsExceed(NumberUtil.isGreater(speed, dev.getThreshold()) ? 1 : 0);
data.setCurrentSpeed(speed);
data.setExceedingThreshold(Objects.equals(data.getIsExceed(), 1) ? NumberUtil.sub(speed, dev.getThreshold()) : new BigDecimal("0"));
String platePicUrl = jo.getString("carPicUri");
String svrIndex = jo.getString("aswSyscode");
if (StringUtils.isNotBlank(platePicUrl)) {
data.setSnapshotImage(hikvisionCall.saveToLocal(HikvisionCall.getHikvisionEventsPicture(new HikvisionEventsPictureRq(svrIndex, platePicUrl), project.getArtemisConfigHost(), project.getArtemisConfigAppKey(), project.getArtemisConfigAppSecret())));
}
data.setUploadTime(time);
data.setProjectSn(dev.getProjectSn());
datas.add(data);
}
}
return datas;
}
private JSONArray getSpeedDataList(String start, Project project) {
try {
JSONObject param = new JSONObject();
HikvisionUtil.addPageParamIfAbsent(param);
param.put("beginTime", start);
param.put("endTime", DateUtil.format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
JSONObject rsJo = HikvisionUtil.getSpeedDataList(project, param);
if (HikvisionUtil.isSuccess(rsJo)) {
JSONObject dataJo = HikvisionUtil.getJsonObjectData(rsJo);
return dataJo.getJSONArray("list");
}
} catch (Exception e) {
log.error("定时http到海康拉取车辆超速错误:{}", e.getMessage());
log.error("", e);
}
return null;
}
}