Search in sources :

Example 1 with Device

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));
        }
    });
}
Also used : CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) Call(retrofit2.Call) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) DeviceData(com.arm.mbed.cloud.sdk.internal.devicedirectory.model.DeviceData) API(com.arm.mbed.cloud.sdk.annotations.API) Nullable(com.arm.mbed.cloud.sdk.annotations.Nullable)

Example 2 with Device

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));
        }
    });
}
Also used : CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) Call(retrofit2.Call) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) DeviceData(com.arm.mbed.cloud.sdk.internal.devicedirectory.model.DeviceData) API(com.arm.mbed.cloud.sdk.annotations.API) Nullable(com.arm.mbed.cloud.sdk.annotations.Nullable)

Example 3 with Device

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();
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) API(com.arm.mbed.cloud.sdk.annotations.API)

Example 4 with Device

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;
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) Subscription(com.arm.mbed.cloud.sdk.connect.model.Subscription) LinkedList(java.util.LinkedList) API(com.arm.mbed.cloud.sdk.annotations.API) Nullable(com.arm.mbed.cloud.sdk.annotations.Nullable)

Example 5 with Device

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());
    }
}
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) 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

Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)19 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)12 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)12 AbstractExample (utils.AbstractExample)12 Example (utils.Example)12 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)10 Connect (com.arm.mbed.cloud.sdk.Connect)8 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)7 DeviceDirectory (com.arm.mbed.cloud.sdk.DeviceDirectory)4 API (com.arm.mbed.cloud.sdk.annotations.API)4 LinkedList (java.util.LinkedList)4 Nullable (com.arm.mbed.cloud.sdk.annotations.Nullable)3 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)2 TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)2 DeviceData (com.arm.mbed.cloud.sdk.internal.devicedirectory.model.DeviceData)2 Call (retrofit2.Call)2 Callback (com.arm.mbed.cloud.sdk.common.Callback)1 Presubscription (com.arm.mbed.cloud.sdk.connect.model.Presubscription)1 Subscription (com.arm.mbed.cloud.sdk.connect.model.Subscription)1 Webhook (com.arm.mbed.cloud.sdk.connect.model.Webhook)1