Tạo xe giao hàng cho các nhiệm vụ đã lên lịch

Tài liệu này mô tả cách tạo xe từ môi trường máy chủ bằng cách sử dụng gRPC hoặc REST. Bạn có thể tạo xe thông qua SDK tài xế, được cung cấp bạn đã cung cấp ứng dụng làm môi trường đáng tin cậy bằng cách sử dụng thông tin xác thực.

Để tìm hiểu cách sử dụng SDK cho người lái xe để tạo xe, hãy xem các nội dung sau:

Để tạo xe mới từ môi trường máy chủ, hãy tạo một Yêu cầu CreateDeliveryVehicle đến Fleet Engine. Sử dụng Đối tượng CreateDeliveryVehicleRequest để xác định các thuộc tính của đối tượng mới xe giao hàng.

Cánh đồng cho xe thực hiện nhiệm vụ theo lịch

Khi tạo DeliveryVehicle, bạn cần đặt các trường không bắt buộc sau:

  • attributes
  • last_location
  • type

Để tạo xe mà không cần thiết lập các trường không bắt buộc, bạn có thể để Chưa đặt trường DeliveryVehicle trong CreateDeliveryVehicleRequest.

Tạo mẫu về xe

Bạn có thể dùng thư viện Java gRPC để tạo xe hoặc REST.

Java

  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890"; // Avoid auto-incrementing IDs.

  DeliveryServiceBlockingStub deliveryService =
    DeliveryServiceGrpc.newBlockingStub(channel);

  // Vehicle settings
  String parent = "providers/" + PROJECT_ID;
  DeliveryVehicle vehicle = DeliveryVehicle.newBuilder()
    .addAttributes(DeliveryVehicleAttribute.newBuilder()
      .setKey("route_number").setValue("1"))  // Opaque to the Fleet Engine
    .build();

  // Vehicle request
  CreateDeliveryVehicleRequest createVehicleRequest =
    CreateDeliveryVehicleRequest.newBuilder()  // No need for the header
        .setParent(parent)
        .setDeliveryVehicleId(VEHICLE_ID)     // Vehicle ID assigned by the Provider
        .setDeliveryVehicle(vehicle)
        .build();

  // Error handling
  // If Fleet Engine does not have vehicle with that ID and the credentials of the
  // requestor pass, the service creates the vehicle successfully.

  try {
    DeliveryVehicle createdVehicle =
      deliveryService.createDeliveryVehicle(createVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case ALREADY_EXISTS:
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }

Kiến trúc chuyển trạng thái đại diện (REST)

Để tạo xe từ môi trường máy chủ, hãy thực hiện lệnh gọi HTTP REST đến CreateDeliveryVehicle:

POST https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles?deliveryVehicleId=<id>

Phần nội dung POST đại diện cho thực thể DeliveryVehicle sẽ được tạo. Bạn có thể chỉ định các trường tuỳ chọn sau:

  • attributes
  • lastLocation
  • type
  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}" \
  --data-binary @- << EOM
  {
    "attributes": [{"key": "model", "value": "sedan"}],
    "lastLocation": {"location": {"latitude": 12.1, "longitude": 14.5}}
  }
  EOM

Để tạo xe mà không thiết lập trường nào, hãy rời khỏi phần nội dung của yêu cầu POST trống. Chiếc xe mới được tạo sau đó trích xuất mã xe từ Tham số deliveryVehicleId trong URL POST.

Ví dụ:

  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}"

Các bước tiếp theo