Search in sources :

Example 1 with DeviceRegistry

use of com.google.api.services.cloudiot.v1.model.DeviceRegistry in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createRegistry.

// [START iot_create_registry]
/**
 * Create a registry for Cloud IoT.
 */
public static void createRegistry(String cloudRegion, String projectId, String registryName, String pubsubTopicPath) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
    final String fullPubsubPath = "projects/" + projectId + "/topics/" + pubsubTopicPath;
    DeviceRegistry registry = new DeviceRegistry();
    EventNotificationConfig notificationConfig = new EventNotificationConfig();
    notificationConfig.setPubsubTopicName(fullPubsubPath);
    List<EventNotificationConfig> notificationConfigs = new ArrayList<EventNotificationConfig>();
    notificationConfigs.add(notificationConfig);
    registry.setEventNotificationConfigs(notificationConfigs);
    registry.setId(registryName);
    DeviceRegistry reg = service.projects().locations().registries().create(projectPath, registry).execute();
    System.out.println("Created registry: " + reg.getName());
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceRegistry(com.google.api.services.cloudiot.v1.model.DeviceRegistry) JsonFactory(com.google.api.client.json.JsonFactory) ArrayList(java.util.ArrayList) EventNotificationConfig(com.google.api.services.cloudiot.v1.model.EventNotificationConfig) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 2 with DeviceRegistry

use of com.google.api.services.cloudiot.v1.model.DeviceRegistry in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method getRegistry.

// [END iot_get_device_state]
// [START iot_get_registry]
/**
 * Retrieves registry metadata from a project. *
 */
public static DeviceRegistry getRegistry(String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(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);
    return service.projects().locations().registries().get(registryPath).execute();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 3 with DeviceRegistry

use of com.google.api.services.cloudiot.v1.model.DeviceRegistry in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method listRegistries.

// [END iot_get_device_configs]
// [START iot_list_registries]
/**
 * Lists all of the registries associated with the given project.
 */
public static void listRegistries(String projectId, String cloudRegion) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
    List<DeviceRegistry> registries = service.projects().locations().registries().list(projectPath).execute().getDeviceRegistries();
    if (registries != null) {
        System.out.println("Found " + registries.size() + " registries");
        for (DeviceRegistry r : registries) {
            System.out.println("Id: " + r.getId());
            System.out.println("Name: " + r.getName());
            if (r.getMqttConfig() != null) {
                System.out.println("Config: " + r.getMqttConfig().toPrettyString());
            }
            System.out.println();
        }
    } else {
        System.out.println("Project has no registries.");
    }
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) DeviceRegistry(com.google.api.services.cloudiot.v1.model.DeviceRegistry) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 JsonFactory (com.google.api.client.json.JsonFactory)3 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)3 DeviceRegistry (com.google.api.services.cloudiot.v1.model.DeviceRegistry)2 EventNotificationConfig (com.google.api.services.cloudiot.v1.model.EventNotificationConfig)1 ArrayList (java.util.ArrayList)1