Search in sources :

Example 31 with ConnectionOptions

use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method setResourceValue.

/**
 * Sets a resource value.
 */
@SuppressWarnings("boxing")
@Example
public void setResourceValue() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    // writable resource path to set a value to
    String resourcePath = "/5001/0/1";
    try {
        // Getting a connected device.
        DeviceListOptions options = new DeviceListOptions();
        options.setLimit(1);
        Paginator<Device> deviceIterator = api.listAllConnectedDevices(options);
        if (!deviceIterator.hasNext()) {
            fail("No endpoints registered. Aborting.");
        }
        Device device = deviceIterator.next();
        log("Device", device);
        Resource resourceToConsider = api.getResource(device, resourcePath);
        log("Resource of interest", resourceToConsider);
        if (resourceToConsider == null) {
            fail("The resource of interest does not exist on this device");
        }
        // Getting resource value
        Object value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("Current resource value", value);
        // Setting a new resource value
        api.setResourceValue(resourceToConsider, "10", new TimePeriod(10));
        // Getting the modified resource value
        value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("Newly set resource value", value);
        // Stopping potential daemons running
        api.stopNotifications();
        api.shutdownConnectService();
    } catch (Exception e) {
        e.printStackTrace();
        logError("last API Metadata", api.getLastApiMetadata());
        try {
            api.stopNotifications();
        } catch (MbedCloudException e1) {
            e1.printStackTrace();
        }
        api.shutdownConnectService();
        fail(e.getMessage());
    }
}
Also used : MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) TimePeriod(com.arm.mbed.cloud.sdk.common.TimePeriod) 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) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 32 with ConnectionOptions

use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method injectNotifications.

/**
 * Injects notifications to the notification system.
 */
@Example
public void injectNotifications() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // Creating notifications.
        String[] payloads = { "Q2hhbmdlIG1lIQ==", "VGhpcyBpcyB2YWx1ZSAy", "VGhpcyBpcyBhbm90aGVyIHZhbHVl", "VGhpcyB3aWxsIGJlIG15IGxhc3Qgbm90aWZpY2F0aW9uIGJlY2F1c2UgSSBhbSB3aWxsaW5nIHRvIGdvIGJhY2sgdG8gc2xlZXA=" };
        List<String> payloadList = Arrays.asList(payloads);
        String deviceId = "015f4ac587f500000000000100100249";
        String resourcePath = "/3200/0/5501";
        NotificationMessage notifications = new NotificationMessage();
        for (String payload : payloadList) {
            NotificationData notification = new NotificationData();
            notification.setEp(deviceId);
            notification.setPath(resourcePath);
            notification.setPayload(payload);
            notifications.addNotificationsItem(notification);
        }
        // Creating the same notifications but using their JSON representation instead.
        String otherNotifications = "{\"notifications\":[{\"path\":\"/3200/0/5501\",\"payload\":\"Q2hhbmdlIG1lIQ\u003d\u003d\",\"ep\":\"015f4ac587f500000000000100100249\"},{\"path\":\"/3200/0/5501\",\"payload\":\"VGhpcyBpcyB2YWx1ZSAy\",\"ep\":\"015f4ac587f500000000000100100249\"}" + ",{\"path\":\"/3200/0/5501\",\"payload\":\"VGhpcyBpcyBhbm90aGVyIHZhbHVl\",\"ep\":\"015f4ac587f500000000000100100249\"},{\"path\":\"/3200/0/5501\",\"payload\":\"VGhpcyB3aWxsIGJlIG15IGxhc3Qgbm90aWZpY2F0aW9uIGJlY2F1c2UgSSBhbSB3aWxsaW5nIHRvIGdvIGJhY2sgdG8gc2xlZXA\u003d\",\"ep\":\"015f4ac587f500000000000100100249\"}]}";
        Resource resource = new Resource(deviceId, resourcePath);
        // Creating a subscriber for this resource.
        api.createResourceSubscriptionObserver(resource, BackpressureStrategy.BUFFER).subscribe(new Consumer<Object>() {

            @Override
            public void accept(Object t) throws Exception {
                log("Received notification value", t);
            }
        });
        // Emitting notifications.
        api.notify(notifications);
        api.notify(otherNotifications);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : NotificationMessage(com.arm.mbed.cloud.sdk.internal.mds.model.NotificationMessage) Connect(com.arm.mbed.cloud.sdk.Connect) Resource(com.arm.mbed.cloud.sdk.connect.model.Resource) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) NotificationData(com.arm.mbed.cloud.sdk.internal.mds.model.NotificationData) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 33 with ConnectionOptions

