Search in sources :

Example 6 with DeviceListOptions

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

the class DeviceDirectoryExamples method listDevices.

/**
 * Lists the last 5 devices.
 */
@SuppressWarnings("boxing")
@Example
public void listDevices() {
    ConnectionOptions config = Configuration.get();
    DeviceDirectory api = new DeviceDirectory(config);
    try {
        // Defining query options.
        DeviceListOptions options = new DeviceListOptions();
        options.setLimit(5);
        options.setOrder(Order.DESC);
        // Listing devices.
        ListResponse<Device> devices = api.listDevices(options);
        for (Device device : devices.getData()) {
            log("Device", device);
        }
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        fail(e.getMessage());
    }
}
Also used : DeviceDirectory(com.arm.mbed.cloud.sdk.DeviceDirectory) 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) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 7 with DeviceListOptions

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

the class TestAllListOptions method testDeviceListOptionsClone.

@Test
public void testDeviceListOptionsClone() {
    DeviceListOptions opt1 = new DeviceListOptions();
    opt1.addEqualFilter("field1", Integer.valueOf(3));
    opt1.includeTotalCount();
    opt1.setLimit(Integer.valueOf(2));
    DeviceListOptions opt2 = new DeviceListOptions();
    assertNotEquals(opt2, opt1);
    assertNotSame(opt2, opt1);
    opt2 = opt1.clone();
    assertEquals(opt2, opt1);
    assertNotSame(opt2, opt1);
}
Also used : DeviceListOptions(com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions) Test(org.junit.Test)

Example 8 with DeviceListOptions

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

the class ConnectExamples method listConnectedDevice.

/**
 * Finds all devices currently connected and created in November 2017.
 */
@Example
public void listConnectedDevice() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        DeviceListOptions options = new DeviceListOptions();
        options.addCreatedAtFilter(new GregorianCalendar(2017, 10, 1).getTime(), FilterOperator.GREATER_THAN);
        options.addCreatedAtFilter(new GregorianCalendar(2017, 10, 30).getTime(), FilterOperator.LESS_THAN);
        Paginator<Device> devices = api.listAllConnectedDevices(options);
        for (Device device : devices) {
            log("Connected device created in November 2017", device);
        }
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) Connect(com.arm.mbed.cloud.sdk.Connect) GregorianCalendar(java.util.GregorianCalendar) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) DeviceListOptions(com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 9 with DeviceListOptions

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

the class ConnectExamples method getResourceValue.

/**
 * Gets a resource value.
 */
@SuppressWarnings("boxing")
@Example
public void getResourceValue() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    // resource path to get value from
    String resourcePath = "/5001/0/1";
    try {
        // Getting a connected device.
        DeviceListOptions options = new DeviceListOptions();
        options.setLimit(1);
        Paginator<Device> deviceIterator = api.listAllConnectedDevices(options);
        if (!deviceIterator.hasNext()) {
            fail("No endpoints registered. Aborting.");
        }
        Device device = deviceIterator.first();
        log("Device", device);
        Resource resourceToConsider = api.getResource(device, resourcePath);
        if (resourceToConsider == null) {
            fail("The resource of interest does not exist on this device");
        }
        log("Resource of interest", resourceToConsider);
        // Getting resource value
        Object value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("Resource value", value);
        // Stopping potential daemons running
        api.stopNotifications();
        api.shutdownConnectService();
    } catch (Exception e) {
        e.printStackTrace();
        logError("last API Metadata", api.getLastApiMetadata());
        try {
            api.stopNotifications();
        } catch (MbedCloudException e1) {
            e1.printStackTrace();
        }
        api.shutdownConnectService();
        fail(e.getMessage());
    }
}
Also used : MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod) Connect(com.arm.mbed.cloud.sdk.Connect) Resource(com.arm.mbed.cloud.sdk.connect.model.Resource) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) DeviceListOptions(com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 10 with DeviceListOptions

use of com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions 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

DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)12 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)10 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)10 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)10 AbstractExample (utils.AbstractExample)10 Example (utils.Example)10 Connect (com.arm.mbed.cloud.sdk.Connect)7 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)6 DeviceDirectory (com.arm.mbed.cloud.sdk.DeviceDirectory)3 TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)2 Callback (com.arm.mbed.cloud.sdk.common.Callback)1 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)1 Presubscription (com.arm.mbed.cloud.sdk.connect.model.Presubscription)1 Query (com.arm.mbed.cloud.sdk.devicedirectory.model.Query)1 DevicePage (com.arm.mbed.cloud.sdk.internal.devicedirectory.model.DevicePage)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 Call (retrofit2.Call)1