

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 示例：使用命令控制车辆转向模式 (AWS CLI)
<a name="remote-command-tutorial"></a>

**重要**  
目前对某些 AWS 物联网 FleetWise 功能的访问受到限制。有关更多信息，请参阅 [AWS AWS 物联网中的区域和功能可用性 FleetWise](fleetwise-regions.md)。

以下示例向您展示了如何使用命令功能 AWS CLI。此示例使用 AWS IoT FleetWise 车辆作为目标设备来展示如何发送命令来远程控制转向模式。

**Topics**
+ [车辆转向模式示例概述](#iot-remote-command-tutorial-overview)
+ [先决条件](#iot-remote-command-tutorial-prereq)
+ [用于使用远程命令的 IAM 政策](#remote-command-policy)
+ [运行 AWS IoT 命令 (AWS CLI)](#iot-remote-command-tutorial-run)
+ [清理](#remote-command-tutorial-clean)

## 车辆转向模式示例概述
<a name="iot-remote-command-tutorial-overview"></a>

在这个例子中，你将：

1. 使用来更改车辆转向模式的操作创建命令资源。`create-command` AWS CLI 

1. 使用检索有关命令的信息，例如命令的创建时间或上次更新的时间`get-command` AWS CLI。

1. 使用以转向模式为必填参数`start-command-execution` AWS CLI 的向车辆发送命令，然后将在设备上执行该命令。

1. 使用获取命令执行的结果`get-command-execution` AWS CLI。您可以检查执行何时完成，并检索其他详细信息，例如执行结果和完成执行命令所花费的时间。

1. 通过删除您不想再使用的命令和命令执行来执行清理活动。

## 先决条件
<a name="iot-remote-command-tutorial-prereq"></a>

在运行此示例之前：
+ 将您的 AWS IoT FleetWise 车辆作为 AWS IoT 注册表中的 AWS IoT 一件事进行配置。您还必须向您的事物添加证书并激活它，并将策略附加到您的事物上。然后，您的设备可以连接到云端并执行命令。有关更多信息，请参阅[预配车辆](https://docs.aws.amazon.com/iot-fleetwise/latest/developerguide/provision-vehicles.html)。
+ 创建一个 IAM 用户和一个 IAM 策略，该策略授予您使用命令执行 API 操作的权限，如所示[用于使用远程命令的 IAM 政策](#remote-command-policy)。

## 用于使用远程命令的 IAM 政策
<a name="remote-command-policy"></a>

下表显示了一个示例 IAM 策略，该策略授予对命令功能的所有控制平面和数据平面 API 操作的访问权限。该应用程序的用户将有权执行所有远程命令 API 操作，如表所示。


**API 操作**  

| API 操作 | 控制/数据平面 | 协议 | 说明 | 资源 | 
| --- | --- | --- | --- | --- | 
| CreateCommand | 控制面板 | HTTP | 创建命令资源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommand | 控制面板 | HTTP | 检索命令的相关信息 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommand | 控制面板 | HTTP | 更新有关命令的信息或将其弃用 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommands | 控制面板 | HTTP | 列出您账户中的命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommand | 控制面板 | HTTP | 删除命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| StartCommandExecution | 数据面板 | HTTP | 开始执行命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommandExecution | 数据面板 | MQTT | 更新命令执行 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommandExecution | 控制面板 | HTTP | 检索有关命令执行的信息 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommandExecutions | 控制面板 | HTTP | 列出您账户中的命令执行情况 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommandExecution | 控制面板 | HTTP | 删除命令执行 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 

在此示例中：
+ `us-east-1`和你的 AWS 区域，比如`ap-south-1`。
+ 将 `111122223333` 替换成您的 AWS 账户 编号，例如 `57EXAMPLE833`。
+ `command-id``command-id1`、，并`command-id2`使用您的唯一命令标识符，例如`LockDoor`或`TurnOffAC`。
+ `thing-name`用你的 AWS IoT 事物名称，比如`my_car`。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": [
                "iot:CreateCommand",
                "iot:GetCommand",
                "iot:ListCommands",
                "iot:UpdateCommand",
                "iot:DeleteCommand"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id1",
                "arn:aws:iot:us-east-1:111122223333:command/command-id2"
            ]
        },
        {
            "Action": [
                "iot:GetCommandExecution",
                "iot:ListCommandExecutions",
                "iot:DeleteCommandExecution"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id",
                "arn:aws:iot:us-east-1:111122223333:thing/thing-name"
            ]
        },
        {
            "Action": "iot:StartCommandExecution",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id",
                "arn:aws:iot:us-east-1:111122223333:thing/thing-name"
            ]
        }
    ]
}
```

------

## 运行 AWS IoT 命令 (AWS CLI)
<a name="iot-remote-command-tutorial-run"></a>

下文显示了如何使用 AWS CLI 来执行命令操作和更改车辆转向模式。

1. 

**为转向模式操作创建命令资源**

   使用 CLI 创建要发送到设备的`create-command`命令。在此示例中，指定：
   + `command-id` 用作 *`TurnOffSteeringMode`*
   + `role-arn`a `"arn:aws:iam:accountId:role/FwCommandExecutionRole"` s `role-arn` 必须提供，因为授予在车辆上创建和运行命令的权限是 IAM 角色。有关更多信息，请参阅 [授 AWS IoT Device Management 予使用以下命令生成有效负载的权限 AWS IoT FleetWise](controlling-access.md#generate-command-payload)。
   + `display-name`作为 “*`Turn off steering mode`*”
   + `namespace`必须是 `AWS-IoT-FleetWise`
   + `mandatory-parameters`作为名称-值对，以 “” 和 “defaultValu *\$1actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode* e `name` 为” `{ "S": "true" }`
**注意**  
您也可以在不指定任何必需参数的情况下创建命令。然后，您必须指定使用 `start-command-execution` CLI 执行命令时要使用的参数。有关示例，请参阅[命令使用场景](remote-command-use-cases.md)。
**重要**  
使用`AWS-IoT-FleetWise`命名空间时，必须确保作为命名空间一部分指定的`Name`字段`mandatory-parameters`使用`$actuatorPath.`前缀，并且该`Value`字段必须使用字符串数据类型。

   ```
   aws iot create-command \ 
       --command-id TurnOffSteeringMode \ 
       --role-arn "arn:aws:iam:accountId:role/FwCommandExecutionRole" \
       --display-name "Turn off steering mode" \ 
       --namespace AWS-IoT-FleetWise \
       --mandatory-parameters '[
         {
           "name": "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode",
           "defaultValue": { "S": "true" }
         }
       ]'
   ```

   以下输出显示了 CLI 的示例响应，其中`ap-south-1`和`123456789012`是 AWS 区域 和 AWS 账户 ID 的示例。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode"
   }
   ```

   有关使用此命令的其他示例，请参见[创建命令资源](create-manage-remote-command-cli.md#create-remote-command-cli)。

1. 

**检索有关命令的信息**

   运行以下命令以检索有关该命令的信息，其中`command-id`是上面`create-command`操作输出中的命令 ID。
**注意**  
如果您创建了多个命令，则可以使用 `ListCommands` API 列出账户中的所有命令，然后使用 `GetCommand` API 获取有关特定命令的更多信息。有关更多信息，请参阅 [在你的账户中列出命令](create-manage-remote-command-cli.md#list-remote-command-cli)。

   ```
   aws iot get-command --command-id TurnOffSteeringMode
   ```

   运行此命令会生成以下响应。您将看到命令的创建时间和上次更新时间、您指定的任何参数以及该命令是否可以在设备上运行。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode",
       "namespace": "AWS-IoT-FleetWise",
       "mandatoryParameters":[
           {
               "name": "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode",
               "defaultValue": {"S": "true" }
           }
       ],
       "createdAt": "2024-03-23T00:50:10.095000-07:00",
       "lastUpdatedAt": "2024-03-23T00:50:10.095000-07:00",
       "deprecated": false
   }
   ```

   有关使用此命令的其他示例，请参见[检索关于命令的信息](create-manage-remote-command-cli.md#get-remote-command-cli)。

1. 

**开始执行命令**

   运行以下命令开始执行该命令，其中`command-arn`是上面`get-command`操作输出中的命令 ARN。`target-arn`是您正在为其执行命令的目标设备的 ARN，例如。*`myVehicle`*

   在此示例中，由于您在创建命令时提供了参数的默认值，因此 `start-command-execution` CLI 可以在执行命令时使用这些值。在使用 CLI 时，您也可以通过为参数指定不同的值来选择覆盖默认值。

   ```
   aws iot-data start-command-execution \    
       --command-arn arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode \
       --target-arn arn:aws:iot:ap-south-1:123456789012:thing/myVehicle
   ```

   运行此命令会返回一个命令执行 ID。您可以使用此 ID 查询命令执行状态、详细信息和命令执行历史记录。

   ```
   {
       "executionId": "07e4b780-7eca-4ffd-b772-b76358da5542"
   }
   ```

   有关使用 CLI 的其他示例，请参阅[发送命令 (AWS CLI)](send-monitor-remote-command-cli.md#send-remote-command-cli)。

1. 

**检索有关命令执行的信息**

   运行以下命令以检索有关您在目标设备上执行的命令的信息。指定作为上面`start-command-execution`操作输出获得的`target-arn`，以及作为目标设备的 ARN。`execution-id`
**注意**  
要获取最新的状态信息，您的设备必须已将更新的状态信息发布到使用 MQTT API 的命令的 M `UpdateCommandExecution` QTT 预留响应主题。有关更多信息，请参阅 [更新命令执行结果](send-monitor-remote-command-cli.md#update-remote-command-execution-cli)。
如果您开始执行多个命令，则可以使用 `ListCommandExecutions` API 列出账户中的所有命令执行，然后使用 `GetCommandExecution` API 获取有关特定执行的更多信息。有关更多信息，请参阅 [列出您账户中的命令执行情况](send-monitor-remote-command-cli.md#list-remote-command-execution-cli)。

   ```
   aws iot get-command-execution \    
       --execution-id <"07e4b780-7eca-4ffd-b772-b76358da5542"> \ 
       --target-arn arn:aws:iot:us-east-1:<account>:thing/myVehicle
   ```

   运行此命令会返回有关命令执行、执行状态、开始执行的时间和完成时间的信息。例如，以下响应显示在目标设备上成功执行命令并且转向模式已关闭。

   ```
   {
       "executionId": "07e4b780-7eca-4ffd-b772-b76358da5542",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode",
       "targetArn": "arn:aws:iot:ap-south-1:123456789012:thing/myVehicle",
       "result": "SUCCEEDED",
        "statusReason": {
           "reasonCode": "65536",
           "reasonDescription": "SUCCESS"
       },
       "result": {
           "KeyName": {
               "S": "",
               "B": true,
               "BIN": null
           }
       },
       "createdAt": "2024-03-23T00:50:10.095000-07:00",
       "completedAt": "2024-03-23T00:50:10.095000-07:00",
       "parameters": '{
            "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode":
            { "S": "true" }
       }' 
   }
   ```

## 清理
<a name="remote-command-tutorial-clean"></a>

现在，您已经创建了一个命令并在您的设备上执行了该命令，如果您不再打算使用此命令，则可以将其删除。任何正在执行的待执行命令将继续运行，不会受到删除请求的影响。

**注意**  
或者，如果命令已过时，您也可以将其弃用，以后可能需要使用它才能在目标设备上运行。

1. 

**（可选）弃用命令资源**

   运行以下命令以弃用该命令，其中`command-id`是上面`get-command`操作输出中的命令 ID。

   ```
   aws iot update-command \    
      --command-id TurnOffSteeringMode \    
      --deprecated
   ```

   运行此命令会返回一个输出，显示该命令已被弃用。您也可以使用 CLI 恢复命令。
**注意**  
您也可以使用 `update-command` CLI 更新命令的显示名称和描述。有关更多信息，请参阅 [更新或弃用命令资源](create-manage-remote-command-cli.md#update-remote-command-cli)。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "deprecated": true,
       "lastUpdatedAt": "2024-05-09T23:16:51.370000-07:00"
   }
   ```

1. 

**删除该命令**

   运行以下命令删除由指定的命令`command-id`。
**注意**  
删除操作是永久性的，无法撤消。

   ```
   aws iot delete-command --command-id TurnOffSteeringMode
   ```

   如果删除请求成功，您将看到 202 或 204 的 HTTP`statusCode`，具体取决于您是否将该命令标记为已弃用以及何时被弃用。有关更多信息以及示例，请参阅 [删除命令资源](create-manage-remote-command-cli.md#delete-remote-command-cli)。

   您可以使用 `get-command` CLI 来验证该命令是否已从您的账户中删除。

1. 

**（可选）删除命令执行**

   默认情况下，所有命令执行将在您创建命令之日起六个月内删除。您可以使用 `GetCommandExecution` API 中的`timeToLive`参数查看此信息。

   或者，如果您的命令执行已变为终止，例如当您的执行状态为`SUCCEEDED``FAILED`、或之一时`REJECTED`，则可以删除该命令的执行。运行以下命令删除执行，其中`execution-id`是上面`get-command-execution`操作输出中的执行 ID。

   ```
   aws iot delete-command-execution \ 
               --execution-id "07e4b780-7eca-4ffd-b772-b76358da5542"
   ```

   您可以使用 `get-command-execution` CLI 验证命令执行是否已从您的账户中删除。