use of com.arm.mbed.cloud.sdk.connect.model.Resource in project mbed-cloud-sdk-java by ARMmbed.
the class Connect method deleteResourceSubscription.
/**
* Deletes a resource's subscription.
* <p>
* Note: this method will deregister all subscription callbacks or observers for this resource if any.
* <p>
*
* Example:
*
* <pre>
* {@code
* try {
* String deviceId = "015f4ac587f500000000000100100249";
* String resourcePath = "/3200/0/5501";
* Resource buttonResource = new Resource(deviceId, resourcePath);
*
* connectApi.deleteResourceSubscription(buttonResource);
* } catch (MbedCloudException e) {
* e.printStackTrace();
* }
* }
* </pre>
*
* @param resource
* resource to subscribe to.
* @throws MbedCloudException
* if a problem occurred during request processing.
*/
@API
public void deleteResourceSubscription(@NonNull Resource resource) throws MbedCloudException {
checkNotNull(resource, TAG_RESOURCE);
checkModelValidity(resource, TAG_RESOURCE);
final Resource finalResource = resource;
CloudCaller.call(this, "deleteResourceSubscription()", null, new CloudCall<Void>() {
@Override
public Call<Void> call() {
return endpoint.getSubscriptions().v2SubscriptionsDeviceIdResourcePathDelete(finalResource.getDeviceId(), ApiUtils.normalisePath(finalResource.getPath()));
}
});
deregisterResourceSubscriptionCallback(finalResource);
removeResourceSubscriptionObserver(finalResource);
}
use of com.arm.mbed.cloud.sdk.connect.model.Resource 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.connect.model.Resource 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.connect.model.Resource 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());
}
}
use of com.arm.mbed.cloud.sdk.connect.model.Resource 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());
}
}
Aggregations