AWS IoT data ejemplos que se utilizan SDK para Java 2.x - AWS SDK for Java 2.x

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

AWS IoT data ejemplos que se utilizan SDK para Java 2.x

Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante el uso del AWS SDK for Java 2.x with AWS IoT data.

Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados y en los ejemplos entre servicios.

Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica llamando a varias funciones dentro del mismo servicio.

Cada ejemplo incluye un enlace a GitHub, donde puede encontrar instrucciones sobre cómo configurar y ejecutar el código en su contexto.

Acciones

En el siguiente ejemplo de código, se muestra cómo usar GetThingShadow.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

/** * Retrieves the payload of a Thing's shadow asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to get the payload of a Thing's shadow. * If the request is successful, it prints the shadow data. * If an exception occurs, it prints the error message. */ public void getPayload(String thingName) { GetThingShadowRequest getThingShadowRequest = GetThingShadowRequest.builder() .thingName(thingName) .build(); CompletableFuture<GetThingShadowResponse> future = getAsyncDataPlaneClient().getThingShadow(getThingShadowRequest); future.whenComplete((getThingShadowResponse, ex) -> { if (getThingShadowResponse != null) { // Extracting payload from response. SdkBytes payload = getThingShadowResponse.payload(); String payloadString = payload.asUtf8String(); System.out.println("Received Shadow Data: " + payloadString); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to get Thing Shadow payload."); } } }); future.join(); }
  • Para API obtener más información, consulte GetThingShadowla AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código, se muestra cómo usar UpdateThingShadow.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

/** * Updates the shadow of an IoT Thing asynchronously. * * @param thingName The name of the IoT Thing. * * This method initiates an asynchronous request to update the shadow of an IoT Thing. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void updateShadowThing(String thingName) { // Create Thing Shadow State Document. String stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}"; SdkBytes data = SdkBytes.fromString(stateDocument, StandardCharsets.UTF_8); UpdateThingShadowRequest updateThingShadowRequest = UpdateThingShadowRequest.builder() .thingName(thingName) .payload(data) .build(); CompletableFuture<UpdateThingShadowResponse> future = getAsyncDataPlaneClient().updateThingShadow(updateThingShadowRequest); future.whenComplete((updateResponse, ex) -> { if (updateResponse != null) { System.out.println("Thing Shadow updated successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to update Thing Shadow."); } } }); future.join(); }
  • Para API obtener más información, consulte UpdateThingShadowla AWS SDK for Java 2.x APIReferencia.