本文說明建立及管理車輛時可更新的重要車輛欄位。
|
如需車輛欄位的完整清單,請參閱: |
車輛屬性欄位
使用車輛 attributes
欄位建立自訂條件,讓消費者或車隊營運商能根據更多搜尋條件,在車隊中尋找車輛。這項功能可提升應用程式的效能,提供比單純根據其他車輛欄位搜尋條件更合適的車輛。每輛車最多可有 100 個屬性,且每個屬性都必須有專屬鍵。值可以是字串、布林值或數字。
舉例來說,您可以宣告名為「zone」的自訂屬性,區分送貨車輛在城市中營運的區域。您可以使用下列字串值代表不同區域:1B
、2C
和 3A
。然後,您可以在車隊追蹤功能中使用篩選器,只向負責特定區域的作業人員顯示在該區域作業的車輛。
不過,自訂屬性值不一定要互斥。你可能會使用 available-at-night 和 has-refrigeration 等條件。這些都可以是使用布林值的個別自訂屬性。特定車輛可同時指派這三項自訂屬性,以及設為適當字串值的區域自訂屬性。
更新車輛屬性
每個 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