use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method listMarch2017Metric.
/**
* Lists metrics for March 2017.
*/
@Example
public void listMarch2017Metric() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
try {
// Defining query options.
MetricsStartEndListOptions options = new MetricsStartEndListOptions();
options.setStart(new GregorianCalendar(2017, 2, 1).getTime());
options.setEnd(new GregorianCalendar(2017, 3, 1).getTime());
// 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());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method setUpAWebhook.
/**
* Sets up a webhook for notifications
*/
@Example
public void setUpAWebhook() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
// Telling the API to stop notification channel if already in use
api.setForceClear(true);
try {
// Defining resource to listen to
String resourcePath = "/5002/0/1";
log("Resource path of interest", resourcePath);
// Creating webhook.
Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
log("Webhook", webhook);
// Setting up webhook.
api.updateWebhook(webhook);
Thread.sleep(2000);
// Adding subscription to all connected devices.
Paginator<Device> connectedDevices = api.listAllConnectedDevices(null);
for (Device connectedDevice : connectedDevices) {
try {
Resource resource = api.getResource(connectedDevice, resourcePath);
if (resource != null) {
api.addResourceSubscription(resource);
}
} catch (Exception e1) {
e1.printStackTrace();
logError("An error occurred when trying to fetch Resource [" + resourcePath + "] on device: " + connectedDevice, api.getLastApiMetadata());
}
}
// Waiting for notifications to be sent to the webhook.
Thread.sleep(60000);
// Deleting the webhook.
deleteWebhook(api);
} catch (Exception e) {
e.printStackTrace();
logError("last API Metadata", api.getLastApiMetadata());
deleteWebhook(api);
fail(e.getMessage());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method subscribeToDeviceStateChanges.
/**
* Subscribes to device state changes.
* <p>
* Note: This example introduces new high level abstraction objects such as Observer
*/
@Example
public void subscribeToDeviceStateChanges() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
try {
// Creating an Observer listening to device state changes for devices whose ids start with 016 and for
// devices which are newly registered or expired.
// For more information about backpressure strategies, please have a look at related documentation:
// https://github.com/ReactiveX/RxJava/wiki/Backpressure
DeviceStateObserver observer = api.subscribe().deviceState(DeviceStateFilterOptions.newFilter().likeDevice("016%").inDeviceStates(Arrays.asList(DeviceState.REGISTRATION, DeviceState.EXPIRED_REGISTRATION)), BackpressureStrategy.BUFFER);
// Printing device changes when they happen.
observer.flow().subscribe(System.out::println);
// Listening to device state changes for 2 minutes.
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());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method managePresubscription.
/**
* Creates, updates and deletes a presubscription.
*/
@SuppressWarnings("null")
@Example
public void managePresubscription() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
try {
// Listing already defined presubscriptions.
List<Presubscription> listPresubscriptions = api.listPresubscriptions();
for (Presubscription presubscription : listPresubscriptions) {
log("Already defined presubscription", presubscription);
}
// Getting a connected device.
DeviceListOptions options = new DeviceListOptions();
options.setLimit(Integer.valueOf(1));
Paginator<Device> deviceIterator = api.listAllConnectedDevices(options);
if (!deviceIterator.hasNext()) {
fail("No endpoints registered. Aborting.");
}
Device device = deviceIterator.next();
log("Device", device);
// Adding presubscriptions on all observable resources of a device.
List<Resource> observableResources = api.listObservableResources(device);
if (observableResources == null) {
fail("There is no observable resources on this device");
}
List<Presubscription> newPresubscriptions = new LinkedList<>();
for (Resource resourceToSubscribeTo : observableResources) {
Presubscription presubscription = new Presubscription(resourceToSubscribeTo);
newPresubscriptions.add(presubscription);
log("New presubscription", presubscription);
}
api.updatePresubscriptions(newPresubscriptions);
// Listing all defined presubscriptions.
listPresubscriptions = api.listPresubscriptions();
for (Presubscription presubscription : listPresubscriptions) {
log("Newly defined presubscription", presubscription);
}
// Deleting all presubscriptions present on the system.
api.deletePresubscriptions();
} catch (Exception e) {
logError("last API Metadata", api.getLastApiMetadata());
fail(e.getMessage());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method subscribeToResourcesWithCallbacks.
/**
* Subscribes to a resource with callbacks.
*/
@SuppressWarnings({ "boxing", "null" })
@Example
public void subscribeToResourcesWithCallbacks() {
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 (final Resource resourceToSubscribeTo : observableResources) {
log("Resource suscribed", resourceToSubscribeTo);
// Creating a subscriber for each resource.
if (resourceToSubscribeTo != null) {
// Defining callbacks.
Callback<Object> onNotificationCallback = new Callback<Object>() {
@Override
public void execute(Object arg) {
log("Received notification value for " + resourceToSubscribeTo + " using callbacks", arg);
}
};
Callback<Throwable> onErrorCallback = new Callback<Throwable>() {
@Override
public void execute(Throwable t) {
log("Received following error for " + resourceToSubscribeTo, t);
}
};
api.addResourceSubscription(resourceToSubscribeTo, onNotificationCallback, onErrorCallback);
}
}
// 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());
}
}
Aggregations