156 lines
7.7 KiB
Java
156 lines
7.7 KiB
Java
package com.zhgd.xmgl.task;
|
|
|
|
import java.util.Date;
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.google.common.collect.Lists;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.zhgd.jeecg.common.execption.OpenAlertException;
|
|
import com.zhgd.xmgl.modules.project.service.IProjectService;
|
|
import com.zhgd.xmgl.modules.vehicleposition.entity.VehiclePositionConfig;
|
|
import com.zhgd.xmgl.modules.vehicleposition.entity.VehiclePositionData;
|
|
import com.zhgd.xmgl.modules.vehicleposition.entity.VehiclePositionDev;
|
|
import com.zhgd.xmgl.modules.vehicleposition.service.IVehiclePositionConfigService;
|
|
import com.zhgd.xmgl.modules.vehicleposition.service.IVehiclePositionDataService;
|
|
import com.zhgd.xmgl.modules.vehicleposition.service.IVehiclePositionDevService;
|
|
import com.zhgd.xmgl.util.HbyjUtil;
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.javacrumbs.shedlock.core.SchedulerLock;
|
|
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.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Slf4j
|
|
@RequestMapping("/xmgl/task")
|
|
@RestController
|
|
public class VehiclePositionTask {
|
|
@Lazy
|
|
@Autowired
|
|
private IVehiclePositionConfigService vehiclePositionConfigService;
|
|
@Lazy
|
|
@Autowired
|
|
private IVehiclePositionDevService vehiclePositionDevService;
|
|
@Lazy
|
|
@Autowired
|
|
private IVehiclePositionDataService vehiclePositionDataService;
|
|
|
|
/**
|
|
* 定时保存车辆(人员)定位数据(河北跃进)
|
|
*/
|
|
@Scheduled(cron = "* */10 * * * ?")
|
|
@SchedulerLock(name = "saveVehiclePositionDatas", lockAtMostFor = 1000 * 60, lockAtLeastFor = 1000 * 60)
|
|
@RequestMapping("saveVehiclePositionDatas")
|
|
public void saveVehiclePositionDatas() {
|
|
List<VehiclePositionConfig> configs = vehiclePositionConfigService.list(new LambdaQueryWrapper<VehiclePositionConfig>()
|
|
.eq(VehiclePositionConfig::getIsEnable, 1)
|
|
.eq(VehiclePositionConfig::getFactoryType, 2)
|
|
);
|
|
Date now = new Date();
|
|
String begin = DateUtil.formatDateTime(now);
|
|
String end = DateUtil.formatDateTime(DateUtil.offsetMinute(now, -10));
|
|
Map<String, List<VehiclePositionDev>> projectMap = vehiclePositionDevService.list(null).stream().collect(Collectors.groupingBy(VehiclePositionDev::getProjectSn));
|
|
for (VehiclePositionConfig config : configs) {
|
|
List<VehiclePositionDev> devList = projectMap.get(config.getProjectSn());
|
|
if (CollUtil.isEmpty(devList)) {
|
|
continue;
|
|
}
|
|
try {
|
|
HbyjUtil.HbyjAuth auth = HbyjUtil.getAuth(config.getIp(), config.getPort(), config.getAccount(), config.getPwd());
|
|
JSONObject jsonObject = HbyjUtil.getCarInfosByProjectId(config.getIp(), config.getPort(), auth.getProject_id());
|
|
if (!Objects.equals(jsonObject.getInteger("code"), 200)) {
|
|
throw new OpenAlertException("保存车辆(人员)定位数据车辆信息错误:" + jsonObject.getString("msg"));
|
|
}
|
|
List<CaridsToNumberPlate> caridToNumberPlates = getCaridsToNumberPlate(jsonObject, devList);
|
|
if (CollUtil.isEmpty(caridToNumberPlates)) {
|
|
continue;
|
|
}
|
|
vehiclePositionDevService.updateHeartbeatTimeByHbyj(caridToNumberPlates);
|
|
String carid = caridToNumberPlates.stream().map(CaridsToNumberPlate::getCarid).collect(Collectors.joining(","));
|
|
jsonObject = HbyjUtil.getPositionDatas(begin, end, carid, auth.getProject_id(), config.getIp(), config.getPort(),auth.getToken());
|
|
if (!Objects.equals(jsonObject.getInteger("code"), 200)) {
|
|
if (!Objects.equals(jsonObject.getInteger("code"), 500) || !Objects.equals(jsonObject.getString("msg"), "无数据")) {
|
|
throw new OpenAlertException("保存车辆(人员)定位数据车辆历史轨迹数据错误:" + jsonObject.getString("msg"));
|
|
}
|
|
}
|
|
List<VehiclePositionData> positionDatas = getVehiclePositionDatas(caridToNumberPlates, jsonObject);
|
|
if (CollUtil.isNotEmpty(positionDatas)) {
|
|
vehiclePositionDataService.saveBatch(positionDatas);
|
|
}
|
|
} catch (Exception e) {
|
|
log.info("", e);
|
|
log.error("定时保存车辆(人员)定位数据(河北跃进)错误:{}", e.getMessage());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private List<VehiclePositionData> getVehiclePositionDatas(List<CaridsToNumberPlate> caridToNumberPlates, JSONObject jsonObject) {
|
|
List<VehiclePositionData> vehiclePositionDatalist = Lists.newArrayList();
|
|
JSONArray body = jsonObject.getJSONArray("body");
|
|
for (int i = 0; i < body.size(); i++) {
|
|
JSONArray jsonArray = body.getJSONArray(i);
|
|
CaridsToNumberPlate plate = caridToNumberPlates.get(i);
|
|
for (int j = 0; j < jsonArray.size(); j++) {
|
|
JSONObject jo1 = jsonArray.getJSONObject(j);
|
|
VehiclePositionData data = new VehiclePositionData();
|
|
data.setDevSn(plate.getDevSn());
|
|
data.setLongitude(jo1.getDouble("longitude"));
|
|
data.setLatitude(jo1.getDouble("latitude"));
|
|
data.setProjectSn(data.getProjectSn());
|
|
data.setSpeed(jo1.getDouble("speed"));
|
|
data.setMileage(Convert.toLong(jo1.getString("totalmileage")));
|
|
data.setCreateTime(DateUtil.parseDateTime(jo1.getString("gps_time")));
|
|
data.setProjectSn(plate.getProjectSn());
|
|
if (!Objects.equals(data.getLongitude(), 0D) || !Objects.equals(data.getLatitude(), 0D)) {
|
|
vehiclePositionDatalist.add(data);
|
|
}
|
|
}
|
|
}
|
|
return vehiclePositionDatalist;
|
|
}
|
|
|
|
private List<CaridsToNumberPlate> getCaridsToNumberPlate(JSONObject jsonObject, List<VehiclePositionDev> devList) {
|
|
List<CaridsToNumberPlate> caridsToNumberPlatelist = Lists.newArrayList();
|
|
JSONArray datas = jsonObject.getJSONArray("data");
|
|
if (CollUtil.isEmpty(datas)) {
|
|
return caridsToNumberPlatelist;
|
|
}
|
|
Map<String, JSONObject> numberPlateMap = datas.stream().collect(Collectors.toMap(o -> ((JSONObject) o).getString("car_number_plate"), o -> ((JSONObject) o), (o1, o2) -> o1));
|
|
for (VehiclePositionDev dev : devList) {
|
|
JSONObject jsonObject1 = numberPlateMap.get(dev.getNumberPlate());
|
|
if (jsonObject1 != null) {
|
|
CaridsToNumberPlate plate = new CaridsToNumberPlate();
|
|
plate.setId(dev.getId());
|
|
plate.setCarid(jsonObject1.getString("car_id"));
|
|
plate.setIsOnline(jsonObject1.getInteger("is_online"));
|
|
plate.setNumberPlate(jsonObject1.getString("car_number_plate"));
|
|
plate.setDevSn(dev.getDevSn());
|
|
plate.setProjectSn(dev.getProjectSn());
|
|
caridsToNumberPlatelist.add(plate);
|
|
}
|
|
}
|
|
return caridsToNumberPlatelist;
|
|
}
|
|
|
|
@Data
|
|
public static class CaridsToNumberPlate {
|
|
private Long id;
|
|
private String carid;
|
|
private String numberPlate;
|
|
private String devSn;
|
|
private Integer isOnline;
|
|
private java.lang.String projectSn;
|
|
}
|
|
}
|