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();
}
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());
}
Aggregations