完成任务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档假定您了解如何创建和使用任务。它提供了有关如何完成配送任务的具体示例,如下所示:
关闭任务
您可以通过以下方式关闭任务:
- 更新车辆的停止状态。您从车辆中移除停靠站,这会关闭与该停靠站关联的所有任务。如需了解详情,请参阅更新停止状态。
- 从车辆停靠站列表中移除任务。这涉及更新停靠站的任务列表,但已关闭的任务不再属于该列表。请参阅更新任务中的“更新任务顺序”。
- 将任务状态设置为
CLOSED
。此操作只能针对未分配给车辆的任务执行。本部分介绍了此方法。
任务关闭后,您可能无法重新打开。
关闭任务并不表示任务成功或失败。表示任务不再被视为正在进行。为了指明任务的实际结果,并让系统出于车队跟踪和行程分享目的显示该结果,您必须指明任务的实际结果。请参阅下文的设置任务结果。
用于关闭任务的任务字段
本部分介绍了关闭任务时需要设置的字段。Fleet Engine 会忽略实体中用于更新的所有其他字段。
必需字段 |
值 |
state |
State.CLOSED |
直接关闭任务
以下示例展示了如何通过 gRPC 或使用对 UpdateTask
的 HTTP REST 请求调用将未分配的任务设置为关闭状态
gRPC
static final String PROJECT_ID = "my-delivery-co-gcp-project";
static final String TASK_ID = "task-8241890";
DeliveryServiceBlockingStub deliveryService =
DeliveryServiceGrpc.newBlockingStub(channel);
// Task settings
String taskName = "providers/" + PROJECT_ID + "/tasks/" + TASK_ID;
Task task = Task.newBuilder()
.setName(taskName)
.setState(Task.State.CLOSED) // You can only directly CLOSE a
.build(); // task that is NOT assigned to a vehicle.
// Task request
UpdateTaskRequest updateTaskRequest =
UpdateTaskRequest.newBuilder() // No need for the header
.setTask(task)
.setUpdateMask(FieldMask.newBuilder().addPaths("state"))
.build();
try {
Task updatedTask = deliveryService.updateTask(updateTaskRequest);
} 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>/tasks/<id>?updateMask=state
- <id> 是任务的唯一标识符。
- 请求标头必须包含一个 Authorization 字段,其值为 Bearer <token>,其中 <token> 是由您的服务器根据服务账号角色和 JSON Web 令牌中所述的准则颁发的。
- 您必须在请求正文中包含
Task
实体
示例 curl
命令:
# Set JWT, PROJECT_ID, and TASK_ID in the local environment
curl -X PATCH "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks/${TASK_ID}?updateMask=state,taskOutcome,taskOutcomeTime" \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${JWT}" \
--data-binary @- << EOM
{
"state": "CLOSED",
"taskOutcome": "SUCCEEDED",
"taskOutcomeTime": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
}
EOM
设置任务结果
如需指明任务的实际结果,请将已关闭任务的结果设置为 SUCCEEDED
或 FAILED
。您必须先关闭任务,然后才能设置其结果。Fleet Engine 仅针对状态为 SUCCEEDED
的配送任务收费。
任务结果详情
任务还提供有关任务结果的其他详细信息。您可以直接设置这些参数,Fleet Engine 会遵循您的设置:
- 任务结果位置:Fleet Engine 会自动使用车辆的最新已知位置填充任务结果位置。如果您愿意,可以改为提供此信息。
- 任务结果时间:Fleet Engine 不会填充此字段,但您可以自行设置。
您可以使用以下任一方法来设置 task_outcome_location
和 task_outcome_time
:
- 在设置任务结果的同一请求中更新它们。
- 在设置任务结果后,稍后更新。
- 在设置后再次修改。
Fleet Engine 会阻止以下与任务结果相关的更新:
- 任务结果一旦设置为
SUCCEEDED
或 FAILED
,便无法再进行修改。
- 对于没有设置结果的任务,您无法设置任务结果位置或结果时间。
用于设置结果的任务字段
本部分介绍了设置任务结果时需要设置的必填字段和可选字段。Fleet Engine 会忽略实体中用于更新的其他字段。
必需字段 |
值 |
taskOutcome |
Outcome.SUCCEEDED 或 Outcome.FAILED |
选填字段 | 值 |
taskOutcomeLocation |
任务完成的地点。如果未设置,Fleet Engine 会将此值默认为车辆的上次位置。 |
taskOutcomeTime |
任务完成时的时间戳。 |
任务结果示例
以下示例展示了如何使用 Java gRPC 库和 HTTP REST 调用 UpdateTask
将任务结果设置为 SUCCEEDED
,并设置任务完成的位置。
gRPC
static final String PROJECT_ID = "my-delivery-co-gcp-project";
static final String TASK_ID = "task-8241890";
DeliveryServiceBlockingStub deliveryService =
DeliveryServiceGrpc.newBlockingStub(channel);
// Task settings
String taskName = "providers/" + PROJECT_ID + "/tasks/" + TASK_ID;
Task task = Task.newBuilder()
.setName(taskName)
.setTaskOutcome(TaskOutcome.SUCCEEDED)
.setTaskOutcomeTime(now())
.setTaskOutcomeLocation( // Grand Indonesia East Mall
LocationInfo.newBuilder().setPoint(
LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))
.build();
// Task request
UpdateTaskRequest updateTaskRequest =
UpdateTaskRequest.newBuilder() // No need for the header
.setTask(task)
.setUpdateMask(FieldMask.newBuilder().addPaths("task_outcome", "task_outcome_time", "task_outcome_location"))
.build();
try {
Task updatedTask = deliveryService.updateTask(updateTaskRequest);
} 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>/tasks/<id>?updateMask=taskOutcome,taskOutcomeTime,taskOutcomeLocation
- <id> 是任务的唯一标识符。
- 请求标头必须包含一个 Authorization 字段,其值为 Bearer <token>,其中 <token> 是由您的服务器根据服务账号角色和 JSON Web 令牌中所述的准则颁发的。
- 请求正文必须包含
Task
实体。
# Set JWT, PROJECT_ID, and TASK_ID in the local environment
curl -X PATCH "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks/${TASK_ID}?updateMask=taskOutcome,taskOutcomeTime,taskOutcomeLocation" \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${JWT}" \
--data-binary @- << EOM
{
"taskOutcome": "SUCCEEDED",
"taskOutcomeTime": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"taskOutcomeLocation": {
"point": {
"latitude": -6.195139,
"longitude": 106.820826
}
}
}
EOM
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis document explains how to finalize shipment tasks in Fleet Engine by closing them and setting their outcome to \u003ccode\u003eSUCCEEDED\u003c/code\u003e or \u003ccode\u003eFAILED\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eClosing a task indicates it is no longer in progress, while setting the outcome reflects the delivery result for billing and journey sharing.\u003c/p\u003e\n"],["\u003cp\u003eTasks can be closed by updating the vehicle's stop status, removing them from the vehicle's stop list, or setting their state to \u003ccode\u003eCLOSED\u003c/code\u003e if unassigned.\u003c/p\u003e\n"],["\u003cp\u003eThe task outcome, location, and time can be set using the \u003ccode\u003eUpdateTask\u003c/code\u003e method with appropriate fields and values, but the outcome cannot be changed once set.\u003c/p\u003e\n"]]],["To finalize shipment tasks, you must first close them, setting the `state` to `CLOSED`, which indicates the task is no longer active. Closing can be done by updating the vehicle's stop status, removing the task from the vehicle's stop list, or directly setting the task state. Once closed, set the `taskOutcome` to `SUCCEEDED` or `FAILED`, with optional details like `taskOutcomeLocation` and `taskOutcomeTime`. The task outcome is essential for journey sharing and billing. You can use gRPC or HTTP REST calls to accomplish both.\n"],null,["This document assumes you understand how to create and use tasks. It provides\nspecific examples for how to finalize shipment tasks as follows:\n\n- **Close a task** : Closing a shipment task changes its state to `CLOSED` and\n indicates that that task is no longer active.\n\n- **Set the task outcome** : Once a task is closed, you then finalize it by\n setting its outcome to either `SUCCEEDED` or `FAILED`. This is an important\n part of finalizing a task in order to show the delivery outcome in journey\n sharing and to ensure correct billing for the Fleet Engine service.\n\nClose a task\n\nYou can close a task in the following ways:\n\n- **Update the stop status for the vehicle** . You remove the stop from the vehicle, which in turn closes all tasks associated with the stop. See [Update stop status for details](/maps/documentation/mobility/fleet-engine/journeys/tasks/update-stops).\n- **Remove the task from the list of vehicle stops** . This involves updating the list of tasks for the stop, but with the closed task no longer part of the list. See Update task order in [Update tasks](/maps/documentation/mobility/fleet-engine/journeys/tasks/update-tasks).\n- **Set the task state to `CLOSED`**. This can only be done on tasks not assigned to vehicles. This section shows this approach.\n\nOnce you close a task, you may not reopen it.\n\u003e \u003e **Closing of a task does not indicate its success or failure** . It indicates\n\u003e \u003e that the task is no longer considered in progress. In order to indicate the\n\u003e \u003e actual outcome of a task and to have that displayed for Fleet Tracking and\n\u003e \u003e journey sharing purposes, you must indicate the actual outcome of a task. See\n\u003e \u003e [Set the task outcome](#set-task-outcome) below.\n\nTask fields for closing tasks\n\nThis section documents the required fields to set when closing a\ntask. Fleet engine ignores all other fields in the entity for the update.\n\n\u003cbr /\u003e\n\n| Required field | Value |\n|----------------|----------------|\n| `state` | `State.CLOSED` |\n\n\u003cbr /\u003e\n\nClose a task directly\n\nThe following examples show how to set an unassigned task to a closed state,\neither in gRPC or using an HTTP REST request call to `UpdateTask` \n\ngRPC \n\n static final String PROJECT_ID = \"my-delivery-co-gcp-project\";\n static final String TASK_ID = \"task-8241890\";\n\n DeliveryServiceBlockingStub deliveryService =\n DeliveryServiceGrpc.newBlockingStub(channel);\n\n // Task settings\n String taskName = \"providers/\" + PROJECT_ID + \"/tasks/\" + TASK_ID;\n Task task = Task.newBuilder()\n .setName(taskName)\n .setState(Task.State.CLOSED) // You can only directly CLOSE a\n .build(); // task that is NOT assigned to a vehicle.\n\n // Task request\n UpdateTaskRequest updateTaskRequest =\n UpdateTaskRequest.newBuilder() // No need for the header\n .setTask(task)\n .setUpdateMask(FieldMask.newBuilder().addPaths(\"state\"))\n .build();\n\n try {\n Task updatedTask = deliveryService.updateTask(updateTaskRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case NOT_FOUND:\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nREST\n\n`PATCH https://fleetengine.googleapis.com/v1/providers/\u003cproject_id\u003e/tasks/\u003cid\u003e?updateMask=state`\n\n- *\\\u003cid\\\u003e* is a unique identifier for the task.\n- The request header must contain a field *Authorization* with the value *Bearer \\\u003ctoken\\\u003e* , where *\\\u003ctoken\\\u003e* is issued by your server according to the guidelines described in [Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts) and [JSON Web tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/jwt).\n- You must include a `Task` entity in the request body\n\nExample `curl` command: \n\n # Set JWT, PROJECT_ID, and TASK_ID in the local environment\n curl -X PATCH \"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks/${TASK_ID}?updateMask=state,taskOutcome,taskOutcomeTime\" \\\n -H \"Content-type: application/json\" \\\n -H \"Authorization: Bearer ${JWT}\" \\\n --data-binary @- \u003c\u003c EOM\n {\n \"state\": \"CLOSED\",\n \"taskOutcome\": \"SUCCEEDED\",\n \"taskOutcomeTime\": \"$(date -u +\"%Y-%m-%dT%H:%M:%SZ\")\"\n }\n EOM\n\nSet the task outcome\n\nTo indicate the actual outcome of a task, you set the outcome for closed tasks\nto either `SUCCEEDED` or `FAILED`. A task must be closed before you set its\noutcome. Fleet Engine only charges for delivery tasks with a state of\n`SUCCEEDED`.\n\nTask outcome details\n\nTasks also provide additional details about the task outcome. You can set these\ndirectly and Fleet Engine respects your settings:\n\n- **Task outcome location**: Fleet Engine automatically fills in the task outcome location with the last known vehicle location. You can provide this instead if you prefer.\n- **Task outcome time**: Fleet Engine does not fill this field in, but is available for you to set.\n\nYou can use any of the following approaches to setting `task_outcome_location`\nand `task_outcome_time`:\n\n- **Update them in the same request** that sets the task outcome.\n- **Update them later**, after you have set task outcome.\n- **Modify them again** after they have been set.\n\nFleet Engine prevents the following updates related to task outcomes:\n\n- **You can't modify a task outcome once it is set** to either `SUCCEEDED` or `FAILED`.\n- **You can't set a task outcome location or outcome time** for tasks without a set outcome.\n\nTask fields for setting outcome\n\nThis section documents the required and optional fields to set when setting a\ntask outcome. Fleet Engine ignores other fields in the entity for the update.\n\n\u003cbr /\u003e\n\n| Required field | Value |\n|----------------|-----------------------------------------|\n| `taskOutcome` | `Outcome.SUCCEEDED` or `Outcome.FAILED` |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Optional field | Value |\n|-----------------------|-----------------------------------------------------------------------------------------------------------------|\n| `taskOutcomeLocation` | The location where the task was completed. If not set, Fleet Engine defaults this to the last vehicle location. |\n| `taskOutcomeTime` | The timestamp when the task was completed. |\n\n\u003cbr /\u003e\n\nTask outcome examples\n\nThe following example shows how to use the [Java gRPC library](/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks) and an HTTP\nREST call to `UpdateTask` to set a task outcome to `SUCCEEDED` and set the\nlocation where the task was completed. \n\ngRPC \n\n static final String PROJECT_ID = \"my-delivery-co-gcp-project\";\n static final String TASK_ID = \"task-8241890\";\n\n DeliveryServiceBlockingStub deliveryService =\n DeliveryServiceGrpc.newBlockingStub(channel);\n\n // Task settings\n String taskName = \"providers/\" + PROJECT_ID + \"/tasks/\" + TASK_ID;\n Task task = Task.newBuilder()\n .setName(taskName)\n .setTaskOutcome(TaskOutcome.SUCCEEDED)\n .setTaskOutcomeTime(now())\n .setTaskOutcomeLocation( // Grand Indonesia East Mall\n LocationInfo.newBuilder().setPoint(\n LatLng.newBuilder().setLatitude(-6.195139).setLongitude(106.820826)))\n .build();\n\n // Task request\n UpdateTaskRequest updateTaskRequest =\n UpdateTaskRequest.newBuilder() // No need for the header\n .setTask(task)\n .setUpdateMask(FieldMask.newBuilder().addPaths(\"task_outcome\", \"task_outcome_time\", \"task_outcome_location\"))\n .build();\n\n try {\n Task updatedTask = deliveryService.updateTask(updateTaskRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case NOT_FOUND:\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nREST\n\n`PATCH https://fleetengine.googleapis.com/v1/providers/\u003cproject_id\u003e/tasks/\u003cid\u003e?updateMask=taskOutcome,taskOutcomeTime,taskOutcomeLocation`\n\n- *\\\u003cid\\\u003e* is a unique identifier for the task.\n- The request header must contain a field *Authorization* with the value *Bearer \\\u003ctoken\\\u003e* , where *\\\u003ctoken\\\u003e* is issued by your server according to the guidelines described in [Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts) and [JSON Web tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/jwt).\n- The request body must contain a `Task` entity.\n\n # Set JWT, PROJECT_ID, and TASK_ID in the local environment\n curl -X PATCH \"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/tasks/${TASK_ID}?updateMask=taskOutcome,taskOutcomeTime,taskOutcomeLocation\" \\\n -H \"Content-type: application/json\" \\\n -H \"Authorization: Bearer ${JWT}\" \\\n --data-binary @- \u003c\u003c EOM\n {\n \"taskOutcome\": \"SUCCEEDED\",\n \"taskOutcomeTime\": \"$(date -u +\"%Y-%m-%dT%H:%M:%SZ\")\",\n \"taskOutcomeLocation\": {\n \"point\": {\n \"latitude\": -6.195139,\n \"longitude\": 106.820826\n }\n }\n }\n EOM\n\nWhat's next\n\n- [Find tasks](/maps/documentation/mobility/fleet-engine/journeys/tasks/find-tasks)"]]