Example usage for com.amazonaws.services.iot.model AttributePayload AttributePayload

List of usage examples for com.amazonaws.services.iot.model AttributePayload AttributePayload

Introduction

In this page you can find the example usage for com.amazonaws.services.iot.model AttributePayload AttributePayload.

Prototype

AttributePayload

Source Link

Usage

From source file:com.erudika.para.iot.AWSIoTService.java

License:Apache License

@Override
public Thing createThing(Thing thing) {
    if (thing == null || StringUtils.isBlank(thing.getName()) || StringUtils.isBlank(thing.getAppid())
            || existsThing(thing)) {/*from  w ww . j  a  v  a 2s  . c o  m*/
        return null;
    }
    thing.setId(Utils.getNewId());
    String id = cloudIDForThing(thing);
    String appid = thing.getAppid();

    // STEP 1: Create thing
    CreateThingResult resp1 = getClient().createThing(new CreateThingRequest().withThingName(id)
            .withAttributePayload(new AttributePayload().addAttributesEntry(Config._APPID, appid)));

    // STEP 2: Create certificate
    CreateKeysAndCertificateResult resp2 = getClient()
            .createKeysAndCertificate(new CreateKeysAndCertificateRequest().withSetAsActive(true));

    String accountId = getAccountIdFromARN(resp1.getThingArn());
    String policyString = (String) (thing.getDeviceMetadata().containsKey("policyJSON")
            ? thing.getDeviceMetadata().get("policyJSON")
            : getDefaultPolicyDocument(accountId, id));

    // STEP 3: Create policy
    getClient().createPolicy(
            new CreatePolicyRequest().withPolicyDocument(policyString).withPolicyName(id + "-Policy"));

    // STEP 4: Attach policy to certificate
    getClient().attachPrincipalPolicy(new AttachPrincipalPolicyRequest()
            .withPrincipal(resp2.getCertificateArn()).withPolicyName(id + "-Policy"));

    // STEP 5: Attach thing to certificate
    getClient().attachThingPrincipal(
            new AttachThingPrincipalRequest().withPrincipal(resp2.getCertificateArn()).withThingName(id));

    thing.getDeviceMetadata().remove("policyJSON");

    thing.setServiceBroker("AWS");
    thing.getDeviceMetadata().put("thingId", thing.getId());
    thing.getDeviceMetadata().put("thingName", id);
    thing.getDeviceMetadata().put("thingARN", resp1.getThingArn());
    thing.getDeviceMetadata().put("clientId", id);
    thing.getDeviceMetadata().put("clientCertId", resp2.getCertificateId());
    thing.getDeviceMetadata().put("clientCertARN", resp2.getCertificateArn());
    thing.getDeviceMetadata().put("clientCert", resp2.getCertificatePem());
    thing.getDeviceMetadata().put("privateKey", resp2.getKeyPair().getPrivateKey());
    thing.getDeviceMetadata().put("publicKey", resp2.getKeyPair().getPublicKey());
    thing.getDeviceMetadata().put("region", Config.AWS_REGION);
    thing.getDeviceMetadata().put("port", 8883);
    thing.getDeviceMetadata().put("host",
            getClient().describeEndpoint(new DescribeEndpointRequest()).getEndpointAddress());

    return thing;
}

From source file:io.klerch.alexa.state.handler.AWSIotStateHandler.java

License:Open Source License

private void createThing(final String thingName, final AlexaScope scope) {
    // only create thing if not already existing
    final AttributePayload attrPayload = new AttributePayload();
    // add thing name as attribute as well. this is how the handler queries for the thing from now on
    attrPayload.addAttributesEntry(thingAttributeName, thingName);
    // if scope is user an attribute saves the plain user id as it is encrypted in the thing name
    if (AlexaScope.USER.includes(scope)) {
        attrPayload.addAttributesEntry(thingAttributeUser, session.getUser().getUserId());
    }//from   ww w .  j av a2 s  .co  m
    // another thing attributes holds the Alexa application-id
    attrPayload.addAttributesEntry(thingAttributeApp, session.getApplication().getApplicationId());
    // now create the thing
    final CreateThingRequest request = new CreateThingRequest().withThingName(thingName)
            .withAttributePayload(attrPayload);
    awsClient.createThing(request);
    log.info(format("Thing '%1$s' is created in AWS IoT.", thingName));
}