Search in sources :

Example 1 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method listGateways.

protected static void listGateways(String projectId, String cloudRegion, String registryName) throws IOException, GeneralSecurityException {
    // [START iot_list_gateways]
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    List<Device> gateways = service.projects().locations().registries().devices().list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute().getDevices();
    if (gateways != null) {
        System.out.println("Found " + gateways.size() + " devices");
        for (Device d : gateways) {
            System.out.println("Id: " + d.getId());
            if (d.getConfig() != null) {
                // Note that this will show the device config in Base64 encoded format.
                System.out.println("Config: " + d.getGatewayConfig().toPrettyString());
            }
            System.out.println();
        }
    } else {
        System.out.println("Registry has no devices.");
    }
// [END iot_list_gateways]
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 2 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method patchRsa256ForAuth.

// [END iot_patch_es]
// [START iot_patch_rsa]
/**
 * Patch the device to add an RSA256 key for authentication.
 */
protected static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("RSA_X509_PEM");
    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);
    Device device = new Device();
    device.setCredentials(Collections.singletonList(devCredential));
    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 3 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method patchEs256ForAuth.

// [END iot_list_registries]
// [START iot_patch_es]
/**
 * Patch the device to add an ES256 key for authentication.
 */
protected static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("ES256_PEM");
    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);
    Device device = new Device();
    device.setCredentials(Collections.singletonList(devCredential));
    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 4 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createDeviceWithRs256.

// [END iot_create_es_device]
// [START iot_create_rsa_device]
/**
 * Create a device that is authenticated using RS256.
 */
protected static void createDeviceWithRs256(String deviceId, String certificateFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(certificateFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("RSA_X509_PEM");
    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);
    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    device.setCredentials(Collections.singletonList(devCredential));
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created device: " + createdDevice.toPrettyString());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) Device(com.google.api.services.cloudiot.v1.model.Device) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) File(java.io.File)

Example 5 with Device

use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createGateway.

/**
 * Create a gateway to bind devices to.
 */
protected static void createGateway(String projectId, String cloudRegion, String registryName, String gatewayId, String certificateFilePath, String algorithm) throws GeneralSecurityException, IOException {
    // [START iot_create_gateway]
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    System.out.println("Creating gateway with id: " + gatewayId);
    Device device = new Device();
    device.setId(gatewayId);
    GatewayConfig gwConfig = new GatewayConfig();
    gwConfig.setGatewayType("GATEWAY");
    gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
    String keyFormat = "RSA_X509_PEM";
    if ("ES256".equals(algorithm)) {
        keyFormat = "ES256_PEM";
    }
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    byte[] keyBytes = java.nio.file.Files.readAllBytes(Paths.get(certificateFilePath));
    publicKeyCredential.setKey(new String(keyBytes, StandardCharsets.US_ASCII));
    publicKeyCredential.setFormat(keyFormat);
    DeviceCredential deviceCredential = new DeviceCredential();
    deviceCredential.setPublicKey(publicKeyCredential);
    device.setGatewayConfig(gwConfig);
    device.setCredentials(Collections.singletonList(deviceCredential));
    Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
    System.out.println("Created gateway: " + createdDevice.toPrettyString());
// [END iot_create_gateway]
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Device(com.google.api.services.cloudiot.v1.model.Device) DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PublicKeyCredential(com.google.api.services.cloudiot.v1.model.PublicKeyCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) GatewayConfig(com.google.api.services.cloudiot.v1.model.GatewayConfig)

Aggregations

Device (com.google.api.services.cloudiot.v1.model.Device)21 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)13 JsonFactory (com.google.api.client.json.JsonFactory)13 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)13 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)13 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)13 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)13 Test (org.junit.jupiter.api.Test)9 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)7 Device (org.hl7.fhir.r4.model.Device)7 Practitioner (org.hl7.fhir.r4.model.Practitioner)7 Reference (org.hl7.fhir.r4.model.Reference)7 ArrayList (java.util.ArrayList)6 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)6 Turtle (org.hl7.fhir.dstu3.utils.formats.Turtle)6 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)6 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)5 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)5 Coding (org.hl7.fhir.r4.model.Coding)5 Identifier (org.hl7.fhir.r4.model.Identifier)5