AWS IoT FleetWise 使用適用於 Kotlin 的 SDK 的範例 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

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

AWS IoT FleetWise 使用適用於 Kotlin 的 SDK 的範例

下列程式碼範例示範如何使用適用於 Kotlin 的 AWS 開發套件來執行動作和實作常見案例 AWS IoT FleetWise。

基本概念是程式碼範例,這些範例說明如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 AWS IoT FleetWise。

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main() { listSignalCatalogs() } /** * Lists the AWS FleetWise Signal Catalogs associated with the current AWS account. */ suspend fun listSignalCatalogs() { val request = ListSignalCatalogsRequest { maxResults = 10 } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.listSignalCatalogs(request) val summaries = response.summaries if (summaries.isNullOrEmpty()) { println("No AWS FleetWise Signal Catalogs were found.") } else { summaries.forEach { summary -> with(summary) { println("Catalog Name: $name") println("ARN: $arn") println("Created: $creationTime") println("Last Modified: $lastModificationTime") println("---------------") } } } } }

基本概念

以下程式碼範例顯示做法:

  • 建立標準化訊號的集合。

  • 建立代表一組車輛的機群。

  • 建立模型資訊清單。

  • 建立解碼器資訊清單。

  • 檢查模型資訊清單的狀態。

  • 檢查解碼器的狀態。

  • 建立 IoT 物件。

  • 建立 車輛。

  • 顯示車輛詳細資訊。

  • 刪除 AWS IoT FleetWise 資產。

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

