Search in sources :

Example 21 with 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 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");
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) Base64(java.util.Base64) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) SendCommandToDeviceRequest(com.google.api.services.cloudiot.v1.model.SendCommandToDeviceRequest) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 22 with 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 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();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) ListDeviceStatesResponse(com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)

Example 23 with Devices

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();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 24 with Devices

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);
    }
}
Also used : DeviceCredential(com.google.api.services.cloudiot.v1.model.DeviceCredential) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) ValidationException(com.google.daq.mqtt.util.ValidationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 25 with Devices

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);
    }
}
Also used : ListDevicesResponse(com.google.api.services.cloudiot.v1.model.ListDevicesResponse) Device(com.google.api.services.cloudiot.v1.model.Device) Devices(com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HashSet(java.util.HashSet)

Aggregations

HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)20 JsonFactory (com.google.api.client.json.JsonFactory)20 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)20 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)20 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)20 Device (com.google.api.services.cloudiot.v1.model.Device)15 DeviceCredential (com.google.api.services.cloudiot.v1.model.DeviceCredential)7 PublicKeyCredential (com.google.api.services.cloudiot.v1.model.PublicKeyCredential)5 File (java.io.File)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 GatewayConfig (com.google.api.services.cloudiot.v1.model.GatewayConfig)3 ListDevicesResponse (com.google.api.services.cloudiot.v1.model.ListDevicesResponse)3 ModifyCloudToDeviceConfigRequest (com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest)3 IOException (java.io.IOException)3 Base64 (java.util.Base64)3 DeviceConfig (com.google.api.services.cloudiot.v1.model.DeviceConfig)2 HashSet (java.util.HashSet)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 Devices (com.google.api.services.cloudiot.v1.CloudIot.Projects.Locations.Registries.Devices)1