All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arm.mbed.cloud.sdk.connect.subscription.ResourceSubscriber Maven / Gradle / Ivy

Go to download

The Pelion Cloud SDK (formerly known as Mbed Cloud SDK) provides a simplified interface to the Pelion Cloud APIs by exposing functionality using conventions and paradigms familiar to Java developers.

There is a newer version: 2.5.0
Show newest version
package com.arm.mbed.cloud.sdk.connect.subscription;

import java.util.List;

import com.arm.mbed.cloud.sdk.annotations.Preamble;
import com.arm.mbed.cloud.sdk.common.AbstractApi;
import com.arm.mbed.cloud.sdk.common.MbedCloudException;
import com.arm.mbed.cloud.sdk.common.listing.FilterOptions;
import com.arm.mbed.cloud.sdk.common.listing.Paginator;
import com.arm.mbed.cloud.sdk.connect.adapters.PresubscriptionAdapter;
import com.arm.mbed.cloud.sdk.connect.model.Presubscription;
import com.arm.mbed.cloud.sdk.devicedirectory.adapters.DeviceAdapter;
import com.arm.mbed.cloud.sdk.devicedirectory.model.Device;
import com.arm.mbed.cloud.sdk.subscribe.model.FirstValue;
import com.arm.mbed.cloud.sdk.subscribe.model.SubscriptionFilterOptions;

@Preamble(description = "Object in charge of performing all necessary action to take place when subscribing to a resource")
public class ResourceSubscriber extends AbstractSubscriptionAction {

    public ResourceSubscriber(AbstractApi api, FirstValue mode) {
        super(api, mode);
    }

    @Override
    public void execute(FilterOptions arg) throws MbedCloudException {
        if (!hasCloudConnection()) {
            return;
        }
        final SubscriptionFilterOptions filters = (SubscriptionFilterOptions) arg;
        // Setting pre-subscriptions
        final List correspondingPresubscriptions = PresubscriptionAdapter
                .mapSubscriptionFilter(filters);
        api.addSomePresubscriptions(correspondingPresubscriptions);
        // Subscribe to currently connected devices
        if (mode == FirstValue.ON_VALUE_UPDATE) {
            final Paginator iterator = api
                    .listAllConnectedDevices(DeviceAdapter.mapSubscriptionOptions(filters));
            while (iterator.hasNext()) {
                api.addResourcesSubscription(
                        filters.getVerifiedResources(api.listObservableResources(iterator.next())));
            }
        }

    }

    @Override
    public ResourceSubscriber clone() {
        return new ResourceSubscriber(api, mode);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy