AWS IoT SDK for Java 2.x を使用した の例 - AWS SDK for Java 2.x

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS IoT SDK for Java 2.x を使用した の例

次のコード例は、 AWS SDK for Java 2.x で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています AWS IoT。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。ここでは GitHub、コンテキスト内でコードを設定および実行する方法の手順を確認できます。

開始方法

次のコード例は、 AWS IoTの使用を開始する方法を示しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iot.IotClient; import software.amazon.awssdk.services.iot.model.ListThingsRequest; import software.amazon.awssdk.services.iot.model.ListThingsResponse; import software.amazon.awssdk.services.iot.model.ThingAttribute; import java.util.List; public class HelloIoT { public static void main(String[] args) { System.out.println("Hello AWS IoT. Here is a listing of your AWS IoT Things:"); IotClient iotClient = IotClient.builder() .region(Region.US_EAST_1) .build(); listAllThings(iotClient); } public static void listAllThings( IotClient iotClient) { ListThingsRequest thingsRequest = ListThingsRequest.builder() .maxResults(10) .build(); ListThingsResponse response = iotClient.listThings(thingsRequest) ; List<ThingAttribute> thingList = response.things(); for (ThingAttribute attribute : thingList) { System.out.println("Thing name: "+attribute.thingName()); System.out.println("Thing ARN: "+attribute.thingArn()); } } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「listThings」を参照してください。

アクション

次の例は、AttachThingPrincipal を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void attachCertificateToThing(IotClient iotClient, String thingName, String certificateArn) { // Attach the certificate to the thing. AttachThingPrincipalRequest principalRequest = AttachThingPrincipalRequest.builder() .thingName(thingName) .principal(certificateArn) .build(); AttachThingPrincipalResponse attachResponse = iotClient.attachThingPrincipal(principalRequest); // Verify the attachment was successful. if (attachResponse.sdkHttpResponse().isSuccessful()) { System.out.println("Certificate attached to Thing successfully."); // Print additional information about the Thing. describeThing(iotClient, thingName); } else { System.err.println("Failed to attach certificate to Thing. HTTP Status Code: " + attachResponse.sdkHttpResponse().statusCode()); } }
  • API の詳細については、「 API リファレンスAttachThingPrincipal」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、CreateKeysAndCertificate を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static String createCertificate(IotClient iotClient) { try { CreateKeysAndCertificateResponse response = iotClient.createKeysAndCertificate(); String certificatePem = response.certificatePem(); String certificateArn = response.certificateArn(); // Print the details. System.out.println("\nCertificate:"); System.out.println(certificatePem); System.out.println("\nCertificate ARN:"); System.out.println(certificateArn); return certificateArn; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • API の詳細については、「 API リファレンスCreateKeysAndCertificate」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、CreateThing を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void createIoTThing(IotClient iotClient, String thingName) { try { CreateThingRequest createThingRequest = CreateThingRequest.builder() .thingName(thingName) .build(); CreateThingResponse createThingResponse = iotClient.createThing(createThingRequest); System.out.println(thingName +" was successfully created. The ARN value is " + createThingResponse.thingArn()); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスCreateThing」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、CreateTopicRule を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void createIoTRule(IotClient iotClient, String roleARN, String ruleName, String action) { try { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); // Create the rule. iotClient.createTopicRule(topicRuleRequest); System.out.println("IoT Rule created successfully."); } catch (IotException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスCreateTopicRule」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DeleteCertificate を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void deleteCertificate(IotClient iotClient, String certificateArn ) { DeleteCertificateRequest certificateProviderRequest = DeleteCertificateRequest.builder() .certificateId(extractCertificateId(certificateArn)) .build(); iotClient.deleteCertificate(certificateProviderRequest); System.out.println(certificateArn +" was successfully deleted."); }
  • API の詳細については、「 API リファレンスDeleteCertificate」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DeleteThing を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void deleteIoTThing(IotClient iotClient, String thingName) { try { DeleteThingRequest deleteThingRequest = DeleteThingRequest.builder() .thingName(thingName) .build(); iotClient.deleteThing(deleteThingRequest); System.out.println("Deleted Thing " + thingName); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDeleteThing」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DescribeEndpoint を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static String describeEndpoint(IotClient iotClient) { try { DescribeEndpointResponse endpointResponse = iotClient.describeEndpoint(DescribeEndpointRequest.builder().build()); // Get the endpoint URL. String endpointUrl = endpointResponse.endpointAddress(); String exString = getValue(endpointUrl); String fullEndpoint = "https://"+exString+"-ats.iot.us-east-1.amazonaws.com"; System.out.println("Full Endpoint URL: "+fullEndpoint); return fullEndpoint; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return "" ; }
  • API の詳細については、「 API リファレンスDescribeEndpoint」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DescribeThing を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

private static void describeThing(IotClient iotClient, String thingName) { try { DescribeThingRequest thingRequest = DescribeThingRequest.builder() .thingName(thingName) .build() ; // Print Thing details. DescribeThingResponse describeResponse = iotClient.describeThing(thingRequest); System.out.println("Thing Details:"); System.out.println("Thing Name: " + describeResponse.thingName()); System.out.println("Thing ARN: " + describeResponse.thingArn()); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDescribeThing」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DetachThingPrincipal を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void detachThingPrincipal(IotClient iotClient, String thingName, String certificateArn){ try { DetachThingPrincipalRequest thingPrincipalRequest = DetachThingPrincipalRequest.builder() .principal(certificateArn) .thingName(thingName) .build(); iotClient.detachThingPrincipal(thingPrincipalRequest); System.out.println(certificateArn +" was successfully removed from " +thingName); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDetachThingPrincipal」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、ListCertificates を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void listCertificates(IotClient iotClient) { ListCertificatesResponse response = iotClient.listCertificates(); List<Certificate> certList = response.certificates(); for (Certificate cert : certList) { System.out.println("Cert id: " + cert.certificateId()); System.out.println("Cert Arn: " + cert.certificateArn()); } }
  • API の詳細については、「 API リファレンスListCertificates」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、SearchIndex を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void searchThings(IotClient iotClient, String queryString){ SearchIndexRequest searchIndexRequest = SearchIndexRequest.builder() .queryString(queryString) .build(); try { // Perform the search and get the result. SearchIndexResponse searchIndexResponse = iotClient.searchIndex(searchIndexRequest); // Process the result. if (searchIndexResponse.things().isEmpty()) { System.out.println("No things found."); } else { searchIndexResponse.things().forEach(thing -> System.out.println("Thing id found using search is " + thing.thingId())); } } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスSearchIndex」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、UpdateThing を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void updateThing(IotClient iotClient, String thingName) { // Specify the new attribute values. String newLocation = "Office"; String newFirmwareVersion = "v2.0"; Map<String, String> attMap = new HashMap<>(); attMap.put("location", newLocation); attMap.put("firmwareVersion", newFirmwareVersion); AttributePayload attributePayload = AttributePayload.builder() .attributes(attMap) .build(); UpdateThingRequest updateThingRequest = UpdateThingRequest.builder() .thingName(thingName) .attributePayload(attributePayload) .build(); try { // Update the IoT Thing attributes. iotClient.updateThing(updateThingRequest); System.out.println("Thing attributes updated successfully."); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスUpdateThing」の「」を参照してください。 AWS SDK for Java 2.x

シナリオ

次のコード例は、 AWS IoT SDK を使用して AWS IoT デバイス管理のユースケースを操作する方法を示しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iot.IotClient; import software.amazon.awssdk.services.iot.model.Action; import software.amazon.awssdk.services.iot.model.AttachThingPrincipalRequest; import software.amazon.awssdk.services.iot.model.AttachThingPrincipalResponse; import software.amazon.awssdk.services.iot.model.AttributePayload; import software.amazon.awssdk.services.iot.model.Certificate; import software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse; import software.amazon.awssdk.services.iot.model.CreateThingRequest; import software.amazon.awssdk.services.iot.model.CreateTopicRuleRequest; import software.amazon.awssdk.services.iot.model.DeleteCertificateRequest; import software.amazon.awssdk.services.iot.model.CreateThingResponse; import software.amazon.awssdk.services.iot.model.DeleteThingRequest; import software.amazon.awssdk.services.iot.model.DescribeEndpointRequest; import software.amazon.awssdk.services.iot.model.DescribeEndpointResponse; import software.amazon.awssdk.services.iot.model.DescribeThingRequest; import software.amazon.awssdk.services.iot.model.DescribeThingResponse; import software.amazon.awssdk.services.iot.model.DetachThingPrincipalRequest; import software.amazon.awssdk.services.iot.model.IotException; import software.amazon.awssdk.services.iot.model.ListCertificatesResponse; import software.amazon.awssdk.services.iot.model.ListTopicRulesRequest; import software.amazon.awssdk.services.iot.model.ListTopicRulesResponse; import software.amazon.awssdk.services.iot.model.SearchIndexRequest; import software.amazon.awssdk.services.iot.model.SearchIndexResponse; import software.amazon.awssdk.services.iot.model.SnsAction; import software.amazon.awssdk.services.iot.model.TopicRuleListItem; import software.amazon.awssdk.services.iot.model.TopicRulePayload; import software.amazon.awssdk.services.iot.model.UpdateThingRequest; import software.amazon.awssdk.services.iotdataplane.IotDataPlaneClient; import software.amazon.awssdk.services.iotdataplane.model.GetThingShadowRequest; import software.amazon.awssdk.services.iotdataplane.model.GetThingShadowResponse; import software.amazon.awssdk.services.iotdataplane.model.UpdateThingShadowRequest; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * This Java example performs these tasks: * * 1. Creates an AWS IoT Thing. * 2. Generate and attach a device certificate. * 3. Update an AWS IoT Thing with Attributes. * 4. Get an AWS IoT Endpoint. * 5. List your certificates. * 6. Updates the shadow for the specified thing.. * 7. Write out the state information, in JSON format * 8. Creates a rule * 9. List rules * 10. Search things * 11. Detach amd delete the certificate. * 12. Delete Thing. */ public class IotScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); private static final String TOPIC = "your-iot-topic"; public static void main(String[] args) { final String usage = """ Usage: <roleARN> <snsAction> Where: roleARN - The ARN of an IAM role that has permission to work with AWS IOT. snsAction - An ARN of an SNS topic. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String thingName; String ruleName; String roleARN = args[0]; String snsAction = args[1]; Scanner scanner = new Scanner(System.in); IotClient iotClient = IotClient.builder() .region(Region.US_EAST_1) .build(); System.out.println(DASHES); System.out.println("Welcome to the AWS IoT example workflow."); System.out.println(""" This example program demonstrates various interactions with the AWS Internet of Things (IoT) Core service. The program guides you through a series of steps, including creating an IoT Thing, generating a device certificate, updating the Thing with attributes, and so on. It utilizes the AWS SDK for Java V2 and incorporates functionality for creating and managing IoT Things, certificates, rules, shadows, and performing searches. The program aims to showcase AWS IoT capabilities and provides a comprehensive example for developers working with AWS IoT in a Java environment. """); System.out.print("Press Enter to continue..."); scanner.nextLine(); System.out.println(DASHES); System.out.println(DASHES); System.out.println("1. Create an AWS IoT Thing."); System.out.println(""" An AWS IoT Thing represents a virtual entity in the AWS IoT service that can be associated with a physical device. """); // Prompt the user for input. System.out.print("Enter Thing name: "); thingName = scanner.nextLine(); createIoTThing(iotClient, thingName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. Generate a device certificate."); System.out.println(""" A device certificate performs a role in securing the communication between devices (Things) and the AWS IoT platform. """); System.out.print("Do you want to create a certificate for " +thingName +"? (y/n)"); String certAns = scanner.nextLine(); String certificateArn="" ; if (certAns != null && certAns.trim().equalsIgnoreCase("y")) { certificateArn = createCertificate(iotClient); System.out.println("Attach the certificate to the AWS IoT Thing."); attachCertificateToThing(iotClient, thingName, certificateArn); } else { System.out.println("A device certificate was not created."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Update an AWS IoT Thing with Attributes."); System.out.println(""" IoT Thing attributes, represented as key-value pairs, offer a pivotal advantage in facilitating efficient data management and retrieval within the AWS IoT ecosystem. """); System.out.print("Press Enter to continue..."); scanner.nextLine(); updateThing(iotClient, thingName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Return a unique endpoint specific to the Amazon Web Services account."); System.out.println(""" An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point for communication between IoT devices and the AWS IoT service. """); System.out.print("Press Enter to continue..."); scanner.nextLine(); String endpointUrl = describeEndpoint(iotClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. List your AWS IoT certificates"); System.out.print("Press Enter to continue..."); scanner.nextLine(); if (certificateArn.length() > 0) { listCertificates(iotClient); } else { System.out.println("You did not create a certificates. Skipping this step."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Create an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device"); System.out.println(""" A Thing Shadow refers to a feature that enables you to create a virtual representation, or "shadow," of a physical device or thing. The Thing Shadow allows you to synchronize and control the state of a device between the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a Thing Shadow. """); System.out.print("Press Enter to continue..."); scanner.nextLine(); IotDataPlaneClient iotPlaneClient = IotDataPlaneClient.builder() .region(Region.US_EAST_1) .endpointOverride(URI.create(endpointUrl)) .build(); updateShadowThing(iotPlaneClient, thingName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Write out the state information, in JSON format."); System.out.print("Press Enter to continue..."); scanner.nextLine(); getPayload(iotPlaneClient, thingName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("8. Creates a rule"); System.out.println(""" Creates a rule that is an administrator-level action. Any user who has permission to create rules will be able to access data processed by the rule. """); System.out.print("Enter Rule name: "); ruleName = scanner.nextLine(); createIoTRule(iotClient, roleARN, ruleName, snsAction); System.out.println(DASHES); System.out.println(DASHES); System.out.println("9. List your rules."); System.out.print("Press Enter to continue..."); scanner.nextLine(); listIoTRules(iotClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Search things using the Thing name."); System.out.print("Press Enter to continue..."); scanner.nextLine(); String queryString = "thingName:"+thingName ; searchThings(iotClient, queryString); System.out.println(DASHES); System.out.println(DASHES); if (certificateArn.length() > 0) { System.out.print("Do you want to detach and delete the certificate for " +thingName +"? (y/n)"); String delAns = scanner.nextLine(); if (delAns != null && delAns.trim().equalsIgnoreCase("y")) { System.out.println("11. You selected to detach amd delete the certificate."); System.out.print("Press Enter to continue..."); scanner.nextLine(); detachThingPrincipal(iotClient, thingName, certificateArn); deleteCertificate(iotClient, certificateArn); } else { System.out.println("11. You selected not to delete the certificate."); } } else { System.out.println("11. You did not create a certificate so there is nothing to delete."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Delete the AWS IoT Thing."); System.out.print("Do you want to delete the IoT Thing? (y/n)"); String delAns = scanner.nextLine(); if (delAns != null && delAns.trim().equalsIgnoreCase("y")) { deleteIoTThing(iotClient, thingName); } else { System.out.println("The IoT Thing was not deleted."); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("The AWS IoT workflow has successfully completed."); System.out.println(DASHES); } public static void listCertificates(IotClient iotClient) { ListCertificatesResponse response = iotClient.listCertificates(); List<Certificate> certList = response.certificates(); for (Certificate cert : certList) { System.out.println("Cert id: " + cert.certificateId()); System.out.println("Cert Arn: " + cert.certificateArn()); } } public static void listIoTRules(IotClient iotClient) { try { ListTopicRulesRequest listTopicRulesRequest = ListTopicRulesRequest.builder().build(); ListTopicRulesResponse listTopicRulesResponse = iotClient.listTopicRules(listTopicRulesRequest); System.out.println("List of IoT Rules:"); List<TopicRuleListItem> ruleList = listTopicRulesResponse.rules(); for (TopicRuleListItem rule : ruleList) { System.out.println("Rule Name: " + rule.ruleName()); System.out.println("Rule ARN: " + rule.ruleArn()); System.out.println("--------------"); } } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void createIoTRule(IotClient iotClient, String roleARN, String ruleName, String action) { try { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); // Create the rule. iotClient.createTopicRule(topicRuleRequest); System.out.println("IoT Rule created successfully."); } catch (IotException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void getPayload(IotDataPlaneClient iotPlaneClient, String thingName) { try { GetThingShadowRequest getThingShadowRequest = GetThingShadowRequest.builder() .thingName(thingName) .build(); GetThingShadowResponse getThingShadowResponse = iotPlaneClient.getThingShadow(getThingShadowRequest); // Extracting payload from response. SdkBytes payload = getThingShadowResponse.payload(); String payloadString = payload.asUtf8String(); System.out.println("Received Shadow Data: " + payloadString); } catch (IotException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void updateShadowThing(IotDataPlaneClient iotPlaneClient, String thingName) { try { // Create Thing Shadow State Document. String stateDocument = "{\"state\":{\"reported\":{\"temperature\":25, \"humidity\":50}}}"; SdkBytes data= SdkBytes.fromString(stateDocument, StandardCharsets.UTF_8 ); UpdateThingShadowRequest updateThingShadowRequest = UpdateThingShadowRequest.builder() .thingName(thingName) .payload(data) .build(); // Update Thing Shadow. iotPlaneClient.updateThingShadow(updateThingShadowRequest); System.out.println("Thing Shadow updated successfully."); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void updateThing(IotClient iotClient, String thingName) { // Specify the new attribute values. String newLocation = "Office"; String newFirmwareVersion = "v2.0"; Map<String, String> attMap = new HashMap<>(); attMap.put("location", newLocation); attMap.put("firmwareVersion", newFirmwareVersion); AttributePayload attributePayload = AttributePayload.builder() .attributes(attMap) .build(); UpdateThingRequest updateThingRequest = UpdateThingRequest.builder() .thingName(thingName) .attributePayload(attributePayload) .build(); try { // Update the IoT Thing attributes. iotClient.updateThing(updateThingRequest); System.out.println("Thing attributes updated successfully."); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static String describeEndpoint(IotClient iotClient) { try { DescribeEndpointResponse endpointResponse = iotClient.describeEndpoint(DescribeEndpointRequest.builder().build()); // Get the endpoint URL. String endpointUrl = endpointResponse.endpointAddress(); String exString = getValue(endpointUrl); String fullEndpoint = "https://"+exString+"-ats.iot.us-east-1.amazonaws.com"; System.out.println("Full Endpoint URL: "+fullEndpoint); return fullEndpoint; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return "" ; } public static void detachThingPrincipal(IotClient iotClient, String thingName, String certificateArn){ try { DetachThingPrincipalRequest thingPrincipalRequest = DetachThingPrincipalRequest.builder() .principal(certificateArn) .thingName(thingName) .build(); iotClient.detachThingPrincipal(thingPrincipalRequest); System.out.println(certificateArn +" was successfully removed from " +thingName); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void deleteCertificate(IotClient iotClient, String certificateArn ) { DeleteCertificateRequest certificateProviderRequest = DeleteCertificateRequest.builder() .certificateId(extractCertificateId(certificateArn)) .build(); iotClient.deleteCertificate(certificateProviderRequest); System.out.println(certificateArn +" was successfully deleted."); } // Get the cert Id from the Cert ARN value. private static String extractCertificateId(String certificateArn) { // Example ARN: arn:aws:iot:region:account-id:cert/certificate-id. String[] arnParts = certificateArn.split(":"); String certificateIdPart = arnParts[arnParts.length - 1]; return certificateIdPart.substring(certificateIdPart.lastIndexOf("/") + 1); } public static String createCertificate(IotClient iotClient) { try { CreateKeysAndCertificateResponse response = iotClient.createKeysAndCertificate(); String certificatePem = response.certificatePem(); String certificateArn = response.certificateArn(); // Print the details. System.out.println("\nCertificate:"); System.out.println(certificatePem); System.out.println("\nCertificate ARN:"); System.out.println(certificateArn); return certificateArn; } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } public static void attachCertificateToThing(IotClient iotClient, String thingName, String certificateArn) { // Attach the certificate to the thing. AttachThingPrincipalRequest principalRequest = AttachThingPrincipalRequest.builder() .thingName(thingName) .principal(certificateArn) .build(); AttachThingPrincipalResponse attachResponse = iotClient.attachThingPrincipal(principalRequest); // Verify the attachment was successful. if (attachResponse.sdkHttpResponse().isSuccessful()) { System.out.println("Certificate attached to Thing successfully."); // Print additional information about the Thing. describeThing(iotClient, thingName); } else { System.err.println("Failed to attach certificate to Thing. HTTP Status Code: " + attachResponse.sdkHttpResponse().statusCode()); } } private static void describeThing(IotClient iotClient, String thingName) { try { DescribeThingRequest thingRequest = DescribeThingRequest.builder() .thingName(thingName) .build() ; // Print Thing details. DescribeThingResponse describeResponse = iotClient.describeThing(thingRequest); System.out.println("Thing Details:"); System.out.println("Thing Name: " + describeResponse.thingName()); System.out.println("Thing ARN: " + describeResponse.thingArn()); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void deleteIoTThing(IotClient iotClient, String thingName) { try { DeleteThingRequest deleteThingRequest = DeleteThingRequest.builder() .thingName(thingName) .build(); iotClient.deleteThing(deleteThingRequest); System.out.println("Deleted Thing " + thingName); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void createIoTThing(IotClient iotClient, String thingName) { try { CreateThingRequest createThingRequest = CreateThingRequest.builder() .thingName(thingName) .build(); CreateThingResponse createThingResponse = iotClient.createThing(createThingRequest); System.out.println(thingName +" was successfully created. The ARN value is " + createThingResponse.thingArn()); } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } private static String getValue(String input) { // Define a regular expression pattern for extracting the subdomain. Pattern pattern = Pattern.compile("^(.*?)\\.iot\\.us-east-1\\.amazonaws\\.com"); // Match the pattern against the input string. Matcher matcher = pattern.matcher(input); // Check if a match is found. if (matcher.find()) { // Extract the subdomain from the first capturing group. String subdomain = matcher.group(1); System.out.println("Extracted subdomain: " + subdomain); return subdomain ; } else { System.out.println("No match found"); } return "" ; } public static void searchThings(IotClient iotClient, String queryString){ SearchIndexRequest searchIndexRequest = SearchIndexRequest.builder() .queryString(queryString) .build(); try { // Perform the search and get the result. SearchIndexResponse searchIndexResponse = iotClient.searchIndex(searchIndexRequest); // Process the result. if (searchIndexResponse.things().isEmpty()) { System.out.println("No things found."); } else { searchIndexResponse.things().forEach(thing -> System.out.println("Thing id found using search is " + thing.thingId())); } } catch (IotException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }