| | |
| | | |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.xiaopeng.api.dto.automaticPilot.MqttPublishDto; |
| | | import com.xiaopeng.api.dto.automaticPilot.PushOrderDto; |
| | | import com.xiaopeng.api.dto.map.OrderRouteAndFareDto; |
| | | import com.xiaopeng.api.dto.operation.LonLatDto; |
| | | import com.xiaopeng.api.dto.order.PopOrderDto; |
| | | import com.xiaopeng.api.dto.pushMessage.PushOrderRouteDTO; |
| | | import com.xiaopeng.api.dto.pushMessage.SendMessageDTO; |
| | | import com.xiaopeng.api.dto.vehicleConvoy.UpdateVehicleStateDto; |
| | | import com.xiaopeng.api.feignClient.map.XpMapClient; |
| | | import com.xiaopeng.api.feignClient.order.XpOrderClient; |
| | | import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient; |
| | | import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient; |
| | | import com.xiaopeng.api.model.order.XpOrder;import com.xiaopeng.api.model.vehicleConvoy.XpVehicle; |
| | | import com.xiaopeng.api.vo.automaticPilot.VehiclePosition; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudCreateOrder.RTTask; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudCreateOrder.TaskStatus; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudCreateOrder.TaskType; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudOrderOperation.OrderOperation; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudOrderOperation.OrderOperationType; |
| | | import com.xiaopeng.api.vo.automaticPilot.protobuf.*; |
| | | import com.xiaopeng.api.vo.operation.RouteVo; |
| | | import com.xiaopeng.api.vo.order.OrderTrajectoryVo; |
| | | import com.xiaopeng.automaticPilot.util.XPUUtil; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.enums.OrderStatusEnum; |
| | | import com.xiaopeng.common.core.enums.PushMessageTypeEnum; |
| | | import com.xiaopeng.common.core.utils.SnowFlake; |
| | | import com.xiaopeng.common.core.utils.StringUtils; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import io.swagger.annotations.Api; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.geo.GeoJsonPoint; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.data.mongodb.core.query.Update; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | |
| | | @Api(tags = "订单任务Api") |
| | | public class OrderMissionController { |
| | | |
| | | private final XPUUtil xpuUtil; |
| | | private final RedisService redisService; |
| | | private final XpVehicleClient xpVehicleClient; |
| | | private final Snowflake snowflake; |
| | | |
| | | public OrderMissionController(XPUUtil xpuUtil, RedisService redisService, XpVehicleClient xpVehicleClient, Snowflake snowflake) { |
| | | this.xpuUtil = xpuUtil; |
| | | this.redisService = redisService; |
| | | this.xpVehicleClient = xpVehicleClient; |
| | | this.snowflake = snowflake; |
| | | } |
| | | @Resource |
| | | private XPUUtil xpuUtil; |
| | | @Resource |
| | | private RedisService redisService; |
| | | @Resource |
| | | private XpVehicleClient xpVehicleClient; |
| | | |
| | | @PostMapping("/inner/pushOrder") |
| | | public R<Boolean> pushOrder(@RequestBody PushOrderDto dto) { |
| | | RTTask task = new RTTask(); |
| | | task.setOrder_id(dto.getOrderId().toString()); |
| | | task.setTask_id("MISSION_" + IdUtil.fastUUID()); |
| | | task.setTask_type(TaskType.ROBOTAXI); |
| | | task.setStatus(TaskStatus.PENDING); |
| | | long sequence = snowflake.nextId(); |
| | | MqttPublishDto mqttPublishDto = xpuUtil.cloudCreateOrder(task, dto.getVin(), sequence); |
| | | if (mqttPublishDto.getCode() == 200) { |
| | | xpVehicleClient.updateVehicleStatus(new UpdateVehicleStateDto(dto.getVin(), 1)); |
| | | RtTask.Builder taskBuilder = RtTask.newBuilder(); |
| | | //TODO 设置目标地点信息 |
| | | taskBuilder.setOrderId(dto.getOrderId().toString()); |
| | | taskBuilder.setTaskId("MISSION_"+ IdUtil.fastUUID()); |
| | | |
| | | if(dto.getOrderId().contains("XU")){ |
| | | taskBuilder.setTaskType(OrderType.ORDER_TYPE_CRUISE); |
| | | }else { |
| | | taskBuilder.setTaskType(OrderType.ORDER_TYPE_ROBOTAXI); |
| | | } |
| | | //构建点位信息 |
| | | Waypoint.Builder pointBuilder = Waypoint.newBuilder(); |
| | | //巡游订单 |
| | | if(dto.getOrderType().equals(com.xiaopeng.api.vo.OrderType.CRUISE_ORDER)){ |
| | | pointBuilder.setPointType(PointType.CRUISE_END_POINT); |
| | | pointBuilder.setSequence(1); |
| | | //构建终点坐标 |
| | | GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder(); |
| | | geoPointBuilder.setLatitude(dto.getEndLatitude()); |
| | | geoPointBuilder.setLongitude(dto.getEndLatitude()); |
| | | // geoPointBuilder.setPoiName("Start Point"); // GeoPoint might not have poi_name |
| | | pointBuilder.setPoint(geoPointBuilder.build()); |
| | | }else{ |
| | | //用户订单 |
| | | if(dto.getStage() == 1){ |
| | | //开往上车点 |
| | | pointBuilder.setPointType(PointType.START_POINT); |
| | | pointBuilder.setActionType(ActionType.PICK_UP); |
| | | pointBuilder.setSequence(1); |
| | | //构建起点坐标 |
| | | GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder(); |
| | | geoPointBuilder.setLatitude(dto.getStartLatitude()); |
| | | geoPointBuilder.setLongitude(dto.getStartLongitude()); |
| | | // geoPointBuilder.setPoiName("Start Point"); // GeoPoint might not have poi_name |
| | | pointBuilder.setPoint(geoPointBuilder.build()); |
| | | }else{ |
| | | //开往终点 |
| | | pointBuilder.setPointType(PointType.END_POINT); |
| | | pointBuilder.setActionType(ActionType.PICK_UP); |
| | | pointBuilder.setSequence(1); |
| | | //构建起点坐标 |
| | | GeoPoint.Builder geoPointBuilder = GeoPoint.newBuilder(); |
| | | geoPointBuilder.setLatitude(dto.getEndLatitude()); |
| | | geoPointBuilder.setLongitude(dto.getEndLatitude()); |
| | | // geoPointBuilder.setPoiName("Start Point"); // GeoPoint might not have poi_name |
| | | pointBuilder.setPoint(geoPointBuilder.build()); |
| | | } |
| | | } |
| | | taskBuilder.addPoints(pointBuilder); |
| | | RtTaskList.Builder taskListBuilder = RtTaskList.newBuilder(); |
| | | taskListBuilder.addTasks(taskBuilder.build()); |
| | | |
| | | SnowFlake snowFlake = new SnowFlake(0, 0); |
| | | snowFlake.withStartTimestamp(1420041600000L); |
| | | long sequence = snowFlake.nextId(); |
| | | MqttPublishDto mqttPublishDto = xpuUtil.cloudCreateOrder(taskListBuilder.build(), dto.getVin(),sequence); |
| | | |
| | | // 正常单子才修改 巡游单子不修改状态 |
| | | if (!dto.getOrderId().contains("XU") && mqttPublishDto.getCode()== 200) { |
| | | // 修改车子为接单状态 |
| | | xpVehicleClient.updateVehicleStatus(new UpdateVehicleStateDto(dto.getVin(),1)); |
| | | return R.ok(true); |
| | | } |
| | | return R.ok(false); |
| | |
| | | |
| | | @PostMapping("/inner/cancelOrder") |
| | | R<Boolean> cancelOrder(@RequestBody PushOrderDto dto) { |
| | | OrderOperation operation = new OrderOperation(); |
| | | operation.setOrder_id(dto.getOrderId().toString()); |
| | | operation.setTask_id("CANCEL_MISSION_" + IdUtil.fastUUID()); |
| | | operation.setOperation_type(dto.getOperation_type()); |
| | | long sequence = snowflake.nextId(); |
| | | xpuUtil.cloudOrderOperation(operation, dto.getVin(), sequence); |
| | | redisService.setCacheObject(String.valueOf(sequence), "cancelOrder_" + dto.getOrderId() + "_" + dto.getVin(), 30L, TimeUnit.MINUTES); |
| | | return R.ok(true); |
| | | OrderOperation.Builder operationBuilder = OrderOperation.newBuilder(); |
| | | operationBuilder.setOrderId(dto.getOrderId().toString()); |
| | | operationBuilder.setTaskId("CANCEL_MISSION_" + IdUtil.fastUUID()); |
| | | if (dto.getOperation_type() != null) { |
| | | operationBuilder.setOperationType(OrderOperationType.forNumber(dto.getOperation_type().getNumber())); |
| | | } |
| | | |
| | | SnowFlake snowFlake = new SnowFlake(0, 0); |
| | | snowFlake.withStartTimestamp(1420041600000L); |
| | | long sequence = snowFlake.nextId(); |
| | | MqttPublishDto mqttPublishDto = xpuUtil.cloudOrderOperation(operationBuilder.build(), dto.getVin(), sequence); |
| | | |
| | | if (mqttPublishDto.getCode() == 200) { |
| | | if(!dto.getOrderId().contains("XU")){ |
| | | // 正常订单处理 |
| | | redisService.setCacheObject(String.valueOf(sequence), "cancelOrder_" + dto.getOrderId() + "_" + dto.getVin(), 30l, TimeUnit.MINUTES); |
| | | } |
| | | return R.ok(true); |
| | | } |
| | | |
| | | return R.ok(false); |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | //-----------------------------------------------用户测试阶段模拟车机报文调用流程接口实现业务闭环--------------------------------------// |
| | | |
| | | |
| | | @Resource |
| | | private XpOrderClient xpOrderClient; |
| | | |
| | | @Resource |
| | | private XpMapClient xpMapClient; |
| | | |
| | | @Resource |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | @Resource |
| | | public RedisTemplate redisTemplate; |
| | | |
| | | @Resource |
| | | private XpPushMessageClient xpPushMessageClient; |
| | | |
| | | |
| | | /** |
| | | * 模拟接单 |
| | | * @param orderId 订单id |
| | | * @param vehicleId 车辆id |
| | | * @param startLon 车子当前位置经度 |
| | | * @param startLat 车子当前位置维度 |
| | | * @return |
| | | */ |
| | | @GetMapping("/base/v1/order/acceptingOrder") |
| | | public R acceptingOrder(Long orderId, String code, Long vehicleId, Double startLon, Double startLat){ |
| | | if(null == vehicleId){ |
| | | vehicleId = 11L;//默认使用造的测试车辆 |
| | | } |
| | | if(null == startLat || null == startLon){ |
| | | startLat = 30.583407D; |
| | | startLon = 104.040251D; |
| | | } |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = null; |
| | | if(null != orderId){ |
| | | xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | } |
| | | if(StringUtils.isNotEmpty(code)){ |
| | | xpOrder = xpOrderClient.queryOrderByOrderNo(code).getData(); |
| | | } |
| | | orderId = xpOrder.getId(); |
| | | OrderRouteAndFareDto dto = new OrderRouteAndFareDto(); |
| | | dto.setOrderId(orderId); |
| | | LonLatDto origin = new LonLatDto(); |
| | | origin.setLongitude(startLon); |
| | | origin.setLatitude(startLat); |
| | | dto.setOrigin(origin); |
| | | LonLatDto destination = new LonLatDto(); |
| | | destination.setLongitude(xpOrder.getStartLon()); |
| | | destination.setLatitude(xpOrder.getStartLat()); |
| | | dto.setDestination(destination); |
| | | dto.setRouteType(1); |
| | | R<RouteVo> routeVoR = xpMapClient.getOrderDistanceAndDuration(dto); |
| | | if (200 != routeVoR.getCode()) { |
| | | throw new RuntimeException("获取订单线路和运费失败"); |
| | | } |
| | | RouteVo routeVo = routeVoR.getData(); |
| | | //缓存数据,用于后续推给乘客导航线路 |
| | | redisService.setCacheObject("ORDER_NAVIGATION_START_DATA:" + xpOrder.getAppUserId(), JSON.toJSONString(routeVo), 24L, TimeUnit.HOURS); |
| | | |
| | | PopOrderDto dto1 = new PopOrderDto(); |
| | | dto1.setOrderId(Long.valueOf(orderId)); |
| | | dto1.setXpVehicleVin(xpVehicle.getVehicleVin()); |
| | | dto1.setOrderType(com.xiaopeng.api.vo.OrderType.USER_ORDER); |
| | | R<?> r = xpOrderClient.popOrder(dto1); |
| | | |
| | | //构建推送消息 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | Map<String, Object> content = new HashMap<>(); |
| | | content.put("orderId", orderId.toString()); |
| | | content.put("status", 3); |
| | | params.put("type", PushMessageTypeEnum.ORDER_STATUS.getDesc()); |
| | | params.put("content", JSONObject.toJSONString(content)); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | String message = JSONObject.toJSONString(params); |
| | | Long appUserId = xpOrder.getAppUserId(); |
| | | SendMessageDTO dto2 = new SendMessageDTO(); |
| | | dto2.setAppUserId(appUserId + ""); |
| | | dto2.setMessage(message); |
| | | R<String> stringR = xpPushMessageClient.sendStatus(dto2); |
| | | if (R.isError(stringR)) { |
| | | log.error("推送消息失败:{}", stringR.getMsg()); |
| | | } |
| | | xpOrderClient.updateOrderStatus(orderId, OrderStatusEnum.PICK_UP.getCode()); |
| | | |
| | | // PushOrderRouteDTO dto3 = new PushOrderRouteDTO(); |
| | | // dto3.setOrderId(orderId); |
| | | // dto3.setRouteType(1); |
| | | // dto3.setLongitude(startLon); |
| | | // dto3.setLatitude(startLat); |
| | | // dto3.setDuration(10); |
| | | // dto3.setDistance(2); |
| | | // dto3.setDirection(60); |
| | | // xpPushMessageClient.pushOrderRoute(dto3); |
| | | |
| | | return r; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 推送司机接驾轨迹和实时位置 |
| | | * @param orderId |
| | | */ |
| | | @GetMapping("/base/v1/order/pushStartDriverLocation") |
| | | private void pushStartDriverLocation(Long vehicleId, Long orderId, String code){ |
| | | if(null == vehicleId){ |
| | | vehicleId = 1L; |
| | | } |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = null; |
| | | if(null != orderId){ |
| | | xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | } |
| | | if(StringUtils.isNotEmpty(code)){ |
| | | xpOrder = xpOrderClient.queryOrderByOrderNo(code).getData(); |
| | | } |
| | | orderId = xpOrder.getId(); |
| | | String vin = xpVehicle.getVehicleVin(); |
| | | Long appUserId = xpOrder.getAppUserId(); |
| | | // Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_START_DATA:" + xpOrder.getAppUserId()); |
| | | // RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class); |
| | | RouteVo routeVo =new RouteVo(); |
| | | ArrayList<LonLatDto> lonLatDtos = new ArrayList<>(); |
| | | LonLatDto lonLatDto1 = new LonLatDto(); |
| | | lonLatDto1.setLatitude(30.575685); |
| | | lonLatDto1.setLongitude(104.001118); |
| | | lonLatDtos.add(lonLatDto1); |
| | | routeVo.setPolyline(lonLatDtos); |
| | | |
| | | |
| | | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1000)); |
| | | Long finalOrderId = orderId; |
| | | Long finalOrderId1 = orderId; |
| | | threadPoolExecutor.execute(() -> { |
| | | //等待10秒后开始执行车子位移 |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | List<LonLatDto> polyline = routeVo.getPolyline(); |
| | | for (LonLatDto lonLatDto : polyline) { |
| | | Double latitude = lonLatDto.getLatitude(); |
| | | Double longitude = lonLatDto.getLongitude(); |
| | | |
| | | //保存车辆实时位置到redis |
| | | redisTemplate.opsForGeo().add("vehiclePosition:" + vin, new Point(longitude, latitude), vin); |
| | | |
| | | //保存车辆实时信息到mongodb |
| | | VehiclePosition vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("vin").is(vin)), VehiclePosition.class); |
| | | if (null == vehiclePosition) { |
| | | vehiclePosition = new VehiclePosition(); |
| | | vehiclePosition.setVin(vin); |
| | | vehiclePosition.setPoint(new GeoJsonPoint(longitude, latitude)); |
| | | vehiclePosition.setCourse(60D); |
| | | vehiclePosition.setAlt(100F); |
| | | vehiclePosition.setSpeed(50F); |
| | | vehiclePosition.setCurrentElectricity(80); |
| | | vehiclePosition.setMileageRange(300f); |
| | | vehiclePosition.setHasOrder(0); |
| | | vehiclePosition.setOrderId(finalOrderId1); |
| | | mongoTemplate.save(vehiclePosition); |
| | | }else{ |
| | | vehiclePosition.setOrderId(finalOrderId1); |
| | | Update update = new Update() |
| | | .set("point", new GeoJsonPoint(longitude, latitude)) |
| | | .set("course", 60D) |
| | | .set("alt", 100F) |
| | | .set("speed", 50F) |
| | | .set("currentElectricity", 80) |
| | | .set("mileageRange", 300f) |
| | | .set("hasOrder", 0) |
| | | .set("orderId", finalOrderId1); |
| | | mongoTemplate.updateFirst(new Query(Criteria.where("vin").is(vin)), update, VehiclePosition.class); |
| | | } |
| | | //调用推送服务接口 |
| | | if (null != finalOrderId) { |
| | | PushOrderRouteDTO dto = new PushOrderRouteDTO(); |
| | | dto.setOrderId(finalOrderId); |
| | | dto.setRouteType(1); |
| | | dto.setLongitude(longitude); |
| | | dto.setLatitude(latitude); |
| | | dto.setDuration(10); |
| | | dto.setDistance(2); |
| | | dto.setDirection(60); |
| | | xpPushMessageClient.pushOrderRoute(dto); |
| | | } |
| | | |
| | | //2秒钟循环一次 |
| | | try { |
| | | Thread.sleep(2000); |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | //构建推送消息 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | Map<String, Object> content = new HashMap<>(); |
| | | content.put("orderId", finalOrderId.toString()); |
| | | content.put("status", 4); |
| | | params.put("type", PushMessageTypeEnum.ORDER_STATUS.getDesc()); |
| | | params.put("content", JSONObject.toJSONString(content)); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | String message = JSONObject.toJSONString(params); |
| | | SendMessageDTO dto2 = new SendMessageDTO(); |
| | | dto2.setAppUserId(appUserId + ""); |
| | | dto2.setMessage(message); |
| | | R<String> stringR = xpPushMessageClient.sendStatus(dto2); |
| | | if (R.isError(stringR)) { |
| | | log.error("推送消息失败:{}", stringR.getMsg()); |
| | | } |
| | | xpOrderClient.updateOrderStatus(finalOrderId, OrderStatusEnum.PICK_UP_GUESTS.getCode()); |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改订单状态 |
| | | * @param orderId |
| | | * @param status |
| | | * @return |
| | | */ |
| | | @GetMapping("/base/v1/order/updateOrderStatus") |
| | | public R updateOrderStatus(Long orderId, String code, Integer status) { |
| | | XpOrder xpOrder = null; |
| | | if(null != orderId){ |
| | | xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | } |
| | | if(StringUtils.isNotEmpty(code)){ |
| | | xpOrder = xpOrderClient.queryOrderByOrderNo(code).getData(); |
| | | } |
| | | orderId = xpOrder.getId(); |
| | | //构建推送消息 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | Map<String, Object> content = new HashMap<>(); |
| | | content.put("orderId", orderId.toString()); |
| | | content.put("status", status); |
| | | params.put("type", PushMessageTypeEnum.ORDER_STATUS.getDesc()); |
| | | params.put("content", JSONObject.toJSONString(content)); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | String message = JSONObject.toJSONString(params); |
| | | SendMessageDTO dto2 = new SendMessageDTO(); |
| | | dto2.setAppUserId(xpOrder.getAppUserId() + ""); |
| | | dto2.setMessage(message); |
| | | R<String> stringR = xpPushMessageClient.sendStatus(dto2); |
| | | if (R.isError(stringR)) { |
| | | log.error("推送消息失败:{}", stringR.getMsg()); |
| | | } |
| | | xpOrderClient.updateOrderStatus(orderId, status); |
| | | if(5 == status){ |
| | | //主动推一次全程轨迹 |
| | | Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_END_DATA:" + xpOrder.getAppUserId()); |
| | | RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class); |
| | | com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject(); |
| | | jsonObject.put("type", "ORDER_ROUTE"); |
| | | jsonObject.put("timestamp", System.currentTimeMillis()); |
| | | com.alibaba.fastjson2.JSONObject content1 = new com.alibaba.fastjson2.JSONObject(); |
| | | content1.put("orderId", orderId.toString()); |
| | | content1.put("routeType", 1); |
| | | com.alibaba.fastjson2.JSONObject route = new com.alibaba.fastjson2.JSONObject(); |
| | | route.put("distance", routeVo.getDistance()); |
| | | route.put("duration", routeVo.getDuration()); |
| | | route.put("routeID", routeVo.getRouteID()); |
| | | |
| | | List<com.alibaba.fastjson2.JSONObject> collect = routeVo.getPolyline().stream().map(item -> { |
| | | com.alibaba.fastjson2.JSONObject point = new com.alibaba.fastjson2.JSONObject(); |
| | | point.put("latitude", item.getLatitude()); |
| | | point.put("longitude", item.getLongitude()); |
| | | return point; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | route.put("polyline", JSON.toJSONString(collect)); |
| | | content1.put("route", route.toJSONString()); |
| | | jsonObject.put("content", content1.toJSONString()); |
| | | SendMessageDTO messageDTO = new SendMessageDTO(); |
| | | messageDTO.setAppUserId(xpOrder.getAppUserId().toString()); |
| | | messageDTO.setMessage(jsonObject.toJSONString()); |
| | | xpPushMessageClient.sendRoute(messageDTO); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 推送司机接驾轨迹和实时位置 |
| | | * @param orderId |
| | | */ |
| | | @GetMapping("/base/v1/order/pushEndDriverLocation") |
| | | private void pushEndDriverLocation(Long vehicleId, Long orderId, String code){ |
| | | if(null == vehicleId){ |
| | | vehicleId = 1L; |
| | | } |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = null; |
| | | if(null != orderId){ |
| | | xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | } |
| | | if(StringUtils.isNotEmpty(code)){ |
| | | xpOrder = xpOrderClient.queryOrderByOrderNo(code).getData(); |
| | | } |
| | | orderId = xpOrder.getId(); |
| | | String vin = xpVehicle.getVehicleVin(); |
| | | Long appUserId = xpOrder.getAppUserId(); |
| | | Object cacheObject = redisService.getCacheObject("ORDER_NAVIGATION_END_DATA:" + xpOrder.getAppUserId()); |
| | | RouteVo routeVo = JSON.parseObject(cacheObject.toString(), RouteVo.class); |
| | | |
| | | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1000)); |
| | | Long finalOrderId = orderId; |
| | | Long finalOrderId1 = orderId; |
| | | threadPoolExecutor.execute(() -> { |
| | | //等待10秒后开始执行车子位移 |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | List<LonLatDto> polyline = routeVo.getPolyline(); |
| | | for (LonLatDto lonLatDto : polyline) { |
| | | Double latitude = lonLatDto.getLatitude(); |
| | | Double longitude = lonLatDto.getLongitude(); |
| | | |
| | | //保存车辆实时位置到redis |
| | | redisTemplate.opsForGeo().add("vehiclePosition:" + vin, new Point(longitude, latitude), vin); |
| | | |
| | | //保存车辆实时信息到mongodb |
| | | VehiclePosition vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("vin").is(vin)), VehiclePosition.class); |
| | | if (null == vehiclePosition) { |
| | | vehiclePosition = new VehiclePosition(); |
| | | vehiclePosition.setVin(vin); |
| | | vehiclePosition.setPoint(new GeoJsonPoint(longitude, latitude)); |
| | | vehiclePosition.setCourse(60D); |
| | | vehiclePosition.setAlt(100F); |
| | | vehiclePosition.setSpeed(50F); |
| | | vehiclePosition.setCurrentElectricity(80); |
| | | vehiclePosition.setMileageRange(300f); |
| | | vehiclePosition.setHasOrder(0); |
| | | vehiclePosition.setOrderId(finalOrderId1); |
| | | mongoTemplate.save(vehiclePosition); |
| | | }else{ |
| | | vehiclePosition.setOrderId(finalOrderId1); |
| | | Update update = new Update() |
| | | .set("point", new GeoJsonPoint(longitude, latitude)) |
| | | .set("course", 60D) |
| | | .set("alt", 100F) |
| | | .set("speed", 50F) |
| | | .set("currentElectricity", 80) |
| | | .set("mileageRange", 300f) |
| | | .set("hasOrder", 0) |
| | | .set("orderId", finalOrderId1); |
| | | mongoTemplate.updateFirst(new Query(Criteria.where("vin").is(vin)), update, VehiclePosition.class); |
| | | } |
| | | //调用推送服务接口 |
| | | if (null != finalOrderId) { |
| | | PushOrderRouteDTO dto = new PushOrderRouteDTO(); |
| | | dto.setOrderId(finalOrderId); |
| | | dto.setRouteType(1); |
| | | dto.setLongitude(longitude); |
| | | dto.setLatitude(latitude); |
| | | dto.setDuration(20); |
| | | dto.setDistance(10); |
| | | dto.setDirection(60); |
| | | xpPushMessageClient.pushOrderRoute(dto); |
| | | |
| | | //存储历史轨迹 |
| | | OrderTrajectoryVo orderTrajectory = new OrderTrajectoryVo(); |
| | | orderTrajectory.setOrderId(finalOrderId); |
| | | orderTrajectory.setLocation(longitude + "," + latitude); |
| | | orderTrajectory.setDirection(dto.getDirection()); |
| | | orderTrajectory.setSpeed(0D); |
| | | orderTrajectory.setLocatetime(System.currentTimeMillis()); |
| | | orderTrajectory.setCreateTime(new Date()); |
| | | xpOrderClient.saveVehicleLocation(orderTrajectory); |
| | | } |
| | | |
| | | //5秒钟循环一次 |
| | | try { |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | //构建推送消息 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | Map<String, Object> content = new HashMap<>(); |
| | | content.put("orderId", finalOrderId.toString()); |
| | | content.put("status", 7); |
| | | params.put("type", PushMessageTypeEnum.ORDER_STATUS.getDesc()); |
| | | params.put("content", JSONObject.toJSONString(content)); |
| | | params.put("timestamp", System.currentTimeMillis() + ""); |
| | | String message = JSONObject.toJSONString(params); |
| | | SendMessageDTO dto2 = new SendMessageDTO(); |
| | | dto2.setAppUserId(appUserId + ""); |
| | | dto2.setMessage(message); |
| | | R<String> stringR = xpPushMessageClient.sendStatus(dto2); |
| | | if (R.isError(stringR)) { |
| | | log.error("推送消息失败:{}", stringR.getMsg()); |
| | | } |
| | | xpOrderClient.updateOrderStatus(finalOrderId, OrderStatusEnum.SEE_OFF_GUESTS.getCode()); |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |