Search in sources :

Example 1 with TimePeriod

use of com.arm.mbed.cloud.sdk.common.TimePeriod in project mbed-cloud-sdk-java by ARMmbed.

the class AbstractMetricsListOptions method setDefault.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.arm.mbed.cloud.sdk.common.listing.ListOptions#setOptions(com.arm.mbed.cloud.sdk.common.listing.ListOptions)
     */
/*
     * (non-Javadoc)
     * 
     * @see com.arm.mbed.cloud.sdk.common.listing.ListOptions#setDefault()
     */
@Override
protected void setDefault() {
    super.setDefault();
    setInterval(new TimePeriod());
}
Also used : TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod)

Example 2 with TimePeriod

use of com.arm.mbed.cloud.sdk.common.TimePeriod in project mbed-cloud-sdk-java by ARMmbed.

the class MetricsPeriodListOptions method setDefault.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.arm.mbed.cloud.sdk.common.listing.ListOptions#setOptions(com.arm.mbed.cloud.sdk.common.listing.ListOptions)
     */
/*
     * (non-Javadoc)
     * 
     * @see com.arm.mbed.cloud.sdk.common.listing.ListOptions#setDefault()
     */
@Override
protected void setDefault() {
    super.setDefault();
    setPeriod(new TimePeriod());
}
Also used : TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod)

Example 3 with TimePeriod

use of com.arm.mbed.cloud.sdk.common.TimePeriod 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 4 with TimePeriod

use of com.arm.mbed.cloud.sdk.common.TimePeriod in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method setResourceValue.

/**
 * Sets a resource value.
 */
@SuppressWarnings("boxing")
@Example
public void setResourceValue() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    // writable resource path to set a value to
    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.next();
        log("Device", device);
        Resource resourceToConsider = api.getResource(device, resourcePath);
        log("Resource of interest", resourceToConsider);
        if (resourceToConsider == null) {
            fail("The resource of interest does not exist on this device");
        }
        // Getting resource value
        Object value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("Current resource value", value);
        // Setting a new resource value
        api.setResourceValue(resourceToConsider, "10", new TimePeriod(10));
        // Getting the modified resource value
        value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("Newly set 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 5 with TimePeriod

use of com.arm.mbed.cloud.sdk.common.TimePeriod in project mbed-cloud-sdk-java by ARMmbed.

the class TestAbstractObserver method testOne.

@Test
public void testOne() {
    SubscriptionTestManagerFixedInput manager = new SubscriptionTestManagerFixedInput(true);
    TestObserver obs = manager.createObserver(SubscriptionType.DEVICE_STATE_CHANGE, null, BackpressureStrategy.BUFFER);
    try {
        NotificationTestMessageValue value = obs.one(new TimePeriod(5));
        // Value should be the first element received.
        assertEquals(0, value.i);
        // The observer should have been disposed
        assertTrue(obs.isDisposed());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    // Checking that timeout works properly.
    SubscriptionTestManagerNeverInput manager2 = new SubscriptionTestManagerNeverInput(true);
    obs = manager2.createObserver(SubscriptionType.DEVICE_STATE_CHANGE, null, BackpressureStrategy.BUFFER);
    long start = 0;
    try {
        start = System.currentTimeMillis();
        NotificationTestMessageValue value = obs.one(new TimePeriod(2));
        assertNull(value);
    // Value should never be received.
    } catch (MbedCloudException e) {
        double time = (System.currentTimeMillis() - start) / 1000d;
        assertNotNull(e);
        assertEquals("The value could not be retrieved", e.getMessage());
        // Check that at least 2 seconds elapsed before the exception was thrown.
        assertTrue(time > 2);
        // Check that the timeout was of the right order of magnitude.
        assertTrue(time < 5);
    }
}
Also used : MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Test(org.junit.Test)

Aggregations

TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)5 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)3 Connect (com.arm.mbed.cloud.sdk.Connect)2 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)2 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)2 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)2 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)2 AbstractExample (utils.AbstractExample)2 Example (utils.Example)2 Test (org.junit.Test)1