Search in sources :

Example 6 with Query

use of com.arm.mbed.cloud.sdk.devicedirectory.model.Query in project mbed-cloud-sdk-java by ARMmbed.

the class DeviceDirectoryExamples method manageQueries.

/**
 * Creates, updates and deletes a query.
 */
@SuppressWarnings("boxing")
@Example
public void manageQueries() {
    ConnectionOptions config = Configuration.get();
    DeviceDirectory api = new DeviceDirectory(config);
    String queryId = null;
    try {
        // Creating a query.
        String queryName = "test-" + UUID.randomUUID().toString();
        log("Query name", queryName);
        Query myQuery = new Query(queryName, null);
        myQuery.addCreatedAtFilter(new Date(), FilterOperator.LESS_THAN);
        // Adding the query.
        myQuery = api.addQuery(myQuery);
        log("Created query", myQuery);
        queryId = myQuery.getId();
        // Updating the query.
        queryName = "test-" + UUID.randomUUID().toString();
        log("Updated Query name", queryName);
        myQuery.setName(queryName);
        myQuery = api.updateQuery(myQuery);
        log("Updated query", myQuery);
        queryId = myQuery.getId();
        // Fetching the query.
        myQuery = api.getQuery(queryId);
        log("Retrieved query", myQuery);
        // Finding the first 5 devices corresponding to the query.
        DeviceListOptions options = new DeviceListOptions();
        options.setFilters(myQuery.fetchFilters());
        options.setLimit(5);
        ListResponse<Device> matchingDevices = api.listDevices(options);
        for (Device device : matchingDevices.getData()) {
            log("Matching device", device);
        }
        // Deleting the query.
        deleteCreatedQuery(queryId, api);
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        deleteCreatedQuery(queryId, api);
        fail(e.getMessage());
    }
}
Also used : DeviceDirectory(com.arm.mbed.cloud.sdk.DeviceDirectory) Query(com.arm.mbed.cloud.sdk.devicedirectory.model.Query) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) DeviceListOptions(com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions) Date(java.util.Date) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Aggregations

Query (com.arm.mbed.cloud.sdk.devicedirectory.model.Query)6 DeviceDirectory (com.arm.mbed.cloud.sdk.DeviceDirectory)3 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)3 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)3 DeviceQuery (com.arm.mbed.cloud.sdk.internal.devicedirectory.model.DeviceQuery)3 AbstractExample (utils.AbstractExample)3 Example (utils.Example)3 API (com.arm.mbed.cloud.sdk.annotations.API)2 Nullable (com.arm.mbed.cloud.sdk.annotations.Nullable)2 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)2 QueryListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.QueryListOptions)2 Date (java.util.Date)2 Call (retrofit2.Call)2 Update (com.arm.mbed.cloud.sdk.Update)1 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)1 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)1 Campaign (com.arm.mbed.cloud.sdk.update.model.Campaign)1 CampaignDeviceState (com.arm.mbed.cloud.sdk.update.model.CampaignDeviceState)1 CampaignDevicesStatesListOptions (com.arm.mbed.cloud.sdk.update.model.CampaignDevicesStatesListOptions)1 FirmwareManifest (com.arm.mbed.cloud.sdk.update.model.FirmwareManifest)1