リモートデバイスの設定と IoT エージェントの使用 - AWS IoT Core

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

リモートデバイスの設定と IoT エージェントの使用

IoT エージェントは、クライアントアクセストークンを含む MQTT メッセージを受信し、リモートデバイスでローカルプロキシを起動するために使用されます。MQTT を使用してセキュアトンネリングでクライアントアクセストークンを配信する場合は、リモートデバイスに IoT エージェントをインストールして実行する必要があります。IoT エージェントは、次の予約済み IoT MQTT トピックにサブスクライブする必要があります。

注記

予約済み MQTT トピックへのサブスクライブ以外の方法で送信先クライアントアクセストークンをリモートデバイスに配信する場合は、送信先クライアントアクセストークン (CAT) リスナーおよびローカルプロキシが必要になることがあります。CAT リスナーは、選択したクライアントアクセストークン配信メカニズムを使用して、送信先モードでローカルプロキシを起動できる必要があります。

IoT エージェントスニペット

IoT エージェントは、次の予約済み IoT MQTT トピックにサブスクライブする必要があります。これにより、MQTT メッセージを受信してローカルプロキシを起動できます。

$aws/things/thing-name/tunnels/notify

Where thing-name AWS IoT はリモートデバイスに関連付けられているモノの名前です。

MQTT メッセージペイロードの例を次に示します。

{ "clientAccessToken": "destination-client-access-token", "clientMode": "destination", "region": "aws-region", "services": ["destination-service"] }

MQTT メッセージを受信した後、IoT エージェントは適切なパラメータを使用してリモートデバイスでローカルプロキシを起動する必要があります。

次の Java コードは、AWS IoT デバイス SDKProcessBuilderJava ライブラリを使用して、安全なトンネリングで動作する簡単な IoT エージェントを構築する方法を示しています。

// Find the IoT device endpoint for your AWS アカウント final String endpoint = iotClient.describeEndpoint(new DescribeEndpointRequest().withEndpointType("iot:Data-ATS")).getEndpointAddress(); // Instantiate the IoT Agent with your AWS credentials final String thingName = "RemoteDeviceA"; final String tunnelNotificationTopic = String.format("$aws/things/%s/tunnels/notify", thingName); final AWSIotMqttClient mqttClient = new AWSIotMqttClient(endpoint, thingName, "your_aws_access_key", "your_aws_secret_key"); try { mqttClient.connect(); final TunnelNotificationListener listener = new TunnelNotificationListener(tunnelNotificationTopic); mqttClient.subscribe(listener, true); } finally { mqttClient.disconnect(); } private static class TunnelNotificationListener extends AWSIotTopic { public TunnelNotificationListener(String topic) { super(topic); } @Override public void onMessage(AWSIotMessage message) { try { // Deserialize the MQTT message final JSONObject json = new JSONObject(message.getStringPayload()); final String accessToken = json.getString("clientAccessToken"); final String region = json.getString("region"); final String clientMode = json.getString("clientMode"); if (!clientMode.equals("destination")) { throw new RuntimeException("Client mode " + clientMode + " in the MQTT message is not expected"); } final JSONArray servicesArray = json.getJSONArray("services"); if (servicesArray.length() > 1) { throw new RuntimeException("Services in the MQTT message has more than 1 service"); } final String service = servicesArray.get(0).toString(); if (!service.equals("SSH")) { throw new RuntimeException("Service " + service + " is not supported"); } // Start the destination local proxy in a separate process to connect to the SSH Daemon listening port 22 final ProcessBuilder pb = new ProcessBuilder("localproxy", "-t", accessToken, "-r", region, "-d", "localhost:22"); pb.start(); } catch (Exception e) { log.error("Failed to start the local proxy", e); } } }