UpdateThing与 AWS SDK 或 CLI 配合使用 - AWS IoT Core

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

UpdateThing与 AWS SDK 或 CLI 配合使用

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

.NET
适用于 .NET 的 SDK(v4)
注意

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

/// <summary> /// Updates an IoT Thing with attributes. /// </summary> /// <param name="thingName">The name of the Thing to update.</param> /// <param name="attributes">Dictionary of attributes to add.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingAsync(string thingName, Dictionary<string, string> attributes) { try { var request = new UpdateThingRequest { ThingName = thingName, AttributePayload = new AttributePayload { Attributes = attributes, Merge = true } }; await _amazonIoT.UpdateThingAsync(request); _logger.LogInformation($"Updated Thing {thingName} with attributes"); return true; } catch (Amazon.IoT.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing attributes. Here's why: {ex.Message}"); return false; } }
  • 有关 API 的详细信息,请参阅 适用于 .NET 的 AWS SDKAPI 参考UpdateThing中的。

C++
SDK for C++
注意

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

//! Update an AWS IoT thing with attributes. /*! \param thingName: The name for the thing. \param attributeMap: A map of key/value attributes/ \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateThing(const Aws::String &thingName, const std::map<Aws::String, Aws::String> &attributeMap, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::UpdateThingRequest request; request.SetThingName(thingName); Aws::IoT::Model::AttributePayload attributePayload; for (const auto &attribute: attributeMap) { attributePayload.AddAttributes(attribute.first, attribute.second); } request.SetAttributePayload(attributePayload); Aws::IoT::Model::UpdateThingOutcome outcome = iotClient.UpdateThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully updated thing " << thingName << std::endl; } else { std::cerr << "Failed to update thing " << thingName << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 有关 API 的详细信息,请参阅 适用于 C++ 的 AWS SDKAPI 参考UpdateThing中的。

CLI
AWS CLI

将事物与事物类型关联

以下update-thing示例将 AWS IoT 注册表中的事物与事物类型相关联。在建立这种关联时,需要为事物类型定义的属性提供值。

aws iot update-thing \ --thing-name "MyOtherLightBulb" \ --thing-type-name "LightBulb" \ --attribute-payload "{"attributes": {"wattage":"75", "model":"123"}}"

此命令不生成任何输出。使用 describe-thing 命令查看结果。

有关更多信息,请参阅《AWS物联网开发人员指南》中的事物类型

  • 有关 API 的详细信息,请参阅AWS CLI命令参考UpdateThing中的。

Kotlin
适用于 Kotlin 的 SDK
注意

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

suspend fun updateThing(thingNameVal: String?) { val newLocation = "Office" val newFirmwareVersion = "v2.0" val attMap: MutableMap<String, String> = HashMap() attMap["location"] = newLocation attMap["firmwareVersion"] = newFirmwareVersion val attributePayloadVal = AttributePayload { attributes = attMap } val updateThingRequest = UpdateThingRequest { thingName = thingNameVal attributePayload = attributePayloadVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> // Update the IoT thing attributes. iotClient.updateThing(updateThingRequest) println("$thingNameVal attributes updated successfully.") } }
  • 有关 API 的详细信息,请参阅适用UpdateThing于 K otlin 的 AWS SDK API 参考

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅AWS IoT与 AWS SDK 一起使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。