AWS IoT data esempi di utilizzo SDK per Java 2.x - AWS SDK for Java 2.x

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

AWS IoT data esempi di utilizzo SDK per Java 2.x

I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS SDK for Java 2.x with AWS IoT data.

Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati e negli esempi tra servizi.

Scenari: esempi di codice che mostrano come eseguire un'attività specifica richiamando più funzioni all'interno dello stesso servizio.

Ogni esempio include un collegamento a GitHub, dove è possibile trovare istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

Il seguente esempio di codice mostra come utilizzareGetThingShadow.

SDKper Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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(); }

Il seguente esempio di codice mostra come utilizzareUpdateThingShadow.

SDKper Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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(); }