use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method sendCommand.
// [END iot_set_iam_policy]
/**
* Send a command to a device. *
*/
// [START iot_send_command]
protected static void sendCommand(String deviceId, String projectId, String cloudRegion, String registryName, String data) 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);
SendCommandToDeviceRequest req = new SendCommandToDeviceRequest();
// Data sent through the wire has to be base64 encoded.
Base64.Encoder encoder = Base64.getEncoder();
String encPayload = encoder.encodeToString(data.getBytes(StandardCharsets.UTF_8.name()));
req.setBinaryData(encPayload);
System.out.printf("Sending command to %s%n", devicePath);
service.projects().locations().registries().devices().sendCommandToDevice(devicePath, req).execute();
System.out.println("Command response: sent");
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices 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. *
*/
protected static List<DeviceState> getDeviceStates(String deviceId, 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);
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.Projects.Locations.Registries.Devices in project java-docs-samples by GoogleCloudPlatform.
the class CloudiotPubsubExampleServer method deleteDevice.
/**
* Delete this device from Cloud IoT.
*/
public static void deleteDevice(String deviceId, 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);
System.out.println("Deleting device " + devicePath);
service.projects().locations().registries().devices().delete(devicePath).execute();
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project udmi by faucetsdn.
the class LocalDevice method loadCredentials.
public void loadCredentials() {
try {
deviceCredentials.clear();
if (hasGateway() && hasAuthType()) {
throw new RuntimeException("Proxied devices should not have cloud.auth_type defined");
}
if (!isDirectConnect()) {
return;
}
if (!hasAuthType()) {
throw new RuntimeException("Credential cloud.auth_type definition missing");
}
String authType = getAuthType();
Set<String> keyFiles = (authType.equals(ES_CERT_TYPE) || authType.equals(RSA_CERT_TYPE)) ? ALL_CERT_FILES : ALL_KEY_FILES;
for (String keyFile : keyFiles) {
DeviceCredential deviceCredential = getDeviceCredential(keyFile);
if (deviceCredential != null) {
deviceCredentials.add(deviceCredential);
}
}
int numCredentials = deviceCredentials.size();
if (numCredentials == 0 || numCredentials > 3) {
throw new RuntimeException(String.format("Found %d credentials", numCredentials));
}
} catch (Exception e) {
throw new RuntimeException("While loading credentials for local device " + deviceId, e);
}
}
use of com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices in project udmi by faucetsdn.
the class CloudIotManager method listDevices.
Set<String> listDevices() {
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, iotConfig.cloud_region, iotConfig.registry_id);
try {
Set<String> deviceSet = new HashSet<>();
String nextPageToken = null;
do {
LOG.info("Listing devices for " + registryPath);
Devices.List listRequest = cloudIot.projects().locations().registries().devices().list(registryPath);
if (nextPageToken != null) {
listRequest.setPageToken(nextPageToken);
}
ListDevicesResponse listDevicesResponse = listRequest.execute();
List<Device> devices = listDevicesResponse.getDevices();
if (devices == null) {
throw new RuntimeException("Devices list is empty");
}
devices.forEach(device -> deviceSet.add(device.getId()));
nextPageToken = listDevicesResponse.getNextPageToken();
} while (nextPageToken != null);
return deviceSet;
} catch (Exception e) {
throw new RuntimeException("While fetching devices from " + registryPath, e);
}
}
Aggregations