文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS IoT 使用 SDK for C++ 的範例
下列程式碼範例示範如何使用 AWS SDK for C++ 搭配 來執行動作和實作常見案例 AWS IoT。
基本概念是程式碼範例,示範如何在服務中執行基本操作。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
開始使用
下列程式碼範例示範如何開始使用 AWS IoT。
- C++ 的 SDK
-
CMakeLists.txt CMake 檔案的程式碼。
# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS iot) # Set this project's name. project("hello_iot") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_iot.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})
hello_iot.cpp 來源檔案的程式碼。
#include <aws/core/Aws.h> #include <aws/iot/IoTClient.h> #include <aws/iot/model/ListThingsRequest.h> #include <iostream> /* * A "Hello IoT" starter application which initializes an AWS IoT client and * lists the AWS IoT topics in the current account. * * main function * * Usage: 'hello_iot' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optional: change the log level for debugging. // options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::IoT::IoTClient iotClient(clientConfig); // List the things in the current account. Aws::IoT::Model::ListThingsRequest listThingsRequest; Aws::String nextToken; // Used for pagination. Aws::Vector<Aws::IoT::Model::ThingAttribute> allThings; do { if (!nextToken.empty()) { listThingsRequest.SetNextToken(nextToken); } Aws::IoT::Model::ListThingsOutcome listThingsOutcome = iotClient.ListThings( listThingsRequest); if (listThingsOutcome.IsSuccess()) { const Aws::Vector<Aws::IoT::Model::ThingAttribute> &things = listThingsOutcome.GetResult().GetThings(); allThings.insert(allThings.end(), things.begin(), things.end()); nextToken = listThingsOutcome.GetResult().GetNextToken(); } else { std::cerr << "List things failed" << listThingsOutcome.GetError().GetMessage() << std::endl; break; } } while (!nextToken.empty()); std::cout << allThings.size() << " thing(s) found." << std::endl; for (auto const &thing: allThings) { std::cout << thing.GetThingName() << std::endl; } } Aws::ShutdownAPI(options); // Should only be called once. return 0; }
-
如需 API 詳細資訊,請參閱 listThings AWS SDK for C++ 參考中的 API。
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 -
基本概念
下列程式碼範例示範如何使用 AWS IoT 裝置管理。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 建立 AWS IoT 物件。
Aws::String thingName = askQuestion("Enter a thing name: "); if (!createThing(thingName, clientConfiguration)) { std::cerr << "Exiting because createThing failed." << std::endl; cleanup("", "", "", "", "", false, clientConfiguration); return false; }
//! Create an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateThingRequest createThingRequest; createThingRequest.SetThingName(thingName); Aws::IoT::Model::CreateThingOutcome outcome = iotClient.CreateThing( createThingRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created thing " << thingName << std::endl; } else { std::cerr << "Failed to create thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
產生並連接裝置憑證。
Aws::String certificateARN; Aws::String certificateID; if (askYesNoQuestion("Would you like to create a certificate for your thing? (y/n) ")) { Aws::String outputFolder; if (askYesNoQuestion( "Would you like to save the certificate and keys to file? (y/n) ")) { outputFolder = std::filesystem::current_path(); outputFolder += "/device_keys_and_certificates"; std::filesystem::create_directories(outputFolder); std::cout << "The certificate and keys will be saved to the folder: " << outputFolder << std::endl; } if (!createKeysAndCertificate(outputFolder, certificateARN, certificateID, clientConfiguration)) { std::cerr << "Exiting because createKeysAndCertificate failed." << std::endl; cleanup(thingName, "", "", "", "", false, clientConfiguration); return false; } std::cout << "\nNext, the certificate will be attached to the thing.\n" << std::endl; if (!attachThingPrincipal(certificateARN, thingName, clientConfiguration)) { std::cerr << "Exiting because attachThingPrincipal failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } }
//! Create keys and certificate for an Aws IoT device. //! This routine will save certificates and keys to an output folder, if provided. /*! \param outputFolder: Location for storing output in files, ignored when string is empty. \param certificateARNResult: A string to receive the ARN of the created certificate. \param certificateID: A string to receive the ID of the created certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createKeysAndCertificate(const Aws::String &outputFolder, Aws::String &certificateARNResult, Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::CreateKeysAndCertificateRequest createKeysAndCertificateRequest; Aws::IoT::Model::CreateKeysAndCertificateOutcome outcome = client.CreateKeysAndCertificate(createKeysAndCertificateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created a certificate and keys" << std::endl; certificateARNResult = outcome.GetResult().GetCertificateArn(); certificateID = outcome.GetResult().GetCertificateId(); std::cout << "Certificate ARN: " << certificateARNResult << ", certificate ID: " << certificateID << std::endl; if (!outputFolder.empty()) { std::cout << "Writing certificate and keys to the folder '" << outputFolder << "'." << std::endl; std::cout << "Be sure these files are stored securely." << std::endl; Aws::String certificateFilePath = outputFolder + "/certificate.pem.crt"; std::ofstream certificateFile(certificateFilePath); if (!certificateFile.is_open()) { std::cerr << "Error opening certificate file, '" << certificateFilePath << "'." << std::endl; return false; } certificateFile << outcome.GetResult().GetCertificatePem(); certificateFile.close(); const Aws::IoT::Model::KeyPair &keyPair = outcome.GetResult().GetKeyPair(); Aws::String privateKeyFilePath = outputFolder + "/private.pem.key"; std::ofstream privateKeyFile(privateKeyFilePath); if (!privateKeyFile.is_open()) { std::cerr << "Error opening private key file, '" << privateKeyFilePath << "'." << std::endl; return false; } privateKeyFile << keyPair.GetPrivateKey(); privateKeyFile.close(); Aws::String publicKeyFilePath = outputFolder + "/public.pem.key"; std::ofstream publicKeyFile(publicKeyFilePath); if (!publicKeyFile.is_open()) { std::cerr << "Error opening public key file, '" << publicKeyFilePath << "'." << std::endl; return false; } publicKeyFile << keyPair.GetPublicKey(); } } else { std::cerr << "Error creating keys and certificate: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Attach a principal to an AWS IoT thing. /*! \param principal: A principal to attach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::attachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::AttachThingPrincipalRequest request; request.SetPrincipal(principal); request.SetThingName(thingName); Aws::IoT::Model::AttachThingPrincipalOutcome outcome = client.AttachThingPrincipal( request); if (outcome.IsSuccess()) { std::cout << "Successfully attached principal to thing." << std::endl; } else { std::cerr << "Failed to attach principal to thing." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
對 AWS IoT 物件執行各種操作。
if (!updateThing(thingName, { {"location", "Office"}, {"firmwareVersion", "v2.0"} }, clientConfiguration)) { std::cerr << "Exiting because updateThing failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now an endpoint will be retrieved for your account.\n" << std::endl; std::cout << "An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point\n" << "for communication between IoT devices and the AWS IoT service." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); Aws::String endpoint; if (!describeEndpoint(endpoint, clientConfiguration)) { std::cerr << "Exiting because getEndpoint failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } std::cout <<"Your endpoint is " << endpoint << "." << std::endl; printAsterisksLine(); std::cout << "Now the certificates in your account will be listed." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); if (!listCertificates(clientConfiguration)) { std::cerr << "Exiting because listCertificates failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now the shadow for the thing will be updated.\n" << std::endl; std::cout << "A thing shadow refers to a feature that enables you to create a virtual representation, or \"shadow,\"\n" << "of a physical device or thing. The thing shadow allows you to synchronize and control the state of a device between\n" << "the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a thing shadow." << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); if (!updateThingShadow(thingName, R"({"state":{"reported":{"temperature":25,"humidity":50}}})", clientConfiguration)) { std::cerr << "Exiting because updateThingShadow failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now, the state information for the shadow will be retrieved.\n" << std::endl; askQuestion("Press Enter to continue:", alwaysTrueTest); Aws::String shadowState; if (!getThingShadow(thingName, shadowState, clientConfiguration)) { std::cerr << "Exiting because getThingShadow failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } std::cout << "The retrieved shadow state is: " << shadowState << std::endl; printAsterisksLine(); std::cout << "A rule with now be added to to the thing.\n" << std::endl; std::cout << "Any user who has permission to create rules will be able to access data processed by the rule." << std::endl; std::cout << "In this case, the rule will use an Simple Notification Service (SNS) topic and an IAM rule." << std::endl; std::cout << "These resources will be created using a CloudFormation template." << std::endl; std::cout << "Stack creation may take a few minutes." << std::endl; askQuestion("Press Enter to continue: ", alwaysTrueTest); Aws::Map<Aws::String, Aws::String> outputs =createCloudFormationStack(STACK_NAME,clientConfiguration); if (outputs.empty()) { std::cerr << "Exiting because createCloudFormationStack failed." << std::endl; cleanup(thingName, certificateARN, certificateID, "", "", false, clientConfiguration); return false; } // Retrieve the topic ARN and role ARN from the CloudFormation stack outputs. auto topicArnIter = outputs.find(SNS_TOPIC_ARN_OUTPUT); auto roleArnIter = outputs.find(ROLE_ARN_OUTPUT); if ((topicArnIter == outputs.end()) || (roleArnIter == outputs.end())) { std::cerr << "Exiting because output '" << SNS_TOPIC_ARN_OUTPUT << "' or '" << ROLE_ARN_OUTPUT << "'not found in the CloudFormation stack." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, "", false, clientConfiguration); return false; } Aws::String topicArn = topicArnIter->second; Aws::String roleArn = roleArnIter->second; Aws::String sqlStatement = "SELECT * FROM '"; sqlStatement += MQTT_MESSAGE_TOPIC_FILTER; sqlStatement += "'"; printAsterisksLine(); std::cout << "Now a rule will be created.\n" << std::endl; std::cout << "Rules are an administrator-level action. Any user who has permission\n" << "to create rules will be able to access data processed by the rule." << std::endl; std::cout << "In this case, the rule will use an SNS topic" << std::endl; std::cout << "and the following SQL statement '" << sqlStatement << "'." << std::endl; std::cout << "For more information on IoT SQL, see https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-reference.html" << std::endl; Aws::String ruleName = askQuestion("Enter a rule name: "); if (!createTopicRule(ruleName, topicArn, sqlStatement, roleArn, clientConfiguration)) { std::cerr << "Exiting because createRule failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, "", false, clientConfiguration); return false; } printAsterisksLine(); std::cout << "Now your rules will be listed.\n" << std::endl; askQuestion("Press Enter to continue: ", alwaysTrueTest); if (!listTopicRules(clientConfiguration)) { std::cerr << "Exiting because listRules failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; } printAsterisksLine(); Aws::String queryString = "thingName:" + thingName; std::cout << "Now the AWS IoT fleet index will be queried with the query\n'" << queryString << "'.\n" << std::endl; std::cout << "For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html" << std::endl; std::cout << "For this query to work, thing indexing must be enabled in your account.\n" << "This can be done with the awscli command line by calling 'aws iot update-indexing-configuration'\n" << "or it can be done programmatically." << std::endl; std::cout << "For more information, see https://docs.aws.amazon.com/iot/latest/developerguide/managing-index.html" << std::endl; if (askYesNoQuestion("Do you want to enable thing indexing in your account? (y/n) ")) { Aws::IoT::Model::ThingIndexingConfiguration thingIndexingConfiguration; thingIndexingConfiguration.SetThingIndexingMode(Aws::IoT::Model::ThingIndexingMode::REGISTRY_AND_SHADOW); thingIndexingConfiguration.SetThingConnectivityIndexingMode(Aws::IoT::Model::ThingConnectivityIndexingMode::STATUS); // The ThingGroupIndexingConfiguration object is ignored if not set. Aws::IoT::Model::ThingGroupIndexingConfiguration thingGroupIndexingConfiguration; if (!updateIndexingConfiguration(thingIndexingConfiguration, thingGroupIndexingConfiguration, clientConfiguration)) { std::cerr << "Exiting because updateIndexingConfiguration failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; } } if (!searchIndex(queryString, clientConfiguration)) { std::cerr << "Exiting because searchIndex failed." << std::endl; cleanup(thingName, certificateARN, certificateID, STACK_NAME, ruleName, false, clientConfiguration); return false; }
//! 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(); } //! Describe the endpoint specific to the AWS account making the call. /*! \param endpointResult: String to receive the endpoint result. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeEndpoint(Aws::String &endpointResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::String endpoint; Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeEndpointRequest describeEndpointRequest; describeEndpointRequest.SetEndpointType( "iot:Data-ATS"); // Recommended endpoint type. Aws::IoT::Model::DescribeEndpointOutcome outcome = iotClient.DescribeEndpoint( describeEndpointRequest); if (outcome.IsSuccess()) { std::cout << "Successfully described endpoint." << std::endl; endpointResult = outcome.GetResult().GetEndpointAddress(); } else { std::cerr << "Error describing endpoint" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! List certificates registered in the AWS account making the call. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listCertificates( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListCertificatesRequest request; Aws::Vector<Aws::IoT::Model::Certificate> allCertificates; Aws::String marker; // Used to paginate results. do { if (!marker.empty()) { request.SetMarker(marker); } Aws::IoT::Model::ListCertificatesOutcome outcome = iotClient.ListCertificates( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListCertificatesResult &result = outcome.GetResult(); marker = result.GetNextMarker(); allCertificates.insert(allCertificates.end(), result.GetCertificates().begin(), result.GetCertificates().end()); } else { std::cerr << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << allCertificates.size() << " certificate(s) found." << std::endl; for (auto &certificate: allCertificates) { std::cout << "Certificate ID: " << certificate.GetCertificateId() << std::endl; std::cout << "Certificate ARN: " << certificate.GetCertificateArn() << std::endl; std::cout << std::endl; } return true; } //! 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(); } //! 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(); } //! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Lists the AWS IoT topic rules. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listTopicRules( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListTopicRulesRequest request; Aws::Vector<Aws::IoT::Model::TopicRuleListItem> allRules; Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::IoT::Model::ListTopicRulesOutcome outcome = iotClient.ListTopicRules( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListTopicRulesResult &result = outcome.GetResult(); allRules.insert(allRules.end(), result.GetRules().cbegin(), result.GetRules().cend()); nextToken = result.GetNextToken(); } else { std::cerr << "ListTopicRules error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << "ListTopicRules: " << allRules.size() << " rule(s) found." << std::endl; for (auto &rule: allRules) { std::cout << " Rule name: " << rule.GetRuleName() << ", rule ARN: " << rule.GetRuleArn() << "." << std::endl; } return true; } //! Query the AWS IoT fleet index. //! For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html /*! \param: query: The query string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::searchIndex(const Aws::String &query, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::SearchIndexRequest request; request.SetQueryString(query); Aws::Vector<Aws::IoT::Model::ThingDocument> allThingDocuments; Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::IoT::Model::SearchIndexOutcome outcome = iotClient.SearchIndex(request); if (outcome.IsSuccess()) { const Aws::IoT::Model::SearchIndexResult &result = outcome.GetResult(); allThingDocuments.insert(allThingDocuments.end(), result.GetThings().cbegin(), result.GetThings().cend()); nextToken = result.GetNextToken(); } else { std::cerr << "Error in SearchIndex: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << allThingDocuments.size() << " thing document(s) found." << std::endl; for (const auto thingDocument: allThingDocuments) { std::cout << " Thing name: " << thingDocument.GetThingName() << "." << std::endl; } return true; }
清除資源。
bool AwsDoc::IoT::cleanup(const Aws::String &thingName, const Aws::String &certificateARN, const Aws::String &certificateID, const Aws::String &stackName, const Aws::String &ruleName, bool askForConfirmation, const Aws::Client::ClientConfiguration &clientConfiguration) { bool result = true; if (!ruleName.empty() && (!askForConfirmation || askYesNoQuestion("Delete the rule '" + ruleName + "'? (y/n) "))) { result &= deleteTopicRule(ruleName, clientConfiguration); } Aws::CloudFormation::CloudFormationClient cloudFormationClient(clientConfiguration); if (!stackName.empty() && (!askForConfirmation || askYesNoQuestion( "Delete the CloudFormation stack '" + stackName + "'? (y/n) "))) { result &= deleteStack(stackName, clientConfiguration); } if (!certificateARN.empty() && (!askForConfirmation || askYesNoQuestion("Delete the certificate '" + certificateARN + "'? (y/n) "))) { result &= detachThingPrincipal(certificateARN, thingName, clientConfiguration); result &= deleteCertificate(certificateID, clientConfiguration); } if (!thingName.empty() && (!askForConfirmation || askYesNoQuestion("Delete the thing '" + thingName + "'? (y/n) "))) { result &= deleteThing(thingName, clientConfiguration); } return result; }
//! Detach a principal from an AWS IoT thing. /*! \param principal: A principal to detach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::detachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DetachThingPrincipalRequest detachThingPrincipalRequest; detachThingPrincipalRequest.SetThingName(thingName); detachThingPrincipalRequest.SetPrincipal(principal); Aws::IoT::Model::DetachThingPrincipalOutcome outcome = iotClient.DetachThingPrincipal( detachThingPrincipalRequest); if (outcome.IsSuccess()) { std::cout << "Successfully detached principal " << principal << " from thing " << thingName << std::endl; } else { std::cerr << "Failed to detach principal " << principal << " from thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete a certificate. /*! \param certificateID: The ID of a certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteCertificate(const Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteCertificateRequest request; request.SetCertificateId(certificateID); Aws::IoT::Model::DeleteCertificateOutcome outcome = iotClient.DeleteCertificate( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted certificate " << certificateID << std::endl; } else { std::cerr << "Error deleting certificate " << certificateID << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete an AWS IoT rule. /*! \param ruleName: The name for the rule. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteTopicRule(const Aws::String &ruleName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::DeleteTopicRuleOutcome outcome = iotClient.DeleteTopicRule( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted rule " << ruleName << std::endl; } else { std::cerr << "Failed to delete rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } //! Delete an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteThingRequest request; request.SetThingName(thingName); const auto outcome = iotClient.DeleteThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted thing " << thingName << std::endl; } else { std::cerr << "Error deleting thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
動作
下列程式碼範例示範如何使用 AttachThingPrincipal
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Attach a principal to an AWS IoT thing. /*! \param principal: A principal to attach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::attachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::AttachThingPrincipalRequest request; request.SetPrincipal(principal); request.SetThingName(thingName); Aws::IoT::Model::AttachThingPrincipalOutcome outcome = client.AttachThingPrincipal( request); if (outcome.IsSuccess()) { std::cout << "Successfully attached principal to thing." << std::endl; } else { std::cerr << "Failed to attach principal to thing." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 AttachThingPrincipal AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 CreateKeysAndCertificate
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Create keys and certificate for an Aws IoT device. //! This routine will save certificates and keys to an output folder, if provided. /*! \param outputFolder: Location for storing output in files, ignored when string is empty. \param certificateARNResult: A string to receive the ARN of the created certificate. \param certificateID: A string to receive the ID of the created certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createKeysAndCertificate(const Aws::String &outputFolder, Aws::String &certificateARNResult, Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient client(clientConfiguration); Aws::IoT::Model::CreateKeysAndCertificateRequest createKeysAndCertificateRequest; Aws::IoT::Model::CreateKeysAndCertificateOutcome outcome = client.CreateKeysAndCertificate(createKeysAndCertificateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created a certificate and keys" << std::endl; certificateARNResult = outcome.GetResult().GetCertificateArn(); certificateID = outcome.GetResult().GetCertificateId(); std::cout << "Certificate ARN: " << certificateARNResult << ", certificate ID: " << certificateID << std::endl; if (!outputFolder.empty()) { std::cout << "Writing certificate and keys to the folder '" << outputFolder << "'." << std::endl; std::cout << "Be sure these files are stored securely." << std::endl; Aws::String certificateFilePath = outputFolder + "/certificate.pem.crt"; std::ofstream certificateFile(certificateFilePath); if (!certificateFile.is_open()) { std::cerr << "Error opening certificate file, '" << certificateFilePath << "'." << std::endl; return false; } certificateFile << outcome.GetResult().GetCertificatePem(); certificateFile.close(); const Aws::IoT::Model::KeyPair &keyPair = outcome.GetResult().GetKeyPair(); Aws::String privateKeyFilePath = outputFolder + "/private.pem.key"; std::ofstream privateKeyFile(privateKeyFilePath); if (!privateKeyFile.is_open()) { std::cerr << "Error opening private key file, '" << privateKeyFilePath << "'." << std::endl; return false; } privateKeyFile << keyPair.GetPrivateKey(); privateKeyFile.close(); Aws::String publicKeyFilePath = outputFolder + "/public.pem.key"; std::ofstream publicKeyFile(publicKeyFilePath); if (!publicKeyFile.is_open()) { std::cerr << "Error opening public key file, '" << publicKeyFilePath << "'." << std::endl; return false; } publicKeyFile << keyPair.GetPublicKey(); } } else { std::cerr << "Error creating keys and certificate: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 CreateKeysAndCertificate AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 CreateThing
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Create an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateThingRequest createThingRequest; createThingRequest.SetThingName(thingName); Aws::IoT::Model::CreateThingOutcome outcome = iotClient.CreateThing( createThingRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created thing " << thingName << std::endl; } else { std::cerr << "Failed to create thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 CreateThing AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 CreateTopicRule
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 CreateTopicRule AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DeleteCertificate
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Delete a certificate. /*! \param certificateID: The ID of a certificate. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteCertificate(const Aws::String &certificateID, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteCertificateRequest request; request.SetCertificateId(certificateID); Aws::IoT::Model::DeleteCertificateOutcome outcome = iotClient.DeleteCertificate( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted certificate " << certificateID << std::endl; } else { std::cerr << "Error deleting certificate " << certificateID << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DeleteCertificate AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DeleteThing
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Delete an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteThingRequest request; request.SetThingName(thingName); const auto outcome = iotClient.DeleteThing(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted thing " << thingName << std::endl; } else { std::cerr << "Error deleting thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DeleteThing AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DeleteTopicRule
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Delete an AWS IoT rule. /*! \param ruleName: The name for the rule. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::deleteTopicRule(const Aws::String &ruleName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DeleteTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::DeleteTopicRuleOutcome outcome = iotClient.DeleteTopicRule( request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted rule " << ruleName << std::endl; } else { std::cerr << "Failed to delete rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DeleteTopicRule AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DescribeEndpoint
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Describe the endpoint specific to the AWS account making the call. /*! \param endpointResult: String to receive the endpoint result. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeEndpoint(Aws::String &endpointResult, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::String endpoint; Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeEndpointRequest describeEndpointRequest; describeEndpointRequest.SetEndpointType( "iot:Data-ATS"); // Recommended endpoint type. Aws::IoT::Model::DescribeEndpointOutcome outcome = iotClient.DescribeEndpoint( describeEndpointRequest); if (outcome.IsSuccess()) { std::cout << "Successfully described endpoint." << std::endl; endpointResult = outcome.GetResult().GetEndpointAddress(); } else { std::cerr << "Error describing endpoint" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DescribeEndpoint AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DescribeThing
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Describe an AWS IoT thing. /*! \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::describeThing(const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DescribeThingRequest request; request.SetThingName(thingName); Aws::IoT::Model::DescribeThingOutcome outcome = iotClient.DescribeThing(request); if (outcome.IsSuccess()) { const Aws::IoT::Model::DescribeThingResult &result = outcome.GetResult(); std::cout << "Retrieved thing '" << result.GetThingName() << "'" << std::endl; std::cout << "thingArn: " << result.GetThingArn() << std::endl; std::cout << result.GetAttributes().size() << " attribute(s) retrieved" << std::endl; for (const auto &attribute: result.GetAttributes()) { std::cout << " attribute: " << attribute.first << "=" << attribute.second << std::endl; } } else { std::cerr << "Error describing thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DescribeThing AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 DetachThingPrincipal
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Detach a principal from an AWS IoT thing. /*! \param principal: A principal to detach. \param thingName: The name for the thing. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::detachThingPrincipal(const Aws::String &principal, const Aws::String &thingName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::DetachThingPrincipalRequest detachThingPrincipalRequest; detachThingPrincipalRequest.SetThingName(thingName); detachThingPrincipalRequest.SetPrincipal(principal); Aws::IoT::Model::DetachThingPrincipalOutcome outcome = iotClient.DetachThingPrincipal( detachThingPrincipalRequest); if (outcome.IsSuccess()) { std::cout << "Successfully detached principal " << principal << " from thing " << thingName << std::endl; } else { std::cerr << "Failed to detach principal " << principal << " from thing " << thingName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 DetachThingPrincipal AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 ListCertificates
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! List certificates registered in the AWS account making the call. /*! \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::listCertificates( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::ListCertificatesRequest request; Aws::Vector<Aws::IoT::Model::Certificate> allCertificates; Aws::String marker; // Used to paginate results. do { if (!marker.empty()) { request.SetMarker(marker); } Aws::IoT::Model::ListCertificatesOutcome outcome = iotClient.ListCertificates( request); if (outcome.IsSuccess()) { const Aws::IoT::Model::ListCertificatesResult &result = outcome.GetResult(); marker = result.GetNextMarker(); allCertificates.insert(allCertificates.end(), result.GetCertificates().begin(), result.GetCertificates().end()); } else { std::cerr << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!marker.empty()); std::cout << allCertificates.size() << " certificate(s) found." << std::endl; for (auto &certificate: allCertificates) { std::cout << "Certificate ID: " << certificate.GetCertificateId() << std::endl; std::cout << "Certificate ARN: " << certificate.GetCertificateArn() << std::endl; std::cout << std::endl; } return true; }
-
如需 API 詳細資訊,請參閱 ListCertificates AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 SearchIndex
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Query the AWS IoT fleet index. //! For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html /*! \param: query: The query string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::searchIndex(const Aws::String &query, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::SearchIndexRequest request; request.SetQueryString(query); Aws::Vector<Aws::IoT::Model::ThingDocument> allThingDocuments; Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::IoT::Model::SearchIndexOutcome outcome = iotClient.SearchIndex(request); if (outcome.IsSuccess()) { const Aws::IoT::Model::SearchIndexResult &result = outcome.GetResult(); allThingDocuments.insert(allThingDocuments.end(), result.GetThings().cbegin(), result.GetThings().cend()); nextToken = result.GetNextToken(); } else { std::cerr << "Error in SearchIndex: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); std::cout << allThingDocuments.size() << " thing document(s) found." << std::endl; for (const auto thingDocument: allThingDocuments) { std::cout << " Thing name: " << thingDocument.GetThingName() << "." << std::endl; } return true; }
-
如需 API 詳細資訊,請參閱 SearchIndex AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 UpdateIndexingConfiguration
。
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Update the indexing configuration. /*! \param thingIndexingConfiguration: A ThingIndexingConfiguration object which is ignored if not set. \param thingGroupIndexingConfiguration: A ThingGroupIndexingConfiguration object which is ignored if not set. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::updateIndexingConfiguration( const Aws::IoT::Model::ThingIndexingConfiguration &thingIndexingConfiguration, const Aws::IoT::Model::ThingGroupIndexingConfiguration &thingGroupIndexingConfiguration, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::UpdateIndexingConfigurationRequest request; if (thingIndexingConfiguration.ThingIndexingModeHasBeenSet()) { request.SetThingIndexingConfiguration(thingIndexingConfiguration); } if (thingGroupIndexingConfiguration.ThingGroupIndexingModeHasBeenSet()) { request.SetThingGroupIndexingConfiguration(thingGroupIndexingConfiguration); } Aws::IoT::Model::UpdateIndexingConfigurationOutcome outcome = iotClient.UpdateIndexingConfiguration( request); if (outcome.IsSuccess()) { std::cout << "UpdateIndexingConfiguration succeeded." << std::endl; } else { std::cerr << "UpdateIndexingConfiguration failed." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 UpdateIndexingConfiguration AWS SDK for C++ 參考中的 API。
-
下列程式碼範例示範如何使用 UpdateThing
。
- C++ 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 UpdateThing AWS SDK for C++ 參考中的 API。
-