Search in sources :

Example 21 with Example

use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method listConnectedDevice.

/**
 * Finds all devices currently connected and created in November 2017.
 */
@Example
public void listConnectedDevice() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    try {
        DeviceListOptions options = new DeviceListOptions();
        options.addCreatedAtFilter(new GregorianCalendar(2017, 10, 1).getTime(), FilterOperator.GREATER_THAN);
        options.addCreatedAtFilter(new GregorianCalendar(2017, 10, 30).getTime(), FilterOperator.LESS_THAN);
        Paginator<Device> devices = api.listAllConnectedDevices(options);
        for (Device device : devices) {
            log("Connected device created in November 2017", device);
        }
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) Connect(com.arm.mbed.cloud.sdk.Connect) GregorianCalendar(java.util.GregorianCalendar) 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 22 with Example

use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.

the class ConnectExamples method getResourceValue.

/**
 * Gets a resource value.
 */
@SuppressWarnings("boxing")
@Example
public void getResourceValue() {
    ConnectionOptions config = Configuration.get();
    Connect api = new Connect(config);
    // resource path to get value from
    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.first();
        log("Device", device);
        Resource resourceToConsider = api.getResource(device, resourcePath);
        if (resourceToConsider == null) {
            fail("The resource of interest does not exist on this device");
        }
        log("Resource of interest", resourceToConsider);
        // Getting resource value
        Object value = api.getResourceValue(resourceToConsider, new TimePeriod(10));
        log("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 23 with Example

use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.

the class CertificatesExamples method manageCertificates.

/**
 * Creates, updates and deletes a developer certificate.
 */
@Example
public void manageCertificates() {
    String certificateId = null;
    ConnectionOptions config = Configuration.get();
    Certificates api = new Certificates(config);
    try {
        String certificateName = generateCertificateName();
        log("Certificate Name", certificateName);
        // Creating and adding a certificate
        Certificate myCertificate = api.addDeveloperCertificate(new Certificate(certificateName, CertificateType.DEVELOPER, false));
        log("Created developer certificate", myCertificate);
        certificateId = myCertificate.getId();
        String newName = generateCertificateName();
        log("New Certificate Name", newName);
        myCertificate.setName(newName);
        myCertificate.setDescription("my updated certificate");
        // Updating certificate
        myCertificate = api.updateCertificate(myCertificate);
        log("Updated developer certificate", myCertificate);
        certificateId = myCertificate.getId();
        // Deleting certificate
        deleteCreatedCertificate(certificateId, api);
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        deleteCreatedCertificate(certificateId, api);
        fail(e.getMessage());
    }
}
Also used : Certificates(com.arm.mbed.cloud.sdk.Certificates) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) Certificate(com.arm.mbed.cloud.sdk.certificates.model.Certificate) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 24 with Example

use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.

the class DeviceDirectoryExamples method manageQueries.

/**
 * Creates, updates and deletes a query.
 */
@SuppressWarnings("boxing")
@Example
public void manageQueries() {
    ConnectionOptions config = Configuration.get();
    DeviceDirectory api = new DeviceDirectory(config);
    String queryId = null;
    try {
        // Creating a query.
        String queryName = "test-" + UUID.randomUUID().toString();
        log("Query name", queryName);
        Query myQuery = new Query(queryName, null);
        myQuery.addCreatedAtFilter(new Date(), FilterOperator.LESS_THAN);
        // Adding the query.
        myQuery = api.addQuery(myQuery);
        log("Created query", myQuery);
        queryId = myQuery.getId();
        // Updating the query.
        queryName = "test-" + UUID.randomUUID().toString();
        log("Updated Query name", queryName);
        myQuery.setName(queryName);
        myQuery = api.updateQuery(myQuery);
        log("Updated query", myQuery);
        queryId = myQuery.getId();
        // Fetching the query.
        myQuery = api.getQuery(queryId);
        log("Retrieved query", myQuery);
        // Finding the first 5 devices corresponding to the query.
        DeviceListOptions options = new DeviceListOptions();
        options.setFilters(myQuery.fetchFilters());
        options.setLimit(5);
        ListResponse<Device> matchingDevices = api.listDevices(options);
        for (Device device : matchingDevices.getData()) {
            log("Matching device", device);
        }
        // Deleting the query.
        deleteCreatedQuery(queryId, api);
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        deleteCreatedQuery(queryId, api);
        fail(e.getMessage());
    }
}
Also used : DeviceDirectory(com.arm.mbed.cloud.sdk.DeviceDirectory) Query(com.arm.mbed.cloud.sdk.devicedirectory.model.Query) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) DeviceListOptions(com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceListOptions) Date(java.util.Date) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Example 25 with Example

use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.

the class DeviceDirectoryExamples method manageDevices.

/**
 * Creates, updates and deletes a device.
 */
@SuppressWarnings("boxing")
@Example
public void manageDevices() {
    ConnectionOptions config = Configuration.get();
    DeviceDirectory api = new DeviceDirectory(config);
    String deviceId = null;
    try {
        // Creating a device.
        String certificateIssuerId = UUID.randomUUID().toString();
        log("Certificate Issuer Id", certificateIssuerId);
        String certificateFingerprint = UUID.randomUUID().toString();
        log("Certificate Fingerprint", certificateFingerprint);
        Device myDevice = new Device(certificateIssuerId, certificateFingerprint);
        myDevice.setName("my-test-device-" + UUID.randomUUID().toString());
        myDevice.setDeviceExecutionMode(1);
        // Adding the device.
        myDevice = api.addDevice(myDevice);
        log("Created device", myDevice);
        deviceId = myDevice.getId();
        // Updating the device.
        certificateIssuerId = UUID.randomUUID().toString();
        log("Updated Certificate Issuer Id", certificateIssuerId);
        myDevice.setCertificateIssuerId(certificateIssuerId);
        myDevice = api.updateDevice(myDevice);
        log("Updated device", myDevice);
        deviceId = myDevice.getId();
        // Deleting the device.
        deleteCreatedDevice(deviceId, api);
    } catch (Exception e) {
        logError("last API Metadata", api.getLastApiMetadata());
        deleteCreatedDevice(deviceId, api);
        fail(e.getMessage());
    }
}
Also used : DeviceDirectory(com.arm.mbed.cloud.sdk.DeviceDirectory) Device(com.arm.mbed.cloud.sdk.devicedirectory.model.Device) ConnectionOptions(com.arm.mbed.cloud.sdk.common.ConnectionOptions) MbedCloudException(com.arm.mbed.cloud.sdk.common.MbedCloudException) AbstractExample(utils.AbstractExample) Example(utils.Example)

Aggregations

ConnectionOptions (com.arm.mbed.cloud.sdk.common.ConnectionOptions)31 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 URL (java.net.URL)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