

AWS IoT FleetWise 不再向新客戶開放。現有的 AWS IoT FleetWise 客戶可以繼續使用 服務。[上的連線行動性指南 AWS](https://aws.amazon.com/solutions/guidance/connected-mobility-on-aws/)提供如何為連線行動解決方案開發和部署模組化服務的指引，這些解決方案可用於實現與 AWS IoT FleetWise 同等的功能。

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

# 教學課程：使用自訂解碼界面設定網路無關的資料收集
<a name="network-agnostic-data-collection"></a>

**重要**  
對特定 AWS IoT FleetWise 功能的存取目前受到鎖定。如需詳細資訊，請參閱[AWS AWS IoT FleetWise 中的區域和功能可用性](fleetwise-regions.md)。

## 簡介
<a name="network-agnostic-data-collection-intro"></a>

本教學課程概述如何設定 AWS IoT FleetWise，以使用利用自訂解碼界面的網路無關資料收集來收集資料和執行命令。透過網路無關的資料收集，您可以使用自己的方法來解碼訊號，然後再將訊號傳送到指定的資料目的地。這可節省時間，因為您不需要特別為 AWS IoT FleetWise 建立訊號解碼器。您可以使用自己的實作來解碼訊號子集，也可以在建立或更新解碼器資訊清單`defaultForUnmappedSignals`時使用。這也可讓您靈活地收集車輛中各種來源的訊號和觸發條件。

 本教學課程適用於不在標準控制器區域網路 (CAN 匯流排） 界面上的車輛訊號。例如，以自訂車輛格式或配置編碼的資料。

## 環境設定
<a name="network-agnostic-data-collection-setup"></a>

本教學課程假設您已完成設定環境以存取 AWS IoT FleetWise 雲端，以及 Edge 實作 APIs和程式碼庫的步驟。

## 資料模型
<a name="network-agnostic-data-models"></a>

下一節說明如何使用自訂解碼界面建立車輛屬性的模型。這適用於資料收集以及命令使用案例。它也適用於車輛中使用的任何基礎資料來源建模，例如 IDLs。

在此範例中，有兩種車輛屬性：要收集的車輛感應器 （目前車輛位置） 和要遠端控制的車輛傳動器 （冷氣器）。這兩個都在此結構描述中定義：

```
// Vehicle WGS84 Coordinates
double Latitude;
double Longitude;

// Vehicle AC 
Boolean ActivateAC;
```

下一個步驟是使用自訂解碼介面 APIs 將這些定義匯入 AWS IoT FleetWise。

### 訊號目錄更新
<a name="network-agnostic-signal-catalog-updates"></a>

在訊號目錄中匯入這些定義。如果您已有 AWS IoT FleetWise 中的訊號目錄，請直接使用更新 API。如果您沒有訊號目錄，請先建立訊號目錄，然後呼叫更新 API。

首先，您必須建立這些車輛訊號的 VSS 表示法。VSS 做為分類來代表 AWS IoT FleetWise 中的車輛資料。使用下列內容建立名為 'vehicle-signals.json' 的 json 檔案：

```
// vehicle-signals.json
// Verify that branches and nodes are unique in terms of fully qualified name
// in the signal catalog.
[
 {
    "branch": {
      "fullyQualifiedName": "Vehicle",
      "description": "Vehicle Branch"
    }
  },
  {
    "branch": {
      "fullyQualifiedName": "Vehicle.CurrentLocation",
      "description": "CurrentLocation"
    }
  },
  {
    "sensor": {
      "dataType": "DOUBLE",
      "fullyQualifiedName": "Vehicle.CurrentLocation.Latitude",
      "description": "Latitude"
    }
  },
  {
    "sensor": {
      "dataType": "DOUBLE",
      "fullyQualifiedName": "Vehicle.CurrentLocation.Longitude",
      "description": "Longitude"
    }
  },
  {
    "actuator": {
      "fullyQualifiedName": "Vehicle.ActivateAC",
      "description": "AC Controller",
      "dataType": "BOOLEAN"
    }
  }
]
```

如果您沒有訊號目錄，則需要叫用 `create-signal-catalog`：

```
VEHICLE_NODES=`cat vehicle-signals.json`
aws iotfleetwise create-signal-catalog \ 
        --name my-signal-catalog \
        --nodes "${VEHICLE_NODES}"
```

如果您已經有訊號目錄，您可以使用 `update-signal-catalog` API 新增這些訊號：

```
VEHICLE_NODES=`cat vehicle-signals.json`
aws iotfleetwise update-signal-catalog \
        --name my-signal-catalog \
        --nodes-to-add "${VEHICLE_NODES}"
```

### 車輛模型和解碼器
<a name="network-agnostic-vehicle-model-decoder"></a>

在訊號目錄中插入訊號後，下一個步驟是建立車輛模型並執行個體化這些訊號。為此，您可以使用 `create-model-manifest`和 `create-decoder-manifest` APIs。

首先，格式化您要插入車輛模型的訊號名稱：

```
# Prepare the signals for insertion into the vehicle model.
VEHICLE_NODES=`cat vehicle-signals.json` 
VEHICLE_NODES=`echo ${VEHICLE_NODES} | jq -r ".[] | .actuator,.sensor | .fullyQualifiedName" | grep Vehicle\\.`
VEHICLE_NODES=`echo "${VEHICLE_NODES}" | jq -Rn [inputs]`
# This is how the vehicle model input looks. 
echo $VEHICLE_NODES
# [ "Vehicle.CurrentLocation.Latitude",
#   "Vehicle.CurrentLocation.Longitude",
#   "Vehicle.ActivateAC" ]
# Create the vehicle model with those signals.
aws iotfleetwise create-model-manifest \
    --name {{my-model-manifest}} \
    --signal-catalog-arn arn:xxxx:signal-catalog/my-signal-catalog \
    --nodes "${VEHICLE_NODES}"  
 
# Activate the vehicle model. 
 aws iotfleetwise update-model-manifest \
    --name my-model-manifest --status ACTIVE
```

現在，使用自訂解碼界面來建立解碼器資訊清單。

**注意**  
只有在您想要指定自訂 IDs 時才需要建立網路介面和訊號，這不是此範例的一部分。  
如需在完整名稱 (FQN) 與自訂解碼訊號 ID 不同時映射解碼資訊的資訊，請參閱 [https://github.com/aws/aws-iot-fleetwise-edge/blob/main/docs/dev-guide/network-agnostic-dev-guide.md#aaos-vhal](https://github.com/aws/aws-iot-fleetwise-edge/blob/main/docs/dev-guide/network-agnostic-dev-guide.md#aaos-vhal)。

```
// Create a network interface that is of type : CUSTOM_DECODING_INTERFACE
// custom-interface.json
[
  {
    "interfaceId": "NAMED_SIGNAL",
    "type": "CUSTOM_DECODING_INTERFACE",
    "customDecodingInterface": {
      "name": "NamedSignalInterface"
    }
  },
  {
    "interfaceId": "AC_ACTUATORS",
    "type": "CUSTOM_DECODING_INTERFACE",
    "customDecodingInterface": {
      "name": "NamedSignalInterface"
    }
  }
]
// custom-decoders.json
// Refer to the fully qualified names of the signals, make them of 
// type CUSTOM_DECODING_SIGNAL, and specify them as part of the same interface ID
// that was defined above.
[
    {
      "fullyQualifiedName": "Vehicle.CurrentLocation.Longitude",
      "interfaceId": "NAMED_SIGNAL",
      "type": "CUSTOM_DECODING_SIGNAL",
      "customDecodingSignal": {
        "id": "Vehicle.CurrentLocation.Longitude"
      }
    },
    {
      "fullyQualifiedName": "Vehicle.CurrentLocation.Latitude",
      "interfaceId": "NAMED_SIGNAL",
      "type": "CUSTOM_DECODING_SIGNAL",
      "customDecodingSignal": {
        "id": "Vehicle.CurrentLocation.Latitude"
      }
    },
    {
        "fullyQualifiedName": "Vehicle.ActivateAC",
        "interfaceId": "AC_ACTUATORS",
        "type": "CUSTOM_DECODING_SIGNAL",
        "customDecodingSignal": {
          "id": "Vehicle.ActivateAC"
        }
    }
]
# Create the decoder manifest.
 CUSTOM_INTERFACE=`cat custom-interface.json`
 CUSTOM_DECODERS=`cat custom-decoders.json`

aws iotfleetwise create-decoder-manifest \
   --name my-decoder-manifest \
   --model-manifest-arn arn:xxx:model-manifest/my-model-manifest \
   --network-interfaces "${CUSTOM_INTERFACE}" \
   --signal-decoders "${CUSTOM_DECODERS}"

 # Activate the decoder manifest.
 aws iotfleetwise update-decoder-manifest \
    --name my-decoder-manifest \
    --status ACTIVE
```

此時，您已在 AWS IoT FleetWise 中完全建立這些訊號的模型。接著建立車輛，並將其與您建立的模型建立關聯。您可以針對以下項目使用 `create-vehicle` API：

```
aws iotfleetwise create-vehicle \
    --decoder-manifest-arn arn:xxx:decoder-manifest/my-decoder-manifest \
    --association-behavior ValidateIotThingExists \
    --model-manifest-arn arn:xxx:model-manifest/my-model-manifest \
    --vehicle-name "my-vehicle"
```

下一個步驟是專注於 AWS IoT FleetWise Edge 程式碼庫，並撰寫必要的程式碼延伸。

**注意**  
如需 Edge 實作的相關資訊，請參閱 [https://github.com/aws/aws-iot-fleetwise-edge/blob/main/docs/dev-guide/network-agnostic-dev-guide.md#implementing-your-own-sensors-and-actuators](https://github.com/aws/aws-iot-fleetwise-edge/blob/main/docs/dev-guide/network-agnostic-dev-guide.md#implementing-your-own-sensors-and-actuators)。

## 傳送命令
<a name="test-command"></a>

現在，編譯軟體 （確定將標頭和 C\+\+ 檔案新增至 CMake 檔案），然後返回雲端 APIs 來測試此啟動器的命令：

```
// Create a command targeting your vehicle.
aws iot create-command --command-id activateAC \
    --namespace "AWS-IoT-Fleetwise" \
    --endpoint-url endpoint-url \ 
    --role-arn ${SERVICE_ROLE_ARN} \
    --mandatory-parameters '[ { "name": "$actuatorPath.Vehicle.ActivateAC", "defaultValue": {"B": "false"} } ]' \
// You will receive the command ARN. 

{
    "commandId": "activateAC",
    "commandArn": "arn:aws:iot:xxx:command/activateAC"
}

// You can send the command to activate the AC targeting your vehicle. 

JOBS_ENDPOINT_URL=`aws iot describe-endpoint --endpoint-type iot:Jobs | jq -j .endpointAddress`
aws iot-jobs-data start-command-execution \
    --command-arn arn:aws:iot:xxx:command/activateAC \
    --target-arn arn:xxx:vehicle/my-vehicle \
    --parameters '{ "$actuatorPath.Vehicle.ActivateAC" : {"B": "true"}}' \
    --endpoint-url https://${JOBS_ENDPOINT_URL}
// You will receive the corresponding execution ID.
{
    "executionId": "01HSK4ZH6ME7D43RB2BV8JC51D"
}

// If you have the AWS IoT FleetWise Edge Agent running, you can see the logs.
[AcCommandDispatcher.cpp:26] [setActuatorValue()]:
[Actuator Vehicle.ActivateAC executed successfully for command ID 01HSK4ZH6ME7D43RB2BV8JC51D]
```