use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class CloudiotPubsubExampleServer method createDevice.
/**
* Create a device to bind to a gateway.
*/
public static void createDevice(String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException {
// [START create_device]
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> devices = service.projects().locations().registries().devices().list(registryPath).setFieldMask("config,gatewayConfig").execute().getDevices();
if (devices != null) {
System.out.println("Found " + devices.size() + " devices");
for (Device d : devices) {
if ((d.getId() != null && d.getId().equals(deviceId)) || (d.getName() != null && d.getName().equals(deviceId))) {
System.out.println("Device exists, skipping.");
return;
}
}
}
System.out.println("Creating device with id: " + deviceId);
Device device = new Device();
device.setId(deviceId);
GatewayConfig gwConfig = new GatewayConfig();
gwConfig.setGatewayType("NON_GATEWAY");
gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
device.setGatewayConfig(gwConfig);
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created device: " + createdDevice.toPrettyString());
// [END create_device]
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class CloudiotPubsubExampleServer method updateDeviceConfig.
/**
* Push the data to the given device as configuration.
*/
public void updateDeviceConfig(String projectId, String region, String registryId, String deviceId, JSONObject data) throws JSONException, UnsupportedEncodingException {
// Push the data to the given device as configuration.
JSONObject configData = new JSONObject();
System.out.println(String.format("Device %s has temperature of: %d", deviceId, data.getInt("temperature")));
if (data.getInt("temperature") < 0) {
// Turn off the fan
configData.put("fan_on", false);
System.out.println("Setting fan state for device " + deviceId + " to off.");
} else if (data.getInt("temperature") > 10) {
// Turn on the fan
configData.put("fan_on", true);
System.out.println("Setting fan state for device " + deviceId + " to on.");
} else {
// temperature is okay, don't need to push new config
return;
}
// Data sent through the wire has to be base64 encoded.
Base64.Encoder encoder = Base64.getEncoder();
String encPayload = encoder.encodeToString(configData.toString().getBytes("UTF-8"));
ModifyCloudToDeviceConfigRequest request = new ModifyCloudToDeviceConfigRequest();
request.setBinaryData(encPayload);
String deviceName = String.format("projects/%s/locations/%s/" + "registries/%s/devices/%s", projectId, region, registryId, deviceId);
try {
service.projects().locations().registries().devices().modifyCloudToDeviceConfig(deviceName, request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method listDevices.
// [END iot_clear_registry]
// [START iot_list_devices]
/**
* Print all of the devices in this registry to standard out.
*/
protected static void listDevices(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);
List<Device> devices = service.projects().locations().registries().devices().list(registryPath).execute().getDevices();
if (devices != null) {
System.out.println("Found " + devices.size() + " devices");
for (Device d : devices) {
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.getConfig().toPrettyString());
}
System.out.println();
}
} else {
System.out.println("Registry has no 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 createDevice.
/**
* Create a device to bind to a gateway.
*/
protected static void createDevice(String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException {
// [START iot_create_device]
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> devices = service.projects().locations().registries().devices().list(registryPath).setFieldMask("config,gatewayConfig").execute().getDevices();
if (devices != null) {
System.out.println("Found " + devices.size() + " devices");
for (Device d : devices) {
if ((d.getId() != null && d.getId().equals(deviceId)) || (d.getName() != null && d.getName().equals(deviceId))) {
System.out.println("Device exists, skipping.");
return;
}
}
}
System.out.println("Creating device with id: " + deviceId);
Device device = new Device();
device.setId(deviceId);
GatewayConfig gwConfig = new GatewayConfig();
gwConfig.setGatewayType("NON_GATEWAY");
gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
device.setGatewayConfig(gwConfig);
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created device: " + createdDevice.toPrettyString());
// [END iot_create_device]
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method clearRegistry.
// [END iot_delete_registry]
/**
* clearRegistry
*
* <ul>
* <li>Registries can't be deleted if they contain devices,
* <li>Gateways (a type of device) can't be deleted if they have bound devices
* <li>Devices can't be deleted if bound to gateways...
* </ul>
*
* To completely remove a registry, you must unbind all devices from gateways, then remove all
* devices in a registry before removing the registry. As pseudocode: <code>
* ForAll gateways
* ForAll devicesBoundToGateway
* unbindDeviceFromGateway
* ForAll devices
* Delete device by ID
* Delete registry
* </code>
*/
// [START iot_clear_registry]
protected static void clearRegistry(String cloudRegion, String projectId, 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);
CloudIot.Projects.Locations.Registries regAlias = service.projects().locations().registries();
CloudIot.Projects.Locations.Registries.Devices devAlias = regAlias.devices();
ListDevicesResponse listGatewaysRes = devAlias.list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute();
List<Device> gateways = listGatewaysRes.getDevices();
// Unbind all devices from all gateways
if (gateways != null) {
System.out.println("Found " + gateways.size() + " devices");
for (Device g : gateways) {
String gatewayId = g.getId();
System.out.println("Id: " + gatewayId);
ListDevicesResponse res = devAlias.list(registryPath).setGatewayListOptionsAssociationsGatewayId(gatewayId).execute();
List<Device> deviceNumIds = res.getDevices();
if (deviceNumIds != null) {
System.out.println("Found " + deviceNumIds.size() + " devices");
for (Device device : deviceNumIds) {
String deviceId = device.getId();
System.out.println(String.format("ID: %s", deviceId));
// Remove any bindings from the device
UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest();
request.setDeviceId(deviceId);
request.setGatewayId(gatewayId);
regAlias.unbindDeviceFromGateway(registryPath, request).execute();
}
} else {
System.out.println("Gateway has no bound devices.");
}
}
}
// Remove all devices from the regsitry
List<Device> devices = devAlias.list(registryPath).execute().getDevices();
if (devices != null) {
System.out.println("Found " + devices.size() + " devices");
for (Device d : devices) {
String deviceId = d.getId();
String devicePath = String.format("%s/devices/%s", registryPath, deviceId);
service.projects().locations().registries().devices().delete(devicePath).execute();
}
}
// Delete the registry
service.projects().locations().registries().delete(registryPath).execute();
}
Aggregations