创建共享泳池行程

本文档介绍了如何创建共享池化行程,以及如何设置正确的 字段,并将其分配给车辆以完成任务。本文假定您已设置舰队 你已经创建了车辆,有了有效的驾驶应用, 消费者应用(可选)。您还应该熟悉 为按需行程提供的场景。请参阅以下相关指南 :

行程创建基础知识

本部分介绍了在 Fleet Engine。您可以使用 gRPC 和 REST 发出创建请求。

  • CreateTrip() 方法:gRPCREST
  • CreateTripRequest 消息:仅 gRPC

行程字段

使用以下字段在 Fleet Engine 中创建行程。您可以使用不同的 为单目的地或多目的地、 或共用泳池旅行等。您 可以在创建行程时提供可选字段,也可将它们设置为 更新行程的时间。

行程字段
名称 必需? 说明
父级 包含项目 ID 的字符串。此 ID 必须与所用的 ID 相同 在整个 Fleet Engine 集成中,使用同一服务账号 角色。
trip_id 您创建的一个字符串,用于唯一标识此行程。行程 ID 包含 如参考信息中所述。
trip_type 针对您要创建的行程类型,将 TripType 设置为以下值: <ph type="x-smartling-placeholder">
    </ph>
  • 单个目的地:设置为 SHAREDEXCLUSIVE
  • 多目的地:设置为 EXCLUSIVE
  • Back-to-back:设置为 EXCLUSIVE
  • 共享池:设置为 SHARED
pickup_point 行程的起点。
中间目的地

仅限多目的地行程:司机在其间访问的中间目的地列表 上车点和下车点。与 dropoff_point 一样,此字段 也可以稍后通过调用 UpdateTrip 进行设置,但这是一个多目的地 根据定义,trip 包含中间目的地。

vehicle_waypoints

仅限共享池化行程:此字段支持交错多个行程的航点。 它包含指定车辆的所有剩余航点,以及 用作此行程的上车点和下车点。您可以设置此字段 方法是调用 CreateTripUpdateTrip。您还可以 通过 waypoints 字段使用 对 UpdateVehicle 的调用。 该服务不会在调用 GetTrip 时返回此信息 。

number_of_passengers 相应行程的乘客人数。
dropoff_point 行程的目的地。
vehicle_id 为行程分配的车辆 ID。

示例:创建共享池化行程

以下后端集成示例演示了如何创建行程和 将其指定为共享拼车行程。

// Vehicle with VEHICLE_ID ID is already created and it is assigned Trip A.

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "shared-trip-A";
static final String VEHICLE_ID = "your-vehicle-id";
static final String TRIP_A_ID = "trip-a-id";
static final String TRIP_B_ID = "trip-b-id";

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

String parent = "providers/" + PROJECT_ID;

LatLng tripBPickup =
    LatLng.newBuilder().setLatitude(-12.12314).setLongitude(88.142123).build();
LatLng tripBDropoff =
    LatLng.newBuilder().setLatitude(-14.12314).setLongitude(90.142123).build();

TerminalLocation tripBPickupTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBPickup).build();
TerminalLocation tripBDropoffTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBDropoff).build();

// TripA already exists and it's assigned to a vehicle with VEHICLE_ID ID.
Trip tripB = Trip.newBuilder()
    .setTripType(TripType.SHARED)
    .setVehicleId(VEHICLE_ID)
    .setPickupPoint(tripBPickupTerminalLocation)
    .setDropoffPoint(tripBDropoffTerminalLocation)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don't specify an order, then the Fleet Engine adds Trip B's
        // waypoints to the end of Trip A's.
        ImmutableList.of(
            // Trip B's pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripBPickupTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripBDropoffTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();

// Create Trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
    .setParent(parent)
    .setTripId(TRIP_B_ID)
    .setTrip(tripB)
    .build();

try {
  // createdTrip.remainingWaypoints will contain shared-pool waypoints.
  // [tripB.pickup, tripA.dropoff, tripB.dropoff]
  Trip createdTrip = tripService.createTrip(createTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}

更新共享池化行程

在 Fleet Engine 中创建的任何行程都必须分配给车辆, Fleet Engine 用于计算行程预计到达时间并进行跟踪。您可以在 行程创建,或稍后在更新行程时创建。

对于共享池化行程,您必须为未访问航点指定顺序 在行程的车辆航点集合 (Trip.vehicle_waypoints) 中。舰队 引擎会使用此列表自动更新所有行程的行程航点 共享池中的对象。

例如,假设有行程 A 和两个共享泳池行程 行程 B

  • 行程 A 正在前往下车点。
  • 然后,行程 B 会添加到同一辆车中。

行程 B 的一个 UpdateTripRequest 中, 您设置了 vehicleId,并将 Trip.vehicle_waypoints 设置为最佳 航点顺序:B 上车用户流失B 流失

  • 调用 getVehicle() 会返回 remainingWaypoints 包含:
    B 上车点流失B 流失
  • getTrip()onTripRemainingWaypointsUpdated 回调 行程 A 返回 remainingWaypoints 包含:
    B 自提离开
  • getTrip()onTripRemainingWaypointsUpdated 回调 行程 B 返回 remainingWaypoints 包含:
    B 上车点A 下车点B 流失

示例

以下后端集成示例演示了如何使用 两个共享泳池行程的车辆 ID 和航点。

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_A_ID = "share-trip-A";
static final String TRIP_B_ID = "share-trip-B";
static final String VEHICLE_ID = "Vehicle";

String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_B_ID;

// Get Trip A and Trip B objects from either the Fleet Engine or storage.
Trip tripA = …;
Trip tripB = …;

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

// The trip settings to update.
Trip trip = Trip.newBuilder()
    .setVehicleId(VEHICLE_ID)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don't specify an order, then the Fleet Engine adds Trip B's
        // waypoints to the end of Trip A's.
        ImmutableList.of(
            // Trip B's pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getPickupPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getDropoffPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();

// The trip update request.
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
    .setName(tripName)
    .setTrip(trip)
    .setUpdateMask(FieldMask.newBuilder()
        .addPaths("vehicle_id")
        .addPaths("vehicle_waypoints"))
    .build();

// Error handling. If Fleet Engine has both a trip and vehicle with the IDs,
// and if the credentials validate, and if the given vehicle_waypoints list
// is valid, then the service updates the trip.
try {
  Trip updatedTrip = tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case NOT_FOUND:          // Either the trip or vehicle does not exist.
      break;
    case PERMISSION_DENIED:
      break;
    case INVALID_REQUEST:    // vehicle_waypoints is invalid.
      break;
  }
  return;
}

后续步骤