public AwsIot(Node root, String region, String accessKeyId, String secretAccessKey, KeyStore keyStore, String keyPassword) { AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey); this.client = new AWSIotClient(awsCredentials); this.client.withRegion(Regions.fromName(region)); String endpoint = getEndpoint(); String clientId = UUID.randomUUID().toString(); if (keyStore != null && keyPassword != null) { this.mqttClient = new AWSIotMqttClient(endpoint, clientId, keyStore, keyPassword); } else { this.mqttClient = new AWSIotMqttClient(endpoint, clientId, accessKeyId, secretAccessKey); } try { this.mqttClient.connect(); } catch (AWSIotException e) { throw new RuntimeException("Failed to connect to AWS IoT service", e); } this.root = root; }
@Test public void getAwsClients() throws Exception { // first check if client is constructed if no one is given final AWSIotStateHandler handler2 = new AWSIotStateHandler(session); assertNotNull(handler2.getAwsClient()); assertNotNull(handler2.getAwsDataClient()); // check if the client given is the client returned final AWSIot iotClient = new AWSIotClient(); final AWSIotData iotData = new AWSIotDataClient(); final AWSIotStateHandler handler3 = new AWSIotStateHandler(session, iotClient, iotData); assertNotNull(handler3.getAwsClient()); assertNotNull(handler3.getAwsDataClient()); assertEquals(iotClient, handler3.getAwsClient()); assertEquals(iotData, handler3.getAwsDataClient()); }
@Override public AWSIotStateHandler givenHandler() { // credentials need to be set in local environment // see http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html return new AWSIotStateHandler(session, new AWSIotClient(), new AWSIotDataClient()); }
/** * AWSIoTサーバにログインします. * * @param accessKey アクセスキー * @param secretKey シークレットキー * @param region リージョン */ public void login(final String accessKey, final String secretKey, final Regions region, final LoginCallback callback) { if (accessKey == null || secretKey == null || region == null) { if (callback != null) { callback.onLogin(new RuntimeException("Arguments is null.")); } return; } BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); mCredentialsProvider = new StaticCredentialsProvider(credentials); mIotClient = new AWSIotClient(mCredentialsProvider); mIotClient.setRegion(Region.getRegion(region)); mIotDataClient = new AWSIotDataClient(mCredentialsProvider); new DescribeEndpointTask() { @Override protected void onPostExecute(final AsyncTaskResult<String> result) { Exception exception = null; if (result.getError() == null) { JSONObject json; try { json = new JSONObject(result.getResult()); if (json.has(END_POINT_ADDRESS)) { String endpoint = json.getString(END_POINT_ADDRESS); mAWSIotEndPoint = endpoint; mIotDataClient.setEndpoint(endpoint); for (OnAWSIotEventListener l : mOnAWSIotEventListeners) { l.onLogin(); } connectMQTT(); } else { exception = new Exception("Not found endpointAddress."); } } catch (JSONException e) { exception = e; } } else { exception = result.getError(); } callback.onLogin(exception); } }.execute(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); keystorePath = getFilesDir().getPath(); // Initialize the AWS Cognito credentials provider credentialsProvider = new CognitoCachingCredentialsProvider( getApplicationContext(), // context COGNITO_POOL_ID, // Identity Pool ID MY_REGION // Region ); iotClient = new AWSIotClient(credentialsProvider); iotClient.setRegion(Region.getRegion(MY_REGION)); btnCreateCertificate = (Button) findViewById(R.id.btnCreateCertificate); btnCreateCertificate.setEnabled(false); tvCertificateStatus = (TextView) findViewById(R.id.tvCertificateStatus); // To load cert/key from keystore on filesystem try { if (AWSIotKeystoreHelper.isKeystorePresent(keystorePath, KEYSTORE_NAME)) { Log.i(LOG_TAG, "Keystore " + KEYSTORE_NAME + " found in " + keystorePath + "."); if (AWSIotKeystoreHelper.keystoreContainsAlias(CERTIFICATE_ID, keystorePath, KEYSTORE_NAME, KEYSTORE_PASSWORD)) { Log.i(LOG_TAG, "Certificate and key found in keystore."); tvCertificateStatus.setText("Certificate and key found in keystore."); btnCreateCertificate.setEnabled(false); } else { Log.i(LOG_TAG, "Certificate and key NOT found in keystore."); tvCertificateStatus .setText("Certificate and key NOT found in keystore. Click to create one with a CSR."); btnCreateCertificate.setEnabled(true); } } else { Log.i(LOG_TAG, "Keystore " + KEYSTORE_NAME + " NOT found in " + keystorePath + "."); tvCertificateStatus.setText("Keystore " + KEYSTORE_NAME + " NOT found in " + keystorePath + ". Click to create a certificate with a CSR and store in keystore."); btnCreateCertificate.setEnabled(true); } } catch (Exception e) { Log.e(LOG_TAG, "An error occurred accessing the keystore and retrieving cert/key.", e); } }