

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例：使用命令控制車輛轉向模式 (AWS CLI)
<a name="remote-command-tutorial"></a>

**重要**  
對特定 AWS IoT FleetWise 功能的存取目前受到鎖定。如需詳細資訊，請參閱[AWS AWS IoT 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 動作 | 控制/資料平面 | 通訊協定 | Description | 資源 | 
| --- | --- | --- | --- | --- | 
| CreateCommand | 控制平面 | HTTP | 建立命令資源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommand | 控制平面 | HTTP | 擷取命令的相關資訊 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommand | 控制平面 | HTTP | 更新命令或 的相關資訊以取代命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommands | 控制平面 | HTTP | 列出您帳戶中的命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommand | 控制平面 | HTTP | 刪除命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| StartCommandExecution | 資料平面 | HTTP | 開始執行命令 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommandExecution | 資料平面 | MQTT | 更新命令執行 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommandExecution | 控制平面 | HTTP | 擷取命令執行的相關資訊 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommandExecutions | 控制平面 | HTTP | 列出您帳戶中的命令執行 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommandExecution | 控制平面 | HTTP | 刪除命令執行 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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. 

**為轉向模式操作建立命令資源**

   使用 `create-command`CLI 建立您要傳送到裝置的命令。在此範例中，指定：
   + `command-id` 作為 *`TurnOffSteeringMode`*
   + `role-arn` 因為`role-arn`必須提供 `"arn:aws:iam:accountId:role/FwCommandExecutionRole"` ，因為它是 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` 作為名稱值對，其中 "*\$1actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode*" `name`和 defaultValue 作為 `{ "S": "true" }`
**注意**  
您也可以建立命令，而無需指定任何必要參數。然後，您必須指定使用 CLI `start-command-execution` 執行命令時要使用的參數。如需範例，請參閱 [命令使用案例](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. 

**擷取命令執行的相關資訊**

   執行下列命令，以擷取您在目標裝置上執行之命令的相關資訊。指定`execution-id`您從上方取得做為`start-command-execution`操作輸出的 ，以及 `target-arn`，這是您要鎖定之裝置的 ARN。
**注意**  
若要取得最新的狀態資訊，您的裝置必須使用 MQTT API 將更新的狀態資訊發佈至命令的 `UpdateCommandExecution` MQTT 預留回應主題。如需詳細資訊，請參閱[更新命令執行結果](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
   ```

   如果刪除請求成功，您會看到 HTTP `statusCode`為 202 或 204，這取決於您是否將命令標記為棄用，以及何時棄用。如需詳細資訊和範例，請參閱 [刪除命令資源](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 來驗證命令執行是否已從您的帳戶中移除。