Search in sources :

Example 1 with Presubscription

use of com.arm.mbed.cloud.sdk.connect.model.Presubscription 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());
    }
}
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) Presubscription(com.arm.mbed.cloud.sdk.connect.model.Presubscription) LinkedList(java.util.LinkedList) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 2 with Presubscription

use of com.arm.mbed.cloud.sdk.connect.model.Presubscription in project mbed-cloud-sdk-java by ARMmbed.

the class Connect method updatePresubscriptions.

/**
 * Updates pre-subscription data.
 * <p>
 * Example:
 *
 * <pre>
 * {@code
 * try {
 *     String deviceId = "015f4ac587f500000000000100100249";
 *
 *     Presubscription presub1 = new Presubscription();
 *     presub1.setDeviceType("default");
 *     List<String> resourceList1 = Arrays.asList("/3201/0/5850", "/3201/0/5853");
 *     presub1.setResourcePaths(resourceList1);
 *
 *     Presubscription presub2 = new Presubscription();
 *     presub2.setDeviceId(deviceId);
 *     List<String> resourceList2 = Arrays.asList("/3200/0/5501");
 *     presub2.setResourcePaths(resourceList2);
 *
 *     List<Presubscription> presubscriptions = Arrays.asList(presub1, presub2);
 *     connectApi.updatePresubscriptions(presubscriptions);
 * } catch (MbedCloudException e) {
 *     e.printStackTrace();
 * }
 * }
 * </pre>
 *
 * @param presubscriptions
 *            The pre-subscription list to update.
 *            <p>
 *            If you send an empty/null array, the pre-subscription data will be removed @see
 *            {@link #deletePresubscriptions()} for similar action.
 * @throws MbedCloudException
 *             if a problem occurred during request processing.
 */
@API
public void updatePresubscriptions(@Nullable List<Presubscription> presubscriptions) throws MbedCloudException {
    final List<Presubscription> finalList = presubscriptions;
    final PresubscriptionArray array = PresubscriptionAdapter.reverseMapList(finalList);
    CloudCaller.call(this, "updatePresubscriptions()", null, new CloudCall<Void>() {

        @Override
        public Call<Void> call() {
            return endpoint.getSubscriptions().v2SubscriptionsPut(array);
        }
    });
}
Also used : Call(retrofit2.Call) CloudCall(com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall) PresubscriptionArray(com.arm.mbed.cloud.sdk.internal.mds.model.PresubscriptionArray) Presubscription(com.arm.mbed.cloud.sdk.connect.model.Presubscription) API(com.arm.mbed.cloud.sdk.annotations.API)

Example 3 with Presubscription

use of com.arm.mbed.cloud.sdk.connect.model.Presubscription in project mbed-cloud-sdk-java by ARMmbed.

the class PresubscriptionAdapter method map.

/**
 * Maps presubscription.
 *
 * @param apiPresubscription
 *            presubscription to map
 * @return mapped presubscription
 */
public static Presubscription map(com.arm.mbed.cloud.sdk.internal.mds.model.Presubscription apiPresubscription) {
    if (apiPresubscription == null) {
        return null;
    }
    final Presubscription presubscription = new Presubscription();
    presubscription.setDeviceId(apiPresubscription.getEndpointName());
    presubscription.setDeviceType(apiPresubscription.getEndpointType());
    presubscription.setResourcePaths(apiPresubscription.getResourcePath());
    return presubscription;
}
Also used : Presubscription(com.arm.mbed.cloud.sdk.connect.model.Presubscription)

Aggregations

Presubscription (com.arm.mbed.cloud.sdk.connect.model.Presubscription)3 Connect (com.arm.mbed.cloud.sdk.Connect)1 API (com.arm.mbed.cloud.sdk.annotations.API)1 CloudCall (com.arm.mbed.cloud.sdk.common.CloudCaller.CloudCall)1 ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)1 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)1 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)1 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)1 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)1 PresubscriptionArray (com.arm.mbed.cloud.sdk.internal.mds.model.PresubscriptionArray)1 LinkedList (java.util.LinkedList)1 Call (retrofit2.Call)1 AbstractExample (utils.AbstractExample)1 Example (utils.Example)1