執行示範 AWS IoT SiteWise 功能的互動式案例。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ var scanner = Scanner(System.`in`) val DASHES = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <signalCatalogName> <manifestName> <fleetId> <vecName> <decName> Where: signalCatalogName - The name of the Signal Catalog to create (eg, catalog30). manifestName - The name of the Vehicle Model (Model Manifest) to create (eg, manifest30). fleetId - The ID of the Fleet to create (eg, fleet30). vecName - The name of the Vehicle to create (eg, vehicle30). decName - The name of the Decoder Manifest to create (eg, decManifest30). """.trimIndent() if (args.size != 5) { println(usage) return } val signalCatalogName = args[0] val manifestName = args[1] val fleetId = args[2] val vecName = args[3] val decName = args[4] println( """ AWS IoT FleetWise is a managed service that simplifies the process of collecting, organizing, and transmitting vehicle data to the cloud in near real-time. Designed for automakers and fleet operators, it allows you to define vehicle models, specify the exact data you want to collect (such as engine temperature, speed, or battery status), and send this data to AWS for analysis. By using intelligent data collection techniques, IoT FleetWise reduces the volume of data transmitted by filtering and transforming it at the edge, helping to minimize bandwidth usage and costs. At its core, AWS IoT FleetWise helps organizations build scalable systems for vehicle data management and analytics, supporting a wide variety of vehicles and sensor configurations. You can define signal catalogs and decoder manifests that describe how raw CAN bus signals are translated into readable data, making the platform highly flexible and extensible. This allows manufacturers to optimize vehicle performance, improve safety, and reduce maintenance costs by gaining real-time visibility into fleet operations. """.trimIndent(), ) waitForInputToContinue(scanner) println(DASHES) runScenario(signalCatalogName, fleetId, manifestName, decName, vecName) } suspend fun runScenario(signalCatalogName: String, fleetIdVal: String, manifestName: String, decName: String, vecName: String) { println(DASHES) println("1. Creates a collection of standardized signals that can be reused to create vehicle models") waitForInputToContinue(scanner) val signalCatalogArn = createbranchVehicle(signalCatalogName) println("The collection ARN is $signalCatalogArn") waitForInputToContinue(scanner) println(DASHES) println(DASHES) println("2. Create a fleet that represents a group of vehicles") println( """ Creating an IoT FleetWise fleet allows you to efficiently collect, organize, and transfer vehicle data to the cloud, enabling real-time insights into vehicle performance and health. It helps reduce data costs by allowing you to filter and prioritize only the most relevant vehicle signals, supporting advanced analytics and predictive maintenance use cases. """.trimIndent(), ) waitForInputToContinue(scanner) val fleetid = createFleet(signalCatalogArn, fleetIdVal) println("The fleet Id is $fleetid") waitForInputToContinue(scanner) val nodeList = listSignalCatalogNode(signalCatalogName) println(DASHES) println(DASHES) println("3. Create a model manifest") println( """ An AWS IoT FleetWise manifest defines the structure and relationships of vehicle data. The model manifest specifies which signals to collect and how they relate to vehicle systems, while the decoder manifest defines how to decode raw vehicle data into meaningful signals. """.trimIndent(), ) waitForInputToContinue(scanner) val nodes = listSignalCatalogNode(signalCatalogName) val manifestArn = nodes?.let { createModelManifest(manifestName, signalCatalogArn, it) } println("The manifest ARN is $manifestArn") println(DASHES) println(DASHES) println("4. Create a decoder manifest") println( """ A decoder manifest in AWS IoT FleetWise defines how raw vehicle data (such as CAN signals) should be interpreted and decoded into meaningful signals. It acts as a translation layer that maps vehicle-specific protocols to standardized data formats using decoding rules. This is crucial for extracting usable data from different vehicle models, even when their data formats vary. """.trimIndent(), ) waitForInputToContinue(scanner) val decArn = createDecoderManifest(decName, manifestArn) println("The decoder manifest ARN is $decArn") waitForInputToContinue(scanner) println(DASHES) println(DASHES) println("5. Check the status of the model manifest") println( """ The model manifest must be in an ACTIVE state before it can be used to create or update a vehicle. """.trimIndent(), ) waitForInputToContinue(scanner) updateModelManifest(manifestName) waitForModelManifestActive(manifestName) waitForInputToContinue(scanner) println(DASHES) println(DASHES) println("6. Check the status of the decoder") println( """ The decoder manifest must be in an ACTIVE state before it can be used to create or update a vehicle. """.trimIndent(), ) waitForInputToContinue(scanner) updateDecoderManifest(decName) waitForDecoderManifestActive(decName) waitForInputToContinue(scanner) println(DASHES) println(DASHES) println("7. Create an IoT Thing") println( """ AWS IoT FleetWise expects an existing AWS IoT Thing with the same name as the vehicle name you are passing to createVehicle method. Before calling createVehicle(), you must create an AWS IoT Thing with the same name using the AWS IoT Core service. """.trimIndent(), ) waitForInputToContinue(scanner) createThingIfNotExist(vecName) println(DASHES) println(DASHES) println("8. Create a vehicle") println( """ Creating a vehicle in AWS IoT FleetWise allows you to digitally represent and manage a physical vehicle within the AWS ecosystem. This enables efficient ingestion, transformation, and transmission of vehicle telemetry data to the cloud for analysis. """.trimIndent(), ) waitForInputToContinue(scanner) createVehicle(vecName, manifestArn, decArn) println(DASHES) println(DASHES) println("9. Display vehicle details") waitForInputToContinue(scanner) getVehicleDetails(vecName) waitForInputToContinue(scanner) println(DASHES) println(DASHES) println("10. Delete the AWS IoT Fleetwise Assets") println("Would you like to delete the IoT Fleetwise Assets? (y/n)") val delAns = scanner.nextLine().trim() if (delAns.equals("y", ignoreCase = true)) { deleteVehicle(vecName) deleteDecoderManifest(decName) deleteModelManifest(manifestName) deleteFleet(fleetid) deleteSignalCatalog(signalCatalogName) } println(DASHES) println( """ Thank you for checking out the AWS IoT Fleetwise Service Use demo. We hope you learned something new, or got some inspiration for your own apps today. For more AWS code examples, have a look at: https://docs.aws.amazon.com/code-library/latest/ug/what-is-code-library.html """.trimIndent(), ) println(DASHES) } suspend fun deleteVehicle(vecName: String) { val request = DeleteVehicleRequest { vehicleName = vecName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteVehicle(request) println("Vehicle $vecName was deleted successfully.") } } suspend fun getVehicleDetails(vehicleNameVal: String) { val request = GetVehicleRequest { vehicleName = vehicleNameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.getVehicle(request) val details = mapOf( "vehicleName" to response.vehicleName, "arn" to response.arn, "modelManifestArn" to response.modelManifestArn, "decoderManifestArn" to response.decoderManifestArn, "attributes" to response.attributes.toString(), "creationTime" to response.creationTime.toString(), "lastModificationTime" to response.lastModificationTime.toString(), ) println("Vehicle Details:") for ((key, value) in details) { println("• %-20s : %s".format(key, value)) } } } suspend fun createVehicle(vecName: String, manifestArn: String?, decArn: String) { val request = CreateVehicleRequest { vehicleName = vecName modelManifestArn = manifestArn decoderManifestArn = decArn } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.createVehicle(request) println("Vehicle $vecName was created successfully.") } } /** * Creates an IoT Thing if it does not already exist. * * @param vecName the name of the IoT Thing to create */ suspend fun createThingIfNotExist(vecName: String) { val request = CreateThingRequest { thingName = vecName } IotClient { region = "us-east-1" }.use { client -> client.createThing(request) println("The $vecName IoT Thing was successfully created") } } suspend fun updateDecoderManifest(nameVal: String) { val request = UpdateDecoderManifestRequest { name = nameVal status = ManifestStatus.Active } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.updateDecoderManifest(request) println("$nameVal was successfully updated") } } /** * Waits for the specified model manifest to become active. * * @param decNameVal the name of the model manifest to wait for */ suspend fun waitForDecoderManifestActive(decNameVal: String) { var elapsedSeconds = 0 var lastStatus: ManifestStatus = ManifestStatus.Draft print("⏳ Elapsed: 0s | Status: DRAFT") IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> while (true) { delay(1000) elapsedSeconds++ if (elapsedSeconds % 5 == 0) { val request = GetDecoderManifestRequest { name = decNameVal } val response = fleetwiseClient.getDecoderManifest(request) lastStatus = response.status ?: ManifestStatus.Draft when (lastStatus) { ManifestStatus.Active -> { print("\rElapsed: ${elapsedSeconds}s | Status: ACTIVE ✅\n") return } ManifestStatus.Invalid -> { print("\rElapsed: ${elapsedSeconds}s | Status: INVALID ❌\n") throw RuntimeException("Model manifest became INVALID. Cannot proceed.") } else -> { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } else { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } } /** * Waits for the specified model manifest to become active. * * @param manifestName the name of the model manifest to wait for */ suspend fun waitForModelManifestActive(manifestNameVal: String) { var elapsedSeconds = 0 var lastStatus: ManifestStatus = ManifestStatus.Draft print("⏳ Elapsed: 0s | Status: DRAFT") IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> while (true) { delay(1000) elapsedSeconds++ if (elapsedSeconds % 5 == 0) { val request = GetModelManifestRequest { name = manifestNameVal } val response = fleetwiseClient.getModelManifest(request) lastStatus = response.status ?: ManifestStatus.Draft when (lastStatus) { ManifestStatus.Active -> { print("\r Elapsed: ${elapsedSeconds}s | Status: ACTIVE ✅\n") return } ManifestStatus.Invalid -> { print("\r Elapsed: ${elapsedSeconds}s | Status: INVALID ❌\n") throw RuntimeException("Model manifest became INVALID. Cannot proceed.") } else -> { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } else { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } } /** * Updates the model manifest. * * @param nameVal the name of the model manifest to update */ suspend fun updateModelManifest(nameVal: String) { val request = UpdateModelManifestRequest { name = nameVal status = ManifestStatus.Active } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.updateModelManifest(request) println("$nameVal was successfully updated") } } suspend fun deleteDecoderManifest(nameVal: String) { val request = DeleteDecoderManifestRequest { name = nameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteDecoderManifest(request) println("$nameVal was successfully deleted") } } /** * Creates a new decoder manifest. * * @param decName the name of the decoder manifest * @param modelManifestArnVal the ARN of the model manifest * @return the ARN of the decoder manifest */ suspend fun createDecoderManifest(decName: String, modelManifestArnVal: String?): String { val interfaceIdVal = "can0" val canInter = CanInterface { name = "canInterface0" protocolName = "CAN" protocolVersion = "1.0" } val networkInterface = NetworkInterface { interfaceId = interfaceIdVal type = NetworkInterfaceType.CanInterface canInterface = canInter } val carRpmSig = CanSignal { messageId = 100 isBigEndian = false isSigned = false startBit = 16 length = 16 factor = 1.0 offset = 0.0 } val carSpeedSig = CanSignal { messageId = 101 isBigEndian = false isSigned = false startBit = 0 length = 16 factor = 1.0 offset = 0.0 } val engineRpmDecoder = SignalDecoder { fullyQualifiedName = "Vehicle.Powertrain.EngineRPM" interfaceId = interfaceIdVal type = SignalDecoderType.CanSignal canSignal = carRpmSig } val vehicleSpeedDecoder = SignalDecoder { fullyQualifiedName = "Vehicle.Powertrain.VehicleSpeed" interfaceId = interfaceIdVal type = SignalDecoderType.CanSignal canSignal = carSpeedSig } val request = CreateDecoderManifestRequest { name = decName modelManifestArn = modelManifestArnVal networkInterfaces = listOf(networkInterface) signalDecoders = listOf(engineRpmDecoder, vehicleSpeedDecoder) } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createDecoderManifest(request) return response.arn } } /** * Deletes a signal catalog. * * @param name the name of the signal catalog to delete */ suspend fun deleteSignalCatalog(catName: String) { val request = DeleteSignalCatalogRequest { name = catName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteSignalCatalog(request) println(" $catName was successfully deleted") } } /** * Deletes a fleet based on the provided fleet ID. * * @param fleetId the ID of the fleet to be deleted */ suspend fun deleteFleet(fleetIdVal: String) { val request = DeleteFleetRequest { fleetId = fleetIdVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteFleet(request) println(" $fleetIdVal was successfully deleted") } } /** * Deletes a model manifest. * * @param nameVal the name of the model manifest to delete */ suspend fun deleteModelManifest(nameVal: String) { val request = DeleteModelManifestRequest { name = nameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteModelManifest(request) println(" $nameVal was successfully deleted") } } /** * Creates a model manifest. * * @param name the name of the model manifest to create * @param signalCatalogArn the Amazon Resource Name (ARN) of the signal catalog * @param nodes a list of nodes to include in the model manifest * @return a {@link CompletableFuture} that completes with the ARN of the created model manifest */ suspend fun createModelManifest(nameVal: String, signalCatalogArnVal: String, nodesList: List<Node>): String { val fqnList: List<String> = nodesList.map { node -> when (node) { is Node.Sensor -> node.asSensor().fullyQualifiedName is Node.Branch -> node.asBranch().fullyQualifiedName else -> throw RuntimeException("Unsupported node type") } } val request = CreateModelManifestRequest { name = nameVal signalCatalogArn = signalCatalogArnVal nodes = fqnList } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createModelManifest(request) return response.arn } } /** * Lists the signal catalog nodes asynchronously. * * @param signalCatalogName the name of the signal catalog * @return a CompletableFuture that, when completed, contains a list of nodes in the specified signal catalog * @throws CompletionException if an exception occurs during the asynchronous operation */ suspend fun listSignalCatalogNode(signalCatalogName: String): List<Node>? { val request = ListSignalCatalogNodesRequest { name = signalCatalogName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.listSignalCatalogNodes(request) return response.nodes } } /** * Creates a new fleet. * * @param catARN the Amazon Resource Name (ARN) of the signal catalog to associate with the fleet * @param fleetId the unique identifier for the fleet * @return the ID of the created fleet */ suspend fun createFleet(catARN: String, fleetIdVal: String): String { val fleetRequest = CreateFleetRequest { fleetId = fleetIdVal signalCatalogArn = catARN description = "Built using the AWS For Kotlin" } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createFleet(fleetRequest) return response.id } } /** * Creates a signal catalog. * * @param signalCatalogName the name of the signal catalog to create the branch vehicle in * @return the ARN (Amazon Resource Name) of the created signal catalog */ suspend fun createbranchVehicle(signalCatalogName: String): String { delay(2000) // Wait for 2 seconds val branchVehicle = Branch { fullyQualifiedName = "Vehicle" description = "Root branch" } val branchPowertrain = Branch { fullyQualifiedName = "Vehicle.Powertrain" description = "Powertrain branch" } val sensorRPM = Sensor { fullyQualifiedName = "Vehicle.Powertrain.EngineRPM" description = "Engine RPM" dataType = NodeDataType.Double unit = "rpm" } val sensorKM = Sensor { fullyQualifiedName = "Vehicle.Powertrain.VehicleSpeed" description = "Vehicle Speed" dataType = NodeDataType.Double unit = "km/h" } // Wrap each specific node type (Branch and Sensor) into the sealed Node class // so they can be included in the CreateSignalCatalogRequest. val myNodes = listOf( Node.Branch(branchVehicle), Node.Branch(branchPowertrain), Node.Sensor(sensorRPM), Node.Sensor(sensorKM), ) val request = CreateSignalCatalogRequest { name = signalCatalogName nodes = myNodes } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createSignalCatalog(request) return response.arn } } private fun waitForInputToContinue(scanner: Scanner) { while (true) { println("") println("Enter 'c' followed by <ENTER> to continue:") val input = scanner.nextLine() if (input.trim { it <= ' ' }.equals("c", ignoreCase = true)) { println("Continuing with the program...") println("") break } else { println("Invalid input. Please try again.") } } }

動作

以下程式碼範例顯示如何使用 createDecoderManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Creates a new decoder manifest. * * @param decName the name of the decoder manifest * @param modelManifestArnVal the ARN of the model manifest * @return the ARN of the decoder manifest */ suspend fun createDecoderManifest(decName: String, modelManifestArnVal: String?): String { val interfaceIdVal = "can0" val canInter = CanInterface { name = "canInterface0" protocolName = "CAN" protocolVersion = "1.0" } val networkInterface = NetworkInterface { interfaceId = interfaceIdVal type = NetworkInterfaceType.CanInterface canInterface = canInter } val carRpmSig = CanSignal { messageId = 100 isBigEndian = false isSigned = false startBit = 16 length = 16 factor = 1.0 offset = 0.0 } val carSpeedSig = CanSignal { messageId = 101 isBigEndian = false isSigned = false startBit = 0 length = 16 factor = 1.0 offset = 0.0 } val engineRpmDecoder = SignalDecoder { fullyQualifiedName = "Vehicle.Powertrain.EngineRPM" interfaceId = interfaceIdVal type = SignalDecoderType.CanSignal canSignal = carRpmSig } val vehicleSpeedDecoder = SignalDecoder { fullyQualifiedName = "Vehicle.Powertrain.VehicleSpeed" interfaceId = interfaceIdVal type = SignalDecoderType.CanSignal canSignal = carSpeedSig } val request = CreateDecoderManifestRequest { name = decName modelManifestArn = modelManifestArnVal networkInterfaces = listOf(networkInterface) signalDecoders = listOf(engineRpmDecoder, vehicleSpeedDecoder) } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createDecoderManifest(request) return response.arn } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 createDecoderManifest

