Approaches
為確保 Fleet Engine 發揮最佳效能,請至少每分鐘提供一次車輛位置更新串流,最多每 5 秒提供一次。請使用下列任一方式提供這些更新:
- 使用 Driver SDK:最簡單的做法。
- 使用自訂程式碼:如果位置資訊是透過後端轉送,或是您使用 Android 或 iOS 以外的裝置,這項功能就很實用。本文件將說明這項做法。
無論您提供車輛位置更新的方式為何,當運送車輛前往停靠站點和抵達停靠站點時,後端都必須負責更新 Fleet Engine。這包括倉庫本身。Fleet Engine 不會自動偵測這些事件。
更新車輛位置範例
您可以使用 Java gRPC 程式庫更新車輛在車隊引擎中的所在位置,或使用 REST。
Java
static final String PROJECT_ID = "my-delivery-co-gcp-project";
static final String VEHICLE_ID = "vehicle-8241890";
DeliveryServiceBlockingStub deliveryService =
DeliveryServiceGrpc.newBlockingStub(channel);
// Vehicle settings
String vehicleName = "providers/" + PROJECT_ID + "/deliveryVehicles/" + VEHICLE_ID;
DeliveryVehicle myDeliveryVehicle = DeliveryVehicle.newBuilder()
.setLastLocation(DeliveryVehicleLocation.newBuilder()
.setSupplementalLocation(LatLng.newBuilder()
.setLatitude(37.3382)
.setLongitude(121.8863))
.setSupplementalLocationTime(now())
.setSupplementalLocationSensor(DeliveryVehicleLocationSensor.CUSTOMER_SUPPLIED_LOCATION)
.setSupplementalLocationAccuracy(DoubleValue.of(15.0))) // Optional
.build();
// DeliveryVehicle request
UpdateDeliveryVehicleRequest updateDeliveryVehicleRequest =
UpdateDeliveryVehicleRequest.newBuilder() // No need for the header
.setName(vehicleName)
.setDeliveryVehicle(myDeliveryVehicle)
.setUpdateMask(FieldMask.newBuilder()
.addPaths("last_location"))
.build();
try {
DeliveryVehicle updatedDeliveryVehicle =
deliveryService.updateDeliveryVehicle(updateDeliveryVehicleRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case NOT_FOUND:
break;
case PERMISSION_DENIED:
break;
}
return;
}
REST
PATCH https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles/<id>?updateMask=last_location
要求詳細資料
要求主體必須包含 DeliveryVehicle
實體,並指定下列欄位:
必填欄位:
欄位 值 lastLocation.supplementalLocation
車輛的位置。 lastLocation.supplementalLocationTime
車輛上次停在這個地點的時間戳記。 lastLocation.supplementalLocationSensor
應填入 CUSTOMER_SUPPLIED_LOCATION
。選用欄位:
欄位 值 lastLocation.supplementalLocationAccuracy
所提供位置的精確度 (以公尺為單位)。
# Set JWT, PROJECT_ID, VEHICLE_ID, TASK1_ID, and TASK2_ID in the local
# environment
curl -X PATCH "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles/${VEHICLE_ID}?updateMask=remainingVehicleJourneySegments" \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${JWT}" \
--data-binary @- << EOM
{
"lastLocation": {
"supplementalLocation": {"latitude": 12.1, "longitude": 14.5},
"supplementalLocationTime": "$(date -u --iso-8601=seconds)",
"supplementalLocationSensor": "CUSTOMER_SUPPLIED_LOCATION",
"supplementalLocationAccuracy": 15
}
}
EOM