Search in sources :

Example 16 with CloudIot

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

the class DeviceRegistryExample method getDeviceStates.

// [END iot_get_device]
// [START iot_get_device_state]
/**
 * Retrieves device metadata from a registry. *
 */
public static List<DeviceState> getDeviceStates(String deviceId, 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 devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, deviceId);
    System.out.println("Retrieving device states " + devicePath);
    ListDeviceStatesResponse resp = service.projects().locations().registries().devices().states().list(devicePath).execute();
    return resp.getDeviceStates();
}
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) ListDeviceStatesResponse(com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)

Example 17 with CloudIot

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

the class DeviceRegistryExample method createDeviceWithNoAuth.

// [END iot_create_rsa_device]
// [START iot_create_unauth_device]
/**
 * Create a device that has no credentials.
 *
 * <p>This is a valid way to construct a device, however until it is patched with a credential the
 * device will not be able to connect to Cloud IoT.
 */
public static void createDeviceWithNoAuth(String deviceId, 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 = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;
    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    device.setCredentials(new ArrayList<DeviceCredential>());
    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) 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) 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)17 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)17 JsonFactory (com.google.api.client.json.JsonFactory)17 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)17 Device (com.google.api.services.cloudiot.v1.model.Device)6 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)5 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)4 File (java.io.File)4 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 DeviceRegistry (com.google.api.services.cloudiot.v1.model.DeviceRegistry)2 GetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest)2 ArrayList (java.util.ArrayList)2 EventNotificationConfig (com.google.api.services.cloudiot.v1.model.EventNotificationConfig)1 ListDeviceStatesResponse (com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)1 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)1 SetIamPolicyRequest (com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest)1 Binding (com.google.iam.v1.Binding)1 Base64 (java.util.Base64)1