Search in sources :

Example 1 with DeviceState

use of com.google.api.services.cloudiot.v1.model.DeviceState in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method main.

// [END iot_set_iam_policy]
/**
 * Entry poit for CLI.
 */
public static void main(String[] args) throws Exception {
    DeviceRegistryExampleOptions options = DeviceRegistryExampleOptions.fromFlags(args);
    if (options == null) {
        // Could not parse.
        return;
    }
    switch(options.command) {
        case "create-iot-topic":
            System.out.println("Create IoT Topic:");
            createIotTopic(options.projectId, options.pubsubTopic);
            break;
        case "create-es":
            System.out.println("Create ES Device:");
            createDeviceWithEs256(options.deviceId, options.ecPublicKeyFile, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "create-rsa":
            System.out.println("Create RSA Device:");
            createDeviceWithRs256(options.deviceId, options.rsaCertificateFile, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "create-unauth":
            System.out.println("Create Unauth Device");
            createDeviceWithNoAuth(options.deviceId, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "create-registry":
            System.out.println("Create registry");
            createRegistry(options.cloudRegion, options.projectId, options.registryName, options.pubsubTopic);
            break;
        case "delete-device":
            System.out.println("Delete device");
            deleteDevice(options.deviceId, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "delete-registry":
            System.out.println("Delete registry");
            deleteRegistry(options.cloudRegion, options.projectId, options.registryName);
            break;
        case "get-device":
            System.out.println("Get device");
            System.out.println(getDevice(options.deviceId, options.projectId, options.cloudRegion, options.registryName).toPrettyString());
            break;
        case "get-iam-permissions":
            System.out.println("Get iam permissions");
            getIamPermissions(options.projectId, options.cloudRegion, options.registryName);
            break;
        case "get-device-state":
            System.out.println("Get device state");
            List<DeviceState> states = getDeviceStates(options.deviceId, options.projectId, options.cloudRegion, options.registryName);
            for (DeviceState state : states) {
                System.out.println(state.toPrettyString());
            }
            break;
        case "get-registry":
            System.out.println("Get registry");
            System.out.println(getRegistry(options.projectId, options.cloudRegion, options.registryName));
            break;
        case "list-devices":
            System.out.println("List devices");
            listDevices(options.projectId, options.cloudRegion, options.registryName);
            break;
        case "list-registries":
            System.out.println("List registries");
            listRegistries(options.projectId, options.cloudRegion);
            break;
        case "patch-device-es":
            System.out.println("Patch device with ES");
            patchEs256ForAuth(options.deviceId, options.ecPublicKeyFile, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "patch-device-rsa":
            System.out.println("Patch device with RSA");
            patchRsa256ForAuth(options.deviceId, options.rsaCertificateFile, options.projectId, options.cloudRegion, options.registryName);
            break;
        case "set-config":
            if (options.deviceId == null) {
                System.out.println("Specify device_id for the device you are updating.");
            } else {
                System.out.println("Setting device configuration");
                setDeviceConfiguration(options.deviceId, options.projectId, options.cloudRegion, options.registryName, options.configuration, options.version);
            }
            break;
        case "set-iam-permissions":
            if (options.member == null || options.role == null) {
                System.out.println("Specify member and role for the policy you are updating.");
            } else {
                System.out.println("Setting iam permissions");
                setIamPermissions(options.projectId, options.cloudRegion, options.registryName, options.member, options.role);
            }
            break;
        default:
            String header = "Cloud IoT Core Commandline Example (Device / Registry management): \n\n";
            String footer = "\nhttps://cloud.google.com/iot-core";
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("DeviceRegistryExample", header, options.options, footer, true);
            break;
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) DeviceState(com.google.api.services.cloudiot.v1.model.DeviceState)

Example 2 with DeviceState

use of com.google.api.services.cloudiot.v1.model.DeviceState 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();
}
Also used : CloudIot(com.google.api.services.cloudiot.v1.CloudIot) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) ListDeviceStatesResponse(com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 JsonFactory (com.google.api.client.json.JsonFactory)1 CloudIot (com.google.api.services.cloudiot.v1.CloudIot)1 DeviceState (com.google.api.services.cloudiot.v1.model.DeviceState)1 ListDeviceStatesResponse (com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1