use of com.arm.mbed.cloud.sdk.devicedirectory.model.Device in project mbed-cloud-sdk-java by ARMmbed.
the class DeviceDirectory method addDevice.
/**
* Adds a device.
* <p>
* Example:
*
* <pre>
* {@code
* try {
* Device device = new Device();
* device.setId("015f4ac587f500000000000100100249");
* device.setName("QuickstartDevice");
* device.setDescription("Quick start device");
* device.setDeviceType("quickstart");
*
* Device newDevice = deviceDirectoryApi.addDevice(device);
* System.out.println("New device state: " + newDevice.getState());
* } catch (MbedCloudException e) {
* e.printStackTrace();
* }
* }
* </pre>
*
* @param device
* Device details.
* @return added device.
* @throws MbedCloudException
* if a problem occurred during request processing.
*/
@API
@Nullable
public Device addDevice(@NonNull Device device) throws MbedCloudException {
checkNotNull(device, TAG_DEVICE);
checkModelValidity(device, TAG_DEVICE);
final Device finalDevice = device;
return CloudCaller.call(this, "addDevice()", DeviceAdapter.getMapper(), new CloudCall<DeviceData>() {
@Override
public Call<DeviceData> call() {
return endpoint.getDirectory().deviceCreate(DeviceAdapter.reverseMapAdd(finalDevice));
}
});
}
use of com.arm.mbed.cloud.sdk.devicedirectory.model.Device in project mbed-cloud-sdk-java by ARMmbed.
the class DeviceDirectory method updateDevice.
/**
* Updates a device.
* <p>
* Example:
*
* <pre>
* {@code
* try {
* Device device = new Device();
* String deviceId = "015f4ac587f500000000000100100249";
* device.setId(deviceId);
* device.setName("QuickstartDevice");
* device.setDescription("Updated quick start device");
* device.setDeviceType("quickstart");
*
* Device newDevice = deviceDirectoryApi.updateDevice(device);
* System.out.println("Updated device description: " + newDevice.getDescription());
* } catch (MbedCloudException e) {
* e.printStackTrace();
* }
* }
* </pre>
*
* @param device
* Device details.
* @return updated device.
* @throws MbedCloudException
* if a problem occurred during request processing.
*/
@API
@Nullable
public Device updateDevice(@NonNull Device device) throws MbedCloudException {
checkNotNull(device, TAG_DEVICE);
checkNotNull(device.getId(), TAG_DEVICE_ID);
checkModelValidity(device, TAG_DEVICE);
final Device finalDevice = device;
return CloudCaller.call(this, "updateDevice()", DeviceAdapter.getMapper(), new CloudCall<DeviceData>() {
@Override
public Call<DeviceData> call() {
return endpoint.getDirectory().deviceUpdate(finalDevice.getId(), DeviceAdapter.reverseMapUpdate(finalDevice));
}
});
}
use of com.arm.mbed.cloud.sdk.devicedirectory.model.Device in project mbed-cloud-sdk-java by ARMmbed.
the class Connect method deleteSubscriptions.
/**
* Removes all subscriptions.
* <p>
* Note: this method will deregister all subscription callbacks or observers if any.
* <p>
* Warning: Please note that this operation is potentially really expensive and hence, use wisely.
* <p>
* It could be slow for large numbers of connected devices. If possible, explicitly delete subscriptions known to
* have been created.
* <p>
* Example:
*
* <pre>
* {@code
* try {
* connectApi.deleteSubscriptions();
* } catch (MbedCloudException e) {
* e.printStackTrace();
* }
* }
* </pre>
*
* @throws MbedCloudException
* if a problem occurred during request processing.
*/
@API
public void deleteSubscriptions() throws MbedCloudException {
// The following is a workaround until there is a Mbed Cloud endpoint providing such an action.
logger.logWarn("deleteSubscriptions() could be slow for large numbers of connected devices. " + "If possible, explicitly delete subscriptions known to have been created.");
final Paginator<Device> connectedDevices = listAllConnectedDevices(null);
if (connectedDevices != null) {
for (final Device connectedDevice : connectedDevices) {
deleteDeviceSubscriptions(connectedDevice);
}
}
// When such an endpoint is created, use some code similar to below.
// CloudCaller.call(this, "deleteSubscriptions()", null, new CloudCall<Void>() {
//
// @Override
// public Call<Void> call() {
// return endpoint.getSubscriptions().v2SubscriptionsDelete();
// }
// });
// deregisterAllResourceSubscriptionObserversOrCallbacks();
}
use of com.arm.mbed.cloud.sdk.devicedirectory.model.Device in project mbed-cloud-sdk-java by ARMmbed.
the class Connect method listSubscriptions.
/**
* Lists all subscriptions.
* <p>
* Warning: Please note that this operation is potentially really expensive and hence, use wisely.
* <p>
* It could be slow for large numbers of connected devices.
* <p>
* Example:
*
* <pre>
* {@code
* try {
*
* List<String> subscriptions = connectApi.listSubscriptions();
* for (Subscription subscription : subscriptions) {
* System.out.println("subscription: " + subscription);
* }
* } catch (MbedCloudException e) {
* e.printStackTrace();
* }
* }
* </pre>
*
* @return list of subscriptions
* @throws MbedCloudException
* if a problem occurred during request processing.
*/
@API
@Nullable
public List<Subscription> listSubscriptions() throws MbedCloudException {
logger.logWarn("listSubscriptions() could be slow for large numbers of connected devices.");
final List<Subscription> subscriptions = new LinkedList<>();
// The following is a workaround until there is a Mbed Cloud endpoint providing such an action.
final Paginator<Device> connectedDevices = listAllConnectedDevices(null);
if (connectedDevices != null) {
for (final Device connectedDevice : connectedDevices) {
final List<String> deviceSubscriptions = listDeviceSubscriptions(connectedDevice);
if (deviceSubscriptions != null) {
subscriptions.add(new Subscription(connectedDevice.getId(), deviceSubscriptions));
}
}
}
return subscriptions.isEmpty() ? null : subscriptions;
}
use of com.arm.mbed.cloud.sdk.devicedirectory.model.Device 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());
}
}
Aggregations