| | |
| | | |
| | | 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.protobuf.*; |
| | | import com.xiaopeng.api.vo.operation.RouteVo; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudOrderOperation.OrderOperation; |
| | | import com.xiaopeng.api.vo.automaticPilot.proto.down.cloudOrderOperation.OrderOperationType; |
| | | 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.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.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Slf4j |
| | |
| | | @Api(tags = "订单任务Api") |
| | | public class OrderMissionController { |
| | | |
| | | @Resource |
| | | private XPUUtil xpuUtil; |
| | | @Resource |
| | | private RedisService redisService; |
| | | @Resource |
| | | private XpVehicleClient xpVehicleClient; |
| | | private final XPUUtil xpuUtil; |
| | | private final RedisService redisService; |
| | | private final XpVehicleClient xpVehicleClient; |
| | | |
| | | public OrderMissionController(XPUUtil xpuUtil, RedisService redisService, XpVehicleClient xpVehicleClient, Snowflake snowflake) { |
| | | this.xpuUtil = xpuUtil; |
| | | this.redisService = redisService; |
| | | this.xpVehicleClient = xpVehicleClient; |
| | | } |
| | | |
| | | @PostMapping("/inner/pushOrder") |
| | | public R<Boolean> pushOrder(@RequestBody PushOrderDto dto) { |
| | | RtTask.Builder taskBuilder = RtTask.newBuilder(); |
| | | RTTask task = new RTTask(); |
| | | //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); |
| | | } |
| | | RtTaskList.Builder taskListBuilder = RtTaskList.newBuilder(); |
| | | taskListBuilder.addTasks(taskBuilder.build()); |
| | | |
| | | task.setOrder_id(dto.getOrderId().toString()); |
| | | task.setTask_id("MISSION_"+ IdUtil.fastUUID()); |
| | | task.setTask_type(TaskType.ROBOTAXI); |
| | | task.setStatus(TaskStatus.PENDING); |
| | | 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) { |
| | | MqttPublishDto mqttPublishDto = xpuUtil.cloudCreateOrder(task, dto.getVin(),sequence); |
| | | if (mqttPublishDto.getCode()== 200) { |
| | | // 修改车子为接单状态 |
| | | xpVehicleClient.updateVehicleStatus(new UpdateVehicleStateDto(dto.getVin(),1)); |
| | | |
| | |
| | | |
| | | @PostMapping("/inner/cancelOrder") |
| | | R<Boolean> cancelOrder(@RequestBody PushOrderDto dto) { |
| | | 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().getValue())); |
| | | } |
| | | |
| | | SnowFlake snowFlake = new SnowFlake(0, 0); |
| | | snowFlake.withStartTimestamp(1420041600000L); |
| | | long sequence = snowFlake.nextId(); |
| | | xpuUtil.cloudOrderOperation(operationBuilder.build(), dto.getVin(), sequence); |
| | | redisService.setCacheObject(String.valueOf(sequence), "cancelOrder_" + dto.getOrderId() + "_" + dto.getVin(), 30l, TimeUnit.MINUTES); |
| | | return R.ok(true); |
| | | |
| | | |
| | | OrderOperation operation = new OrderOperation(); |
| | | operation.setOrder_id(dto.getOrderId().toString()); |
| | | operation.setTask_id("CANCEL_MISSION_"+ IdUtil.fastUUID()); |
| | | operation.setOperation_type(dto.getOperation_type()); |
| | | SnowFlake snowFlake = new SnowFlake(0, 0); |
| | | snowFlake.withStartTimestamp(1420041600000L); |
| | | 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); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | //-----------------------------------------------用户测试阶段模拟车机报文调用流程接口实现业务闭环--------------------------------------// |
| | | |
| | | |
| | | @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, Long vehicleId, Double startLon, Double startLat){ |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | 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(), com.alibaba.fastjson.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); |
| | | 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()); |
| | | return r; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 推送司机接驾轨迹和实时位置 |
| | | * @param orderId |
| | | */ |
| | | @GetMapping("/base/v1/order/pushStartDriverLocation") |
| | | private void pushStartDriverLocation(Long vehicleId, Long orderId){ |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | 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); |
| | | |
| | | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1000)); |
| | | threadPoolExecutor.execute(() -> { |
| | | //等待10秒后开始执行车子位移 |
| | | try { |
| | | Thread.sleep(10000); |
| | | } 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 GeoJsonPoint(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.setOrderId(orderId); |
| | | mongoTemplate.save(vehiclePosition); |
| | | //调用推送服务接口 |
| | | if (null != orderId) { |
| | | PushOrderRouteDTO dto = new PushOrderRouteDTO(); |
| | | dto.setOrderId(orderId); |
| | | dto.setRouteType(1); |
| | | dto.setLongitude(longitude); |
| | | dto.setLatitude(latitude); |
| | | //TODO 这两个字段现在拿不到 |
| | | // dto.setDuration(rtTripInfo.getRemaining_time()); |
| | | // dto.setDistance(rtTripInfo.getRemaining_distance().intValue()); |
| | | xpPushMessageClient.pushOrderRoute(dto); |
| | | } |
| | | |
| | | //5秒钟循环一次 |
| | | 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", orderId); |
| | | 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(orderId, OrderStatusEnum.PICK_UP_GUESTS.getCode()); |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改订单状态 |
| | | * @param orderId |
| | | * @param status |
| | | * @return |
| | | */ |
| | | @GetMapping("/base/v1/order/updateOrderStatus") |
| | | public R updateOrderStatus(Long orderId, Integer status) { |
| | | XpOrder xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | //构建推送消息 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | Map<String, Object> content = new HashMap<>(); |
| | | content.put("orderId", orderId); |
| | | 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); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 推送司机接驾轨迹和实时位置 |
| | | * @param orderId |
| | | */ |
| | | @GetMapping("/base/v1/order/pushEndDriverLocation") |
| | | private void pushEndDriverLocation(Long vehicleId, Long orderId){ |
| | | XpVehicle xpVehicle = xpVehicleClient.queryById(vehicleId).getData(); |
| | | XpOrder xpOrder = xpOrderClient.queryOrderById(orderId).getData(); |
| | | 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)); |
| | | threadPoolExecutor.execute(() -> { |
| | | //等待10秒后开始执行车子位移 |
| | | try { |
| | | Thread.sleep(10000); |
| | | } 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 GeoJsonPoint(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.setOrderId(orderId); |
| | | mongoTemplate.save(vehiclePosition); |
| | | //调用推送服务接口 |
| | | if (null != orderId) { |
| | | PushOrderRouteDTO dto = new PushOrderRouteDTO(); |
| | | dto.setOrderId(orderId); |
| | | dto.setRouteType(1); |
| | | dto.setLongitude(longitude); |
| | | dto.setLatitude(latitude); |
| | | //TODO 这两个字段现在拿不到 |
| | | // dto.setDuration(rtTripInfo.getRemaining_time()); |
| | | // dto.setDistance(rtTripInfo.getRemaining_distance().intValue()); |
| | | xpPushMessageClient.pushOrderRoute(dto); |
| | | } |
| | | |
| | | //5秒钟循环一次 |
| | | 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", orderId); |
| | | 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(orderId, OrderStatusEnum.PICK_UP_GUESTS.getCode()); |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |