Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .
As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Use createVehicle com um AWS SDK
Os exemplos de código a seguir mostram como usar o createVehicle.
Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação em contexto no seguinte exemplo de código:
- Java
-
- SDK para Java 2.x
-
/**
* Creates a new vehicle in the system.
*
* @param vecName the name of the vehicle to be created
* @param manifestArn the Amazon Resource Name (ARN) of the model manifest for the vehicle
* @param decArn the Amazon Resource Name (ARN) of the decoder manifest for the vehicle
* @return a {@link CompletableFuture} that completes when the vehicle has been created, or throws a
*/
public CompletableFuture<Void> createVehicleAsync(String vecName, String manifestArn, String decArn) {
CreateVehicleRequest request = CreateVehicleRequest.builder()
.vehicleName(vecName)
.modelManifestArn(manifestArn)
.decoderManifestArn(decArn)
.build();
CompletableFuture<Void> result = new CompletableFuture<>();
getAsyncClient().createVehicle(request)
.whenComplete((response, exception) -> {
if (exception != null) {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof ResourceNotFoundException) {
result.completeExceptionally(cause);
} else {
result.completeExceptionally(new RuntimeException("Failed to create vehicle: " + cause.getMessage(), cause));
}
} else {
logger.info("Vehicle '{}' created successfully.", vecName);
result.complete(null); // mark future as complete
}
});
return result;
}
- Kotlin
-
- SDK para Kotlin
-
suspend fun createVehicle(vecName: String, manifestArn: String?, decArn: String) {
val request = CreateVehicleRequest {
vehicleName = vecName
modelManifestArn = manifestArn
decoderManifestArn = decArn
}
IotFleetWiseClient.fromEnvironment { region = "us-east-1" }.use { fleetwiseClient ->
fleetwiseClient.createVehicle(request)
println("Vehicle $vecName was created successfully.")
}
}