Skip to content

AferoJavaSDK Overview

Tony Myles edited this page Jul 27, 2017 · 4 revisions

AferoJavaSDK Overview

AferoClient Interface

These are the base functions that need to be implemented in order for the core SDK to make the necessary Afero REST API calls.

Observable<ActionResponse> postAttributeWrite(DeviceModel deviceModel, PostActionBody body, int maxRetryCount, int statusCode);

Observable<RequestResponse[]> postBatchAttributeWrite(DeviceModel deviceModel, DeviceRequest[] body, int maxRetryCount, int statusCode);

Observable<DeviceProfile> getDeviceProfile(String profileId);

Observable<DeviceProfile[]> getAccountDeviceProfiles();

Observable<ConclaveAccessDetails> postConclaveAccess(String mobileClientId);

Observable<Location> getDeviceLocation(DeviceModel deviceModel);

Observable<DeviceAssociateResponse> deviceAssociateGetProfile(String associationId, boolean isOwnershipVerified);

Observable<DeviceAssociateResponse> deviceAssociate(String associationId);

Observable<DeviceModel> deviceDisassociate(DeviceModel deviceModel);

Observable<Void> putDeviceTimezone(DeviceModel deviceModel, TimeZone tz);

String getActiveAccountId();

int getStatusCode(Throwable t);

boolean isTransferVerificationError(Throwable t);

AferoClientRetrofit2

AferoClientRetrofit2 is a concrete implementation of the AferoClient interface using Retrofit2 and okhttp3.

Highlights

In addition to the base AferoClient calls, AferoClientRetrofit2 provides calls to fetch an access token, account tracking, password reset, sign out, etc.

public Observable<AccessToken> getAccessToken(String user, String password)

public void setOwnerAndActiveAccountId(String accountId)

public Observable<Void> resetPassword(String email)

public void signOut(String userId, String mobileClientId)

Example

AferoClientRetrofit2.Config aferoClientConfig = new AferoClientRetrofit2.ConfigBuilder()
    .oauthClientId(myOAuthClientId)
    .oauthClientSecret(myOAuthClientSecret)
    .logLevel(BuildConfig.HTTP_LOG_LEVEL)
    .defaultTimeout(DEFAULT_SERVICE_TIMEOUT)
    .build();

mAferoClient = new AferoClientRetrofit2(aferoClientConfig);

mAferoClient.setOwnerAndActiveAccountId(accountId);

mAferoClient.setToken(new AccessToken(accessToken, refreshToken));

DeviceCollection

The DeviceCollection manages the devices associated with the account, and provides updates when the collection or individual device state changes.

Setup

mDeviceCollection = new DeviceCollection(mAferoClient, ClientID.get(this));
mDeviceCollection.start()
    .subscribe(new Observer<DeviceCollection>() {
            @Override
            public void onNext(DeviceCollection deviceCollection) {
                // DeviceCollection is fully populated and listening for updates
            }

            @Override
            public void onCompleted() {
                // DeviceCollection is fully populated and listening for updates
            }

            @Override
            public void onError(Throwable e) {
                // may get IllegalStateException if already started
            }
        });

Adding a device to the account

public Observable addDevice(String associationId, boolean isOwnershipVerified)

deviceCollection.addDevice(associationId, false)
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("New Device Added: " + deviceModel.getName());
        }
    });

Removing a device from the account

public Observable removeDevice(DeviceModel deviceModel)

deviceCollection.removeDevice(deviceModel)
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("New Device Added: " + deviceModel.getName());
        }
    });    

Observing as devices are added to the account

deviceCollection.observeCreates()
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("Device Added: " + deviceModel.getName());
        }
    });    

Observing as devices are removed from the account

deviceCollection.observeDeletes()
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("Device Removed: " + deviceModel.getName());
        }
    });    

AferoSofthub

The AferoSofthub is a singleton that manages the soft hub functionality.

Example

mAferoSofthub = AferoSofthub.acquireInstance(this, mAferoClient, "my-unique-client-identifier");

if (!(mAferoSofthub.isRunning() || mAferoSofthub.isStarting()) {
    mAferoSofthub.start()
        .subscribe(new Observer<AferoSofthub>() {
            @Override
            public void onNext(AferoSofthub aferoSofthub) {
                // AferoSofthub is now running
            }

            @Override
            public void onCompleted() {
                // AferoSofthub is now running
            }

            @Override
            public void onError(Throwable e) {
                // may get IllegalStateException if already starting or running
            }
        });
}

DeviceWifiSetup

This class provides support for the Afero wifi setup process to help bring wifi based devices online.

Example

DeviceWifiSetup wifiSetup = new DeviceWifiSetup(DeviceModel deviceModel, AferoClient aferoClient);
wifiSetup.start();

// fetch the wifi network list visible from the Afero device
wifiSetup.getWifiSSIDList()
    .subscribe(new Action1<WifiSSIDEntry>() {
        @Override
        public void call(WifiSSIDEntry wifiSSIDEntry) {
            AfLog.i((wifiSSIDEntry.isSecure() ? "+" : "-") + " " + wifiSSIDEntry.getSSID());
        }
    });
Clone this wiki locally