Tài liệu này mô tả cách tạo xe từ môi trường máy chủ bằng gRPC hoặc REST. Bạn có thể tạo xe từ Driver SDK, miễn là bạn đã cung cấp ứng dụng dưới dạng môi trường đáng tin cậy bằng thông tin đăng nhập thích hợp.
Để tìm hiểu cách sử dụng Driver SDK để tạo xe, hãy xem các phần sau:
- Driver SDK cho các tác vụ theo lịch
- Vai trò tài khoản dịch vụ trong Các yếu tố cần thiết của Fleet Engine.
Để tạo một xe mới từ môi trường máy chủ, hãy gửi 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 xe giao hàng mới.
Các trường cho xe thực hiện tác vụ theo lịch
Khi tạo DeliveryVehicle, bạn sẽ đặt các trường không bắt buộc sau đây:
attributeslast_locationtype
Để tạo xe mà không cần đặt bất kỳ trường không bắt buộc nào, bạn có thể để trống trường
DeliveryVehicle trong CreateDeliveryVehicleRequest.
Ví dụ về cách tạo xe
Để tạo xe, bạn có thể sử dụng thư viện gRPC Java hoặc REST. Để xem danh sách đầy đủ các ngôn ngữ được hỗ trợ, hãy xem Thư viện ứng dụng cho các dịch vụ tác vụ theo lịch.
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;
}
REST
Để tạo xe từ môi trường máy chủ, hãy gửi lệnh gọi HTTP REST đến CreateDeliveryVehicle:
POST https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles?deliveryVehicleId=<id>
Nội dung POST đại diện cho thực thể DeliveryVehicle cần tạo. Bạn có thể chỉ định các trường không bắt buộc sau đây:
attributeslastLocationtype
# 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 cần đặt bất kỳ trường nào, hãy để trống nội dung của yêu cầu POST. Sau đó, xe mới tạo sẽ 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}"