Skip to content

Commit

Permalink
KAA-384: JEventDemo, fixed bug, added description, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Yemets committed Mar 10, 2015
1 parent 5acd2a8 commit 265156e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
Expand Up @@ -31,52 +31,80 @@
import org.kaaproject.kaa.client.event.registration.EndpointRegistrationManager;
import org.kaaproject.kaa.client.event.registration.UserAttachCallback;
import org.kaaproject.kaa.client.transact.TransactionId;
import org.kaaproject.kaa.common.endpoint.gen.SyncResponseResultType;
import org.kaaproject.kaa.common.endpoint.gen.UserAttachResponse;
import org.kaaproject.kaa.schema.sample.event.thermo.ChangeDegreeRequest;
import org.kaaproject.kaa.schema.sample.event.thermo.CustomThermoEventClassFamily;
import org.kaaproject.kaa.schema.sample.event.thermo.CustomThermoEventClassFamily.DefaultEventFamilyListener;
import org.kaaproject.kaa.schema.sample.event.thermo.ThermostatEventClassFamily;
import org.kaaproject.kaa.schema.sample.event.thermo.ThermostatEventClassFamily.DefaultEventFamilyListener;
import org.kaaproject.kaa.schema.sample.event.thermo.ThermostatInfoRequest;
import org.kaaproject.kaa.schema.sample.event.thermo.ThermostatInfoResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* This class receives Kaa events and user attach/detach callbacks and dispatch them to other application components via event bus.
* This class demonstrates how to send/receive events to/from endpoints via Kaa event subsystem.
*/
public class EventDemo {

private static final Logger LOG = LoggerFactory.getLogger(EventDemo.class);

// Credentials for attaching user
private static final String USER_EXTERNAL_ID = "userExternalId";
private static final String USER_ACCESS_TOKEN = "userAccessToken";
// Kaa client
private static KaaClient kaaClient;

public static void main(String[] args) {
LOG.info("Event demo started");

KaaClient kaaClient = Kaa.newClient(new DesktopKaaPlatformContext(),
new SimpleKaaClientStateListener());
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
LOG.info("{}", e1);
}

kaaClient = Kaa.newClient(new DesktopKaaPlatformContext(), new SimpleKaaClientStateListener());
kaaClient.start();

EndpointRegistrationManager registrationManager = kaaClient
.getEndpointRegistrationManager();
EndpointRegistrationManager registrationManager = kaaClient.getEndpointRegistrationManager();

registrationManager.attachUser("userExternalId", "userAccessToken",
// Our demo application uses trustful verifier, so it does not matter
// which credentials you would pass to registration manager
registrationManager.attachUser(USER_EXTERNAL_ID, USER_ACCESS_TOKEN,
new UserAttachCallback() {
@Override
public void onAttachResult(UserAttachResponse response) {
LOG.info("Attach response {}", response.getResult());

//If our endpoint was successfully attached
if (response.getResult() == SyncResponseResultType.SUCCESS) {
doWork();
}
//If not - Release all network connections and application resources.
// Shutdown all Kaa client tasks.
else {
kaaClient.stop();
LOG.info("Event demo stopped");
}
}
});

EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
CustomThermoEventClassFamily tecf = eventFamilyFactory
.getCustomThermoEventClassFamily();
}


public static void doWork() {

List<String> FQNs = new LinkedList<>();
FQNs.add(ThermostatInfoRequest.class.getName());
FQNs.add(ChangeDegreeRequest.class.getName());

//Getting event listener resolver
EventListenersResolver eventListenersResolver = kaaClient.getEventListenerResolver();

//And then finding all listener listening to events in FQNs list
eventListenersResolver.findEventListeners(FQNs, new FetchEventListeners() {

//Doing something with event listeners in case of success
@Override
public void onEventListenersReceived(List<String> eventListeners) {
LOG.info("{} event listeners received", eventListeners.size());
Expand All @@ -85,26 +113,19 @@ public void onEventListenersReceived(List<String> eventListeners) {
}
}

//Or if something gone wrong handling fail somehow
@Override
public void onRequestFailed() {
LOG.info("Request failed");
}
});

TransactionId trxId = eventFamilyFactory.startEventsBlock();

// Add events to the block
// Adding a broadcasted event to the block
tecf.addEventToBlock(trxId, new ThermostatInfoRequest());
// Adding a targeted event to the block
tecf.addEventToBlock(trxId, new ChangeDegreeRequest(-30), "home_thermostat");

// Send added events in a batch
eventFamilyFactory.submitEventsBlock(trxId);
// Dismiss the event batch (if the batch was not submitted as shown in the previous line)
// eventFamilyFactory.removeEventsBlock(trxId);

//Getting event family factory
EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
//Getting concrete event family
ThermostatEventClassFamily tecf = eventFamilyFactory.getThermostatEventClassFamily();

//Adding event listeners for family factory
tecf.addListener(new DefaultEventFamilyListener() {

@Override
Expand All @@ -123,16 +144,34 @@ public void onEvent(ThermostatInfoRequest arg0, String arg1) {
}
});


//Broadcasting ChangeDegreeRequest event
tecf.sendEventToAll(new ChangeDegreeRequest(10));
LOG.info("ChangeDegreeRequest sent");

TransactionId trxId = eventFamilyFactory.startEventsBlock();
// Add events to the block
// Adding a broadcasted event to the block
tecf.addEventToBlock(trxId, new ThermostatInfoRequest());
// Adding a targeted event to the block
tecf.addEventToBlock(trxId, new ChangeDegreeRequest(-30), "home_thermostat");

// Send added events in a batch
eventFamilyFactory.submitEventsBlock(trxId);
// Dismiss the event batch (if the batch was not submitted as shown in the previous line)
// eventFamilyFactory.removeEventsBlock(trxId);


try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}

//Release all network connections and application resources.
//Shutdown all Kaa client tasks.
kaaClient.stop();

LOG.info("Event demo stopped");
}


}
Expand Up @@ -51,11 +51,11 @@ protected void buildDemoApplicationImpl(AdminClient client) throws Exception {
loginTenantAdmin(client);

EventClassFamilyDto thermoEventClassFamily = new EventClassFamilyDto();
thermoEventClassFamily.setName("Thermo Event Class Family");
thermoEventClassFamily.setName("Thermostat Event Class Family");
thermoEventClassFamily.setNamespace("org.kaaproject.kaa.schema.sample.event.thermo");
thermoEventClassFamily.setClassName("CustomThermoEventClassFamily");
thermoEventClassFamily.setClassName("ThermostatEventClassFamily");
thermoEventClassFamily = client.editEventClassFamily(thermoEventClassFamily);
client.addEventClassFamilySchema(thermoEventClassFamily.getId(), "demo/jevent/thermoEventClassFamily.json");
client.addEventClassFamilySchema(thermoEventClassFamily.getId(), "demo/jevent/thermostatEventClassFamily.json");

ApplicationDto jeventApplication = new ApplicationDto();
jeventApplication.setName("Java Event Demo");
Expand Down

0 comments on commit 265156e

Please sign in to comment.