以下程式碼範例顯示如何使用 createFleet

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Creates a new fleet. * * @param catARN the Amazon Resource Name (ARN) of the signal catalog to associate with the fleet * @param fleetId the unique identifier for the fleet * @return the ID of the created fleet */ suspend fun createFleet(catARN: String, fleetIdVal: String): String { val fleetRequest = CreateFleetRequest { fleetId = fleetIdVal signalCatalogArn = catARN description = "Built using the AWS For Kotlin" } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createFleet(fleetRequest) return response.id } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 createFleet

以下程式碼範例顯示如何使用 createModelManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Creates a model manifest. * * @param name the name of the model manifest to create * @param signalCatalogArn the Amazon Resource Name (ARN) of the signal catalog * @param nodes a list of nodes to include in the model manifest * @return a {@link CompletableFuture} that completes with the ARN of the created model manifest */ suspend fun createModelManifest(nameVal: String, signalCatalogArnVal: String, nodesList: List<Node>): String { val fqnList: List<String> = nodesList.map { node -> when (node) { is Node.Sensor -> node.asSensor().fullyQualifiedName is Node.Branch -> node.asBranch().fullyQualifiedName else -> throw RuntimeException("Unsupported node type") } } val request = CreateModelManifestRequest { name = nameVal signalCatalogArn = signalCatalogArnVal nodes = fqnList } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createModelManifest(request) return response.arn } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 createModelManifest

