Search in sources :

Example 1 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project udmi by faucetsdn.

the class CloudIotManager method fetchDeviceList.

public Set<String> fetchDeviceList() {
    Preconditions.checkNotNull(cloudIotService, "CloudIoT service not initialized");
    Set<Device> allDevices = new HashSet<>();
    String nextPageToken = null;
    try {
        do {
            ListDevicesResponse response = cloudIotRegistries.devices().list(getRegistryPath()).setPageToken(nextPageToken).setPageSize(LIST_PAGE_SIZE).execute();
            List<Device> devices = response.getDevices();
            allDevices.addAll(devices == null ? ImmutableList.of() : devices);
            System.err.printf("Retrieved %d devices from registry...%n", allDevices.size());
            nextPageToken = response.getNextPageToken();
        } while (nextPageToken != null);
        return allDevices.stream().map(Device::getId).collect(Collectors.toSet());
    } catch (Exception e) {
        throw new RuntimeException("While listing devices for registry " + registryId, e);
    }
}
Also used : Device(com.google.api.services.cloudiot.v1.model.Device) ListDevicesResponse(com.google.api.services.cloudiot.v1.model.ListDevicesResponse) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 2 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project udmi by faucetsdn.

the class CloudIotManager method setDeviceConfig.

public void setDeviceConfig(String deviceId, String data) {
    try {
        final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, iotConfig.cloud_region, iotConfig.registry_id);
        final String devicePath = registryPath + "/devices/" + deviceId;
        ModifyCloudToDeviceConfigRequest req = new ModifyCloudToDeviceConfigRequest();
        String encPayload = Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8.name()));
        req.setBinaryData(encPayload);
        cloudIot.projects().locations().registries().devices().modifyCloudToDeviceConfig(devicePath, req).execute();
    } catch (Exception e) {
        throw new RuntimeException("While setting device config for " + deviceId, e);
    }
}
Also used : ModifyCloudToDeviceConfigRequest(com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 3 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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 Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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 5 with Devices

use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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

HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)20 JsonFactory (com.google.api.client.json.JsonFactory)20 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)20 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)20 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)20 Device (com.google.api.services.cloudiot.v1.model.Device)15 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)7 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)5 File (java.io.File)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 GatewayConfig (com.google.api.services.cloudiot.v1.model.GatewayConfig)3 ListDevicesResponse (com.google.api.services.cloudiot.v1.model.ListDevicesResponse)3 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)3 IOException (java.io.IOException)3 Base64 (java.util.Base64)3 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 HashSet (java.util.HashSet)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 Devices (com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices)1