use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.
the class AccountManagementExamples method getAccount.
/**
* Gets details of the account associated with this API key.
*/
@Example
public void getAccount() {
ConnectionOptions config = Configuration.get();
AccountManagement api = new AccountManagement(config);
try {
Account thisAccount;
thisAccount = api.getAccount();
log("This account", thisAccount);
} catch (Exception e) {
logError("last API Metadata", api.getLastApiMetadata());
fail(e.getMessage());
}
}
use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.
the class AccountManagementExamples method listApiKeys.
/**
* Lists the first 5 API Keys.
*/
@SuppressWarnings("boxing")
@Example
public void listApiKeys() {
ConnectionOptions config = Configuration.get();
AccountManagement api = new AccountManagement(config);
try {
// Defining query options
ApiKeyListOptions options = new ApiKeyListOptions();
options.setLimit(5);
// Listing API keys.
Paginator<ApiKey> apikeys = api.listAllApiKeys(options);
for (ApiKey apiKey : apikeys) {
log("API key", apiKey);
}
} catch (Exception e) {
logError("last API Metadata", api.getLastApiMetadata());
fail(e.getMessage());
}
}
use of utils.Example in project mbed-cloud-sdk-java by ARMmbed.
the class AccountManagementExamples method listUsers.
/**
* Lists the first 5 active users.
*/
@SuppressWarnings("boxing")
@Example
public void listUsers() {
ConnectionOptions config = Configuration.get();
AccountManagement api = new AccountManagement(config);
try {
// Defining query options
UserListOptions options = new UserListOptions();
options.setLimit(5);
// Listing users.
Paginator<User> users = api.listAllUsers(options);
for (User user : users) {
log("User", user);
}
} catch (Exception e) {
logError("last API Metadata", api.getLastApiMetadata());
fail(e.getMessage());
}
}
use of utils.Example 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());
}
}
use of utils.Example 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());
}
}
Aggregations