use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method listDeviceResources.

/**
 * Lists device resources.
 */
@Example
public void listDeviceResources() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        // 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.first();
        log("Device", device);
        // Listing all device's resource.
        List<Resource> resources = api.listResources(device);
        for (Resource resource : resources) {
            log("Resource present on device", resource);
        }
    } 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) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 34 with ConnectionOptions

use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method retrieveConnectionOptions.

private ConnectionOptions retrieveConnectionOptions(String bodyAsString) {
    if (bodyAsString == null || bodyAsString.isEmpty()) {
        logger.logWarn("The test server did not receive any connection configuration. Defaulting to test server configuration.");
        return defaultConnectionConfiguration;
    }
    InstanceConfiguration conf = null;
    try {
        conf = Serializer.convertStringToObject(bodyAsString, InstanceConfiguration.class);
    } catch (Exception e) {
        logger.logWarn("The test server could not interpret instance configuration properly: [" + bodyAsString + "]. Defaulting to test server configuration.");
        return defaultConnectionConfiguration;
    }
    ConnectionOptions opts = new ConnectionOptions(conf.getApiKey(), conf.getHost());
    if (opts.isApiKeyEmpty()) {
        logger.logWarn("The test server could not find the API key configuration in the request: [" + bodyAsString + "]. Defaulting to test server configuration.");
        return defaultConnectionConfiguration;
    }
    opts.setAutostartDaemon(TranslationUtils.toBool(conf.isAutostartDaemon(), defaultConnectionConfiguration.isAutostartDaemon()));
    opts.setClientLogLevel(defaultConnectionConfiguration.getClientLogLevel());
    opts.setRequestTimeout(defaultConnectionConfiguration.getRequestTimeout());
    return opts;
}
Also used : InstanceConfiguration(com.arm.mbed.cloud.sdk.testserver.model.InstanceConfiguration) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)

Example 35 with ConnectionOptions

use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method retrieveConfig.

private void retrieveConfig() {
    defaultConnectionConfiguration = new ConnectionOptions(System.getenv(ENVVAR_MBED_CLOUD_API_KEY), System.getenv(ENVVAR_MBED_CLOUD_HOST));
    defaultConnectionConfiguration.setClientLogLevel(CallLogLevel.getLevel(System.getenv(ENVVAR_HTTP_LOG_LEVEL)));
// logger.logInfo("Default config:");
// logger.logInfo(JsonObject.mapFrom(defaultConnectionConfiguration).encodePrettily());
}
Also used : ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions)

Aggregations

ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)35 AbstractExample (utils.AbstractExample)31 Example (utils.Example)31 MbedCloudException (com.arm.mbed.cloud.sdk.common.MbedCloudException)27 Connect (com.arm.mbed.cloud.sdk.Connect)15 Device (com.arm.mbed.cloud.sdk.devicedirectory.model.Device)12 DeviceListOptions (com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions)10 Resource (com.arm.mbed.cloud.sdk.connect.model.Resource)8 DeviceDirectory (com.arm.mbed.cloud.sdk.DeviceDirectory)7 AccountManagement (com.arm.mbed.cloud.sdk.AccountManagement)4 Update (com.arm.mbed.cloud.sdk.Update)4 Metric (com.arm.mbed.cloud.sdk.connect.model.Metric)3 Webhook (com.arm.mbed.cloud.sdk.connect.model.Webhook)3 Query (com.arm.mbed.cloud.sdk.devicedirectory.model.Query)3 Certificates (com.arm.mbed.cloud.sdk.Certificates)2 ApiKey (com.arm.mbed.cloud.sdk.accountmanagement.model.ApiKey)2 User (com.arm.mbed.cloud.sdk.accountmanagement.model.User)2 Certificate (com.arm.mbed.cloud.sdk.certificates.model.Certificate)2 TimePeriod (com.arm.mbed.cloud.sdk.common.TimePeriod)2 MetricsPeriodListOptions (com.arm.mbed.cloud.sdk.connect.model.MetricsPeriodListOptions)2