use of com.arm.mbed.cloud.sdk.internal.mds.model.NotificationData in project mbed-cloud-sdk-java by ARMmbed.
the class NotificationHandlersStore method handleSubscriptions.
private void handleSubscriptions(List<NotificationData> notifications) {
if (notifications == null) {
return;
}
for (final NotificationData notification : notifications) {
if (notification == null) {
continue;
}
Object value = null;
Throwable throwable = null;
try {
value = decodePayload(notification.getPayload(), notification.getCt());
} catch (DecodingException exception) {
logNotificationError(exception);
throwable = exception;
}
subscriptionHandlers.handleNotification(notification.getEp(), notification.getPath(), value, throwable);
}
}
use of com.arm.mbed.cloud.sdk.internal.mds.model.NotificationData in project mbed-cloud-sdk-java by ARMmbed.
the class TestNotificationHandling method testNotifyNotificationMessage.
@SuppressWarnings("boxing")
@Test
public void testNotifyNotificationMessage() {
Future<?> handle = null;
ScheduledExecutorService executor = null;
String[] payloads = { "MQ==", "Mg==", "Mw==", "NA==", "NQ==" };
try {
executor = Executors.newScheduledThreadPool(1);
NotificationHandlersStore store = new NotificationHandlersStore(null, null, executor, null);
List<Integer> receivedNotificationsUsingObservers = new LinkedList<>();
List<Integer> receivedNotificationsUsingCallbacks = new LinkedList<>();
List<Throwable> receivedErrorsUsingCallbacks = new LinkedList<>();
String deviceId = "015f4ac587f500000000000100100249";
String resourcePath = "/3200/0/5501";
Resource resource = new Resource(deviceId, resourcePath);
store.createResourceSubscriptionObserver(resource, BackpressureStrategy.BUFFER).subscribe(object -> receivedNotificationsUsingObservers.add(Integer.parseInt(String.valueOf(object.getRawValue()))));
store.registerSubscriptionCallback(resource, new Callback<Object>() {
@Override
public void execute(Object arg) {
System.out.println("Received notification: " + arg);
receivedNotificationsUsingCallbacks.add(Integer.parseInt(String.valueOf(arg)));
}
}, new Callback<Throwable>() {
@Override
public void execute(Throwable arg) {
System.err.println("Error happened during notification handling: " + arg);
receivedErrorsUsingCallbacks.add(arg);
}
});
// The following should not have any impact.
store.deregisterNotificationSubscriptionCallback(new Resource(deviceId, "/3200/0/5502"));
store.removeResourceSubscriptionObserver(new Resource(deviceId, "/3200/0/5503"));
int Interval = 100;
handle = executor.scheduleWithFixedDelay(new Runnable() {
List<String> payloadList = Arrays.asList(payloads);
private int i = 0;
@Override
public void run() {
if (i < payloadList.size()) {
NotificationMessage notifications = new NotificationMessage();
NotificationData notification = new NotificationData();
notification.setEp(deviceId);
notification.setPath(resourcePath);
notification.setPayload(payloadList.get(i));
notifications.addNotificationsItem(notification);
store.notify(null);
store.notify(notifications);
i++;
}
}
}, 0, Interval, TimeUnit.MILLISECONDS);
Thread.sleep((payloads.length + 1) * Interval);
assertTrue(receivedErrorsUsingCallbacks.isEmpty());
assertFalse(receivedNotificationsUsingCallbacks.isEmpty());
assertFalse(receivedNotificationsUsingObservers.isEmpty());
for (int i = 0; i < payloads.length; i++) {
assertEquals(i + 1, receivedNotificationsUsingCallbacks.get(i), 0);
assertEquals(i + 1, receivedNotificationsUsingObservers.get(i), 0);
}
store.shutdown();
if (handle != null) {
handle.cancel(true);
}
executor.shutdownNow();
} catch (Exception e) {
if (handle != null) {
handle.cancel(true);
}
if (executor != null) {
executor.shutdownNow();
}
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.arm.mbed.cloud.sdk.internal.mds.model.NotificationData 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