use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method listLast2DaysMetric.
/**
* Lists metrics from last 2 days in 3-hour intervals.
*/
@Example
public void listLast2DaysMetric() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
try {
// Defining query options.
MetricsPeriodListOptions options = new MetricsPeriodListOptions();
options.setPeriod("2d");
options.setInterval("3h");
// Listing metrics data.
Paginator<Metric> metrics = api.listAllMetrics(options);
for (Metric metric : metrics) {
log("Metric", metric);
}
} catch (Exception e) {
logError("last API Metadata", api.getLastApiMetadata());
fail(e.getMessage());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions in project mbed-cloud-sdk-java by ARMmbed.
the class ConnectExamples method switchNotificationChannelWithForceClear.
/**
* Switches between notification channels using forceClear.
* <p>
* forceClear parameter forces a notification channel to stop before a new one is started.
*/
@Example
public void switchNotificationChannelWithForceClear() {
ConnectionOptions config = Configuration.get();
Connect api = new Connect(config);
try {
// Setting forceClear to true
api.setForceClear(true);
// Using Notification pull channel.
api.startNotifications();
Thread.sleep(5000);
// Starting a webhook channel.
// Creating a webhook.
Webhook webhook = new Webhook(new URL("http://mbedcloudjavawebhooktest.requestcatcher.com/test"));
log("Webhook", webhook);
// Setting up the webhook.
api.updateWebhook(webhook);
Thread.sleep(2000);
// Using Notification pull channel again.
api.startNotifications();
Thread.sleep(5000);
// Shutting down connect service.
api.shutdownConnectService();
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
try {
api.shutdownConnectService();
Thread.sleep(100);
} catch (Exception e1) {
e1.printStackTrace();
}
deleteWebhook(api);
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions 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());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions 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());
}
}
use of com.arm.mbed.cloud.sdk.common.ConnectionOptions 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());
}
}
Aggregations