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