我正在为我的一个IoT应用程序试验Kaa事件子系统。我创建了一个事件类系列,并将其添加到其中一个项目中,将该属性保留为" Both"因为我想发送/接收两者。我的事件触发代码如下所示:
DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new SimpleKaaClientStateListener() {
@Override
public void onStarted() {
System.out.println("--= Kaa client started =--");
}
@Override
public void onStopped() {
System.out.println("--= Kaa client stopped =--");
}
}, true);
// Obtain the event family factory.
final EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
Random rnRandom = new Random();
event1 = eventFamilyFactory.getEventClassFamilyDemo();
DemoEventsCTL event = new DemoEventsCTL(rnRandom.nextBoolean());
kaaClient.start();
event.setMachineOn(rnRandom.nextBoolean());
event1.sendEventToAll(event);
System.out.println("Hello : Event : " + event.getMachineOn() + "has been addded");
}
}
我的事件处理程序如下所示: DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new SimpleKaaClientStateListener() {
@Override
public void onStarted() {
LOG.info("--= Kaa client started =--");
}
@Override
public void onStopped() {
LOG.info("--= Kaa client stopped =--");
}
}, true);
final EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
event1 = eventFamilyFactory.getEventClassFamilyDemo();
event1.addListener(new EventClassFamilyDemo.Listener() {
@Override
public void onEvent(DemoEventsCTL event, String source) {
System.out.println(kaaClient.isAttachedToUser());
System.out.println("The event received is :" + event.getMachineOn());
});
// Start the Kaa client and connect it to the Kaa server.
kaaClient.start();
}
当我单独执行这些应用程序时,两者都运行时没有任何错误日志,我看到与服务器的连接成功。但是,我没有看到收到的已发送事件。 代理会创建任何问题吗?如果有人以前尝试过,你能告诉我我是否遗漏了什么?
答案 0 :(得分:0)
以下是我使用的两个Kaa客户端实现。一个发出事件,另一个消耗这些事件。
您可以看到两个客户端都连接到同一个用户("testverifier", "82884822542100000000")
。
首先,我运行EventConsumer
,以便在我启动它时准备好捕获EventEmitter
发送的事件。
<强> EventConsumer 强>
public class TestEventConsumer {
private static KaaClient kaaClient;
private static ScheduledExecutorService scheduledExecutorService;
public static void main(final String[] args) {
System.out.println(SampleEventListener.class.getSimpleName() + " Event Consumer Starting!");
scheduledExecutorService = Executors.newScheduledThreadPool(1);
// Create the Kaa desktop context for the application.
final DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new FirstKaaClientStateListener(), true);
kaaClient.start();
consumeEvents();
System.out.println("--= Press any key to exit =--");
try {
System.in.read();
} catch (final IOException e) {
System.out.println("IOException has occurred: {}" + e.getMessage());
}
System.out.println("Stopping Event Consumer...");
scheduledExecutorService.shutdown();
kaaClient.stop();
}
private static class FirstKaaClientStateListener extends SimpleKaaClientStateListener {
@Override
public void onStarted() {
super.onStarted();
System.out.println("Kaa client (Event Consumer) started");
}
@Override
public void onStopped() {
super.onStopped();
System.out.println("Kaa client (Event Consumer) stopped");
}
}
public static void consumeEvents() {
kaaClient.attachUser("testverifier", "82884822542100000000", new UserAttachCallback() {
@Override
public void onAttachResult(final UserAttachResponse response) {
System.out.println("Attach response" + response.getResult());
}
});
final EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
final MyCustomECF myEcf = eventFamilyFactory.getMyCustomECF();
myEcf.addListener(new MyCustomECF.Listener() {
@Override
public void onEvent(final MyCustomEvent event, final String source) {
System.out.println("MyCustomEvent received from source : " + source + "Event Data Name : " + event.getName());
}
});
}
}
<强> EventEmitter 强>
public class TestEventEmitter {
private static KaaClient kaaClient;
private static ScheduledExecutorService scheduledExecutorService;
public static void main(final String[] args) {
scheduledExecutorService = Executors.newScheduledThreadPool(1);
// Create the Kaa desktop context for the application.
final DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new FirstKaaClientStateListener(), true);
kaaClient.start();
attachUserandEmitEvents();
System.out.println("--= Press any key to exit =--");
try {
System.in.read();
} catch (final IOException e) {
System.out.println("IOException has occurred: {}" + e.getMessage());
}
System.out.println("Stopping Event Emitter...");
scheduledExecutorService.shutdown();
kaaClient.stop();
}
private static void attachUserandEmitEvents() {
// attach user
kaaClient.attachUser("testverifier", "82884822542100000000", new UserAttachCallback() {
@Override
public void onAttachResult(final UserAttachResponse response) {
final EventFamilyFactory eventFamilyFactory = kaaClient.getEventFamilyFactory();
final MyCustomECF myEcf = eventFamilyFactory.getMyCustomECF();
for (int i = 0; i < 5; i++) {
MyCustomEvent event = new MyCustomEvent();
event.setCustId("ID00" + i);
event.setName("Name - " + i);
myEcf.sendEventToAll(event);
}
}
});
}
private static class FirstKaaClientStateListener extends SimpleKaaClientStateListener {
@Override
public void onStarted() {
super.onStarted();
System.out.println("Kaa client (Event Emitter) started");
}
@Override
public void onStopped() {
super.onStopped();
System.out.println("Kaa client (Event Emitter) stopped");
}
}
}