与 AWS SDK或UpdateThingShadow一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或UpdateThingShadow一起使用 CLI

以下代码示例演示如何使用 UpdateThingShadow

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Update the shadow of an AWS IoT thing. /*! \param thingName: The name for the thing. \param document: The state information, in JSON format. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThingShadow(const Aws::String &thingName, const Aws::String &document, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoTDataPlane::IoTDataPlaneClient iotDataPlaneClient(clientConfiguration); Aws::IoTDataPlane::Model::UpdateThingShadowRequest updateThingShadowRequest; updateThingShadowRequest.SetThingName(thingName); std::shared_ptr<std::stringstream> streamBuf = std::make_shared<std::stringstream>( document); updateThingShadowRequest.SetBody(streamBuf); Aws::IoTDataPlane::Model::UpdateThingShadowOutcome outcome = iotDataPlaneClient.UpdateThingShadow( updateThingShadowRequest); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing shadow." << std::endl; } else { std::cerr << "Error while updating thing shadow." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 UpdateThingShadow” 中的。

CLI
AWS CLI

更新事物影子

以下update-thing-shadow示例修改了指定事物的设备影子的当前状态并将其保存到文件output.txt中。

aws iot-data update-thing-shadow \ --thing-name MyRPi \ --payload "{"state":{"reported":{"moisture":"okay"}}}" \ "output.txt"

该命令不会在显示屏上生成任何输出,但以下显示了以下内容output.txt

{ "state": { "reported": { "moisture": "okay" } }, "metadata": { "reported": { "moisture": { "timestamp": 1560270036 } } }, "version": 2, "timestamp": 1560270036 }

有关更多信息,请参阅《AWS 物联网开发人员指南》中的 Device Shado w 服务数据流

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 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(); }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 UpdateThingShadow” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun updateShawdowThing(thingNameVal: String?) { // Create the thing shadow state document. val stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}" val byteStream: ByteStream = ByteStream.fromString(stateDocument) val byteArray: ByteArray = byteStream.toByteArray() val updateThingShadowRequest = UpdateThingShadowRequest { thingName = thingNameVal payload = byteArray } IotDataPlaneClient { region = "us-east-1" }.use { iotPlaneClient -> iotPlaneClient.updateThingShadow(updateThingShadowRequest) println("The thing shadow was updated successfully.") } }
  • 有关API详细信息,请参阅UpdateThingShadow中的 Kotlin AWS SDK API 参考