|
本文說明建立及管理車輛時可更新的重要車輛欄位。
|
如需車輛欄位的完整清單,請參閱: |
車輛屬性欄位
您可以使用車輛 attributes 欄位建立自訂條件,讓消費者或車隊營運商能透過更多元的搜尋條件,找到車隊中的車輛。這項功能可提升應用程式的效能,提供比單純使用其他車輛欄位搜尋條件更合適的車輛。每輛車最多可有 100 個屬性,且每個屬性都必須有專屬鍵。值可以是字串、布林值或數字。
舉例來說,您可以宣告名為 zone 的自訂屬性,區分送貨車輛在城市中的營運區域。您可以使用下列字串值代表不同區域:1B、2C 和 3A。接著,您可以在車隊追蹤中使用篩選器,只向負責特定區域的營運商顯示在該區域工作的車輛。
不過,自訂屬性值不一定要互斥。您可以使用「available-at-night」和「has-refrigeration」等條件。這些都可以是使用布林值的獨立自訂屬性。您可以為特定車輛指派這三項自訂屬性,並將 zone 自訂屬性設為適當的字串值。
更新車輛屬性
每輛車的每個 attributes 鍵只能有一個值。您可以使用欄位遮罩中的 attributes 宣告自訂車輛屬性,然後根據下列方法提供值。
這個 UpdateDeliveryVehicle API 不允許只更新單一屬性。使用這個方法時,只要在欄位遮罩中使用 attributes 欄位,系統就會為車輛重新宣告「整組」車輛屬性。這會導致任何未明確包含在欄位遮罩中的預先存在屬性遭到覆寫。如果使用這個方法宣告新的自訂屬性,也必須重新宣告要讓車輛保留的每個自訂屬性。如果從欄位遮罩中排除 attributes,這個方法會保留車輛先前定義的現有自訂屬性。如果您在欄位遮罩中使用 attributes,但未設定值,就等同於從車輛中移除所有自訂屬性。
更新車輛欄位範例
本節說明如何使用 UpdateDeliveryVehicleRequest 更新車輛欄位,其中包含 update_mask,指出要更新的欄位。詳情請參閱通訊協定緩衝區說明文件的欄位遮罩部分。
如要更新 last_location 以外的欄位,則須具備 Fleet Engine Delivery 管理員權限。
範例:設定自訂屬性
這個範例指定了新屬性:zone。如先前在「更新車輛屬性」一節中所述,使用這種方法更新 attributes 欄位時,必須指出所有要保留的自訂屬性。因此,範例顯示寫入 available-at-night 值,以防止在指定 attributes 欄位的更新作業中遭到覆寫。
請參閱 providers.deliveryVehicles.patch 參考資料。
gRPC
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()
.addAllAttributes(ImmutableList.of(
DeliveryVehicleAttribute.newBuilder().setKey("zone").setValue("1B").build(),
DeliveryVehicleAttribute.newBuilder().setKey("available-at-night").setValue("true").build()))
.build();
// DeliveryVehicle request
UpdateDeliveryVehicleRequest updateDeliveryVehicleRequest =
UpdateDeliveryVehicleRequest.newBuilder() // No need for the header
.setName(vehicleName)
.setDeliveryVehicle(myDeliveryVehicle)
.setUpdateMask(FieldMask.newBuilder()
.addPaths("attributes"))
.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
# 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=attributes" \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${JWT}" \
--data-binary @- << EOM
{
"attributes": [
{"key": "zone", "value": "1B"},
{"key": "available-at-night", "value": "true"}
]
}
EOM