Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS IoT SDK for C++ を使用する例
次のコード例は、 AWS SDK for C++ を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています AWS IoT。
「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。
開始方法
次のコード例は、 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 を参照してください。
注記
GitHub には他にもあります。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 -
基本
次のコード例は、 AWS IoT デバイス管理の操作方法を示しています。
- C++ のSDK
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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 を参照してください。
-