文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
GetThingShadow
与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 GetThingShadow
。
- C++
-
- SDK for C++
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 //! Get the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param documentResult: String to receive the state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::getThingShadow(const Aws::String &thingName, Aws::String &documentResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotClient(clientConfiguration); Aws::IoTDataPlane::Model::GetThingShadowRequest request; request.SetThingName(thingName); auto outcome = iotClient.GetThingShadow(request); if (outcome.IsSuccess()) { std::stringstream ss; ss << outcome.GetResult().GetPayload().rdbuf(); documentResult = ss.str(); } else { std::cerr << "Error getting thing shadow: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
有关 API 的详细信息,请参阅 AWS SDK for C++ API 参考GetThingShadow中的。
-
- CLI
-
- AWS CLI
-
获取事物影子文档
以下
get-thing-shadow
示例获取指定 IoT 事物的事物影子文档。aws iot-data get-thing-shadow \ --thing-name
MyRPi
\output.txt
该命令不会在显示屏上生成任何输出,但以下显示了
output.txt
的内容:{ "state":{ "reported":{ "moisture":"low" } }, "metadata":{ "reported":{ "moisture":{ "timestamp":1560269319 } } }, "version":1,"timestamp":1560269405 }
有关更多信息,请参阅《AWS IoT 开发人员指南》中的设备影子服务数据流。
-
有关 API 的详细信息,请参阅AWS CLI 命令参考GetThingShadow
中的。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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(); }
-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考GetThingShadow中的。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 suspend fun getPayload(thingNameVal: String?) { val getThingShadowRequest = GetThingShadowRequest { thingName = thingNameVal } IotDataPlaneClient { region = "us-east-1" }.use { iotPlaneClient -> val getThingShadowResponse = iotPlaneClient.getThingShadow(getThingShadowRequest) val payload = getThingShadowResponse.payload val payloadString = payload?.let { java.lang.String(it, Charsets.UTF_8) } println("Received shadow data: $payloadString") } }
-
有关 API 的详细信息,请参阅适用GetThingShadow
于 K otlin 的AWS SDK API 参考。
-
操作
UpdateThingShadow