Search in sources :

Example 6 with Connect

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

the class ConnectExamples method subscribeToResourcesWithObservableStreams.

/**
 * Subscribes to a resource with observable streams.
 * <p>
 * Note: for more information about observable streams, see also <a href="http://reactivex.io/">Reactive X</a>
 */
@SuppressWarnings({ "boxing", "null" })
@Example
public void subscribeToResourcesWithObservableStreams() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    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);
        List<Resource> observableResources = api.listObservableResources(device);
        if (observableResources == null) {
            fail("There is no observable resources on this device");
        }
        for (Resource resourceToSubscribeTo : observableResources) {
            log("Resource suscribed", resourceToSubscribeTo);
            // Adding subscription for each resource.
            if (resourceToSubscribeTo != null) {
                api.addResourceSubscription(resourceToSubscribeTo, BackpressureStrategy.BUFFER).subscribe(generateSubscriptionConsumer(resourceToSubscribeTo));
            }
        }
        // Listening to notifications for 2 minutes.
        api.startNotifications();
        Thread.sleep(120000);
        // Stopping notification pull channel.
        api.stopNotifications();
        Thread.sleep(100);
        api.shutdownConnectService();
    } catch (Exception e) {
        e.printStackTrace();
        logError("last API Metadata", api.getLastApiMetadata());
        try {
            api.stopNotifications();
            Thread.sleep(100);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        api.shutdownConnectService();
        fail(e.getMessage());
    }
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) 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 7 with Connect

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

the class ConnectExamples method listLast30DaysMetric.

/**
 * Lists metrics from last 30 days in 1-day intervals.
 */
@Example
public void listLast30DaysMetric() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Defining query options.
        MetricsPeriodListOptions options = new MetricsPeriodListOptions();
        options.setPeriod("30d");
        options.setInterval("1d");
        // Listing metrics data.
        Paginator<Metric> metrics = api.listAllMetrics(options);
        for (Metric metric : metrics) {
            log("Metric", metric);
        }
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        fail(e.getMessage());
    }
}
Also used : MetricsPeriodListOptions(com.arm.mbed.cloud.sdk.connect.model.MetricsPeriodListOptions) Connect(com.arm.mbed.cloud.sdk.Connect) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Metric(com.arm.mbed.cloud.sdk.connect.model.Metric) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 8 with Connect

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

the class ConnectExamples method switchNotificationChannel.

/**
 * Switches between notification channels.
 */
@Example
public void switchNotificationChannel() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Using Notification pull channel.
        api.startNotifications();
        Thread.sleep(5000);
        // Stopping notification pull channel.
        api.stopNotifications();
        Thread.sleep(100);
        // Starting a webhook channel.
        // Creating a webhook.
        Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
        log("Webhook", webhook);
        // Setting up the webhook.
        api.updateWebhook(webhook);
        Thread.sleep(2000);
        // Stopping webhook channel.
        deleteWebhook(api);
        // Shutting down connect service.
        api.shutdownConnectService();
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            api.stopNotifications();
            Thread.sleep(100);
            api.shutdownConnectService();
            Thread.sleep(100);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        deleteWebhook(api);
    }
}
Also used : Connect(com.arm.mbed.cloud.sdk.Connect) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) URL(java.net.URL) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 9 with Connect

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

the class ConnectExamples method listLast2DaysMetric.

/**
 * Lists metrics from last 2 days in 3-hour intervals.
 */
@Example
public void listLast2DaysMetric() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Defining query options.
        MetricsPeriodListOptions options = new MetricsPeriodListOptions();
        options.setPeriod("2d");
        options.setInterval("3h");
        // Listing metrics data.
        Paginator<Metric> metrics = api.listAllMetrics(options);
        for (Metric metric : metrics) {
            log("Metric", metric);
        }
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        fail(e.getMessage());
    }
}
Also used : MetricsPeriodListOptions(com.arm.mbed.cloud.sdk.connect.model.MetricsPeriodListOptions) Connect(com.arm.mbed.cloud.sdk.Connect) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Metric(com.arm.mbed.cloud.sdk.connect.model.Metric) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 10 with Connect

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

the class ConnectExamples method switchNotificationChannelWithForceClear.

/**
 * Switches between notification channels using forceClear.
 * <p>
 * forceClear parameter forces a notification channel to stop before a new one is started.
 */
@Example
public void switchNotificationChannelWithForceClear() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Setting forceClear to true
        api.setForceClear(true);
        // Using Notification pull channel.
        api.startNotifications();
        Thread.sleep(5000);
        // Starting a webhook channel.
        // Creating a webhook.
        Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
        log("Webhook", webhook);
        // Setting up the webhook.
        api.updateWebhook(webhook);
        Thread.sleep(2000);
        // Using Notification pull channel again.
        api.startNotifications();
        Thread.sleep(5000);
        // Shutting down connect service.
        api.shutdownConnectService();
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            api.shutdownConnectService();
            Thread.sleep(100);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        deleteWebhook(api);
    }
}
Also used : Connect(com.arm.mbed.cloud.sdk.Connect) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) Webhook(com.arm.mbed.cloud.sdk.connect.model.Webhook) URL(java.net.URL) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Aggregations

Connect (com.arm.mbed.cloud.sdk.Connect)15 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)15 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)15 AbstractExample (utils.AbstractExample)15 Example (utils.Example)15 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)8 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)8 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)7 Metric (com.arm.mbed.cloud.sdk.connect.model.Metric)3 Webhook (com.arm.mbed.cloud.sdk.connect.model.Webhook)3 URL (java.net.URL)3 TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)2 MetricsPeriodListOptions (com.arm.mbed.cloud.sdk.connect.model.MetricsPeriodListOptions)2 GregorianCalendar (java.util.GregorianCalendar)2 Callback (com.arm.mbed.cloud.sdk.common.Callback)1 MetricsStartEndListOptions (com.arm.mbed.cloud.sdk.connect.model.MetricsStartEndListOptions)1 Presubscription (com.arm.mbed.cloud.sdk.connect.model.Presubscription)1 NotificationData (com.arm.mbed.cloud.sdk.internal.mds.model.NotificationData)1 NotificationMessage (com.arm.mbed.cloud.sdk.internal.mds.model.NotificationMessage)1 DeviceStateObserver (com.arm.mbed.cloud.sdk.subscribe.model.DeviceStateObserver)1