以下程式碼範例顯示如何使用 createSignalCatalog

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Creates a signal catalog. * * @param signalCatalogName the name of the signal catalog to create the branch vehicle in * @return the ARN (Amazon Resource Name) of the created signal catalog */ suspend fun createbranchVehicle(signalCatalogName: String): String { delay(2000) // Wait for 2 seconds val branchVehicle = Branch { fullyQualifiedName = "Vehicle" description = "Root branch" } val branchPowertrain = Branch { fullyQualifiedName = "Vehicle.Powertrain" description = "Powertrain branch" } val sensorRPM = Sensor { fullyQualifiedName = "Vehicle.Powertrain.EngineRPM" description = "Engine RPM" dataType = NodeDataType.Double unit = "rpm" } val sensorKM = Sensor { fullyQualifiedName = "Vehicle.Powertrain.VehicleSpeed" description = "Vehicle Speed" dataType = NodeDataType.Double unit = "km/h" } // Wrap each specific node type (Branch and Sensor) into the sealed Node class // so they can be included in the CreateSignalCatalogRequest. val myNodes = listOf( Node.Branch(branchVehicle), Node.Branch(branchPowertrain), Node.Sensor(sensorRPM), Node.Sensor(sensorKM), ) val request = CreateSignalCatalogRequest { name = signalCatalogName nodes = myNodes } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.createSignalCatalog(request) return response.arn } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 createSignalCatalog

以下程式碼範例顯示如何使用 createVehicle

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun createVehicle(vecName: String, manifestArn: String?, decArn: String) { val request = CreateVehicleRequest { vehicleName = vecName modelManifestArn = manifestArn decoderManifestArn = decArn } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.createVehicle(request) println("Vehicle $vecName was created successfully.") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 createVehicle

以下程式碼範例顯示如何使用 deleteDecoderManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun deleteDecoderManifest(nameVal: String) { val request = DeleteDecoderManifestRequest { name = nameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteDecoderManifest(request) println("$nameVal was successfully deleted") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 deleteDecoderManifest

以下程式碼範例顯示如何使用 deleteFleet

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a fleet based on the provided fleet ID. * * @param fleetId the ID of the fleet to be deleted */ suspend fun deleteFleet(fleetIdVal: String) { val request = DeleteFleetRequest { fleetId = fleetIdVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteFleet(request) println(" $fleetIdVal was successfully deleted") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 deleteFleet

以下程式碼範例顯示如何使用 deleteModelManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a model manifest. * * @param nameVal the name of the model manifest to delete */ suspend fun deleteModelManifest(nameVal: String) { val request = DeleteModelManifestRequest { name = nameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteModelManifest(request) println(" $nameVal was successfully deleted") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 deleteModelManifest

以下程式碼範例顯示如何使用 deleteSignalCatalog

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a signal catalog. * * @param name the name of the signal catalog to delete */ suspend fun deleteSignalCatalog(catName: String) { val request = DeleteSignalCatalogRequest { name = catName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteSignalCatalog(request) println(" $catName was successfully deleted") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 deleteSignalCatalog

以下程式碼範例顯示如何使用 deleteVehicle

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun deleteVehicle(vecName: String) { val request = DeleteVehicleRequest { vehicleName = vecName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.deleteVehicle(request) println("Vehicle $vecName was deleted successfully.") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 deleteVehicle

以下程式碼範例顯示如何使用 getDecoderManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Waits for the specified model manifest to become active. * * @param decNameVal the name of the model manifest to wait for */ suspend fun waitForDecoderManifestActive(decNameVal: String) { var elapsedSeconds = 0 var lastStatus: ManifestStatus = ManifestStatus.Draft print("⏳ Elapsed: 0s | Status: DRAFT") IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> while (true) { delay(1000) elapsedSeconds++ if (elapsedSeconds % 5 == 0) { val request = GetDecoderManifestRequest { name = decNameVal } val response = fleetwiseClient.getDecoderManifest(request) lastStatus = response.status ?: ManifestStatus.Draft when (lastStatus) { ManifestStatus.Active -> { print("\rElapsed: ${elapsedSeconds}s | Status: ACTIVE ✅\n") return } ManifestStatus.Invalid -> { print("\rElapsed: ${elapsedSeconds}s | Status: INVALID ❌\n") throw RuntimeException("Model manifest became INVALID. Cannot proceed.") } else -> { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } else { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 getDecoderManifest

以下程式碼範例顯示如何使用 getModelManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Waits for the specified model manifest to become active. * * @param manifestName the name of the model manifest to wait for */ suspend fun waitForModelManifestActive(manifestNameVal: String) { var elapsedSeconds = 0 var lastStatus: ManifestStatus = ManifestStatus.Draft print("⏳ Elapsed: 0s | Status: DRAFT") IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> while (true) { delay(1000) elapsedSeconds++ if (elapsedSeconds % 5 == 0) { val request = GetModelManifestRequest { name = manifestNameVal } val response = fleetwiseClient.getModelManifest(request) lastStatus = response.status ?: ManifestStatus.Draft when (lastStatus) { ManifestStatus.Active -> { print("\r Elapsed: ${elapsedSeconds}s | Status: ACTIVE ✅\n") return } ManifestStatus.Invalid -> { print("\r Elapsed: ${elapsedSeconds}s | Status: INVALID ❌\n") throw RuntimeException("Model manifest became INVALID. Cannot proceed.") } else -> { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } else { print("\r Elapsed: ${elapsedSeconds}s | Status: $lastStatus") } } } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 getModelManifest

以下程式碼範例顯示如何使用 getVehicle

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun getVehicleDetails(vehicleNameVal: String) { val request = GetVehicleRequest { vehicleName = vehicleNameVal } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.getVehicle(request) val details = mapOf( "vehicleName" to response.vehicleName, "arn" to response.arn, "modelManifestArn" to response.modelManifestArn, "decoderManifestArn" to response.decoderManifestArn, "attributes" to response.attributes.toString(), "creationTime" to response.creationTime.toString(), "lastModificationTime" to response.lastModificationTime.toString(), ) println("Vehicle Details:") for ((key, value) in details) { println("• %-20s : %s".format(key, value)) } } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 getVehicle

以下程式碼範例顯示如何使用 listSignalCatalogNodes

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Lists the signal catalog nodes asynchronously. * * @param signalCatalogName the name of the signal catalog * @return a CompletableFuture that, when completed, contains a list of nodes in the specified signal catalog * @throws CompletionException if an exception occurs during the asynchronous operation */ suspend fun listSignalCatalogNode(signalCatalogName: String): List<Node>? { val request = ListSignalCatalogNodesRequest { name = signalCatalogName } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> val response = fleetwiseClient.listSignalCatalogNodes(request) return response.nodes } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 listSignalCatalogNodes

以下程式碼範例顯示如何使用 updateDecoderManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun updateDecoderManifest(nameVal: String) { val request = UpdateDecoderManifestRequest { name = nameVal status = ManifestStatus.Active } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.updateDecoderManifest(request) println("$nameVal was successfully updated") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 updateDecoderManifest

以下程式碼範例顯示如何使用 updateModelManifest

SDK for Kotlin
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Updates the model manifest. * * @param nameVal the name of the model manifest to update */ suspend fun updateModelManifest(nameVal: String) { val request = UpdateModelManifestRequest { name = nameVal status = ManifestStatus.Active } IotFleetWiseClient { region = "us-east-1" }.use { fleetwiseClient -> fleetwiseClient.updateModelManifest(request) println("$nameVal was successfully updated") } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 updateModelManifest