用例:收集较低级别的运动事件,将它们关联起来以删除重复项(例如,一个人绕着房子走动,在一个摄像头前经过,然后经过另一个摄像头),然后报告相关的检测事件。
方法:(见图)我从视频分析和其他传感器发起运动事件,这些事件由 AwarenessAnalytics 组件接收并关联,然后向家庭自动化主组件发出检测事件。它类似于责任链模式,但与事件相反。
我在同一个包的不同文件中定义了两个完全独立的事件接口(interface);
public interface MotionEventListener {
public void motionDetected(MotionEvent event);
public void motionLocation (MotionLocation location);
public void motionCeased(MotionEvent event);
public void eventVideoComplete(String eventId);
}
public interface DetectionEventListener {
public void motionIsDetected(DetectionEvent event);
public void motionAtLocation (MotionLocation location);
public void motionHasCeased(DetectionEvent event);
public void eventVideoNowComplete(String eventId);
}
我在 VideoAnalytic 线程中创建运动事件;
private synchronized void fireDetectedEvent() {
Object source = new Object();
alertStartTime = getDateTime();
eventId++;
System.out.println("*** Motion Detected! ***" + alertStartTime + ", eventId = " +
eventId);
// Send alert to listener
String details ="";
MotionEvent event = new MotionEvent(source, alertActive, eventId,
Calendar.getInstance(), cameraId, Classification.unknown, details, alertStartTime);
Iterator i = listeners.iterator();
if (alertActive) {
while(i.hasNext()) {
((MotionEventListener) i.next()).motionDetected(event);
}
} else {
while(i.hasNext()) {
((MotionEventListener) i.next()).motionCeased(event);
}
resetVideoStreamEventCounter = 0;// keeps track of how many video resets occur from one
//event to another
}
}
运动事件已在 AwarenessAnalytic 层中成功捕获,如果还没有正在进行的事件,我会在其中创建一个新的检测事件;
public void motionDetected(MotionEvent e) {
System.out.println("Motion Detected Listener activated " + e.getCameraId());
if (alertCounter == 0) {
Object source = new Object();
System.out.println("*** Motion Detected! ***" );
// Send alert to listener
alertCounter++;
alertId++;
alertActive = true;
DetectionEvent event = new DetectionEvent(
source,
alertActive,
alertId,
e.getEventDateTime(),
e.getCameraId(),
e.getEventType(),
e.getKnownDetails(),
e.getEventSubdirectory());
Iterator i = listeners.iterator();
if (alertActive) {
while(i.hasNext()) {
((DetectionEventListener) i.next()).motionDetected(event);
}
} else {
alertCounter++;
}
}
System.out.println("Motion Detected event received by AA from " + e.getCameraId());
}
设计画报:
问题:
我 try catch Home Automation Main 中的事件,如下所示;
AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics();
// establish the listener set
awarenessAnalytic.addListener(this);
但是,这会导致编译错误“无法在静态上下文中使用它”
为此我需要一个单独的监听器类吗?还是别的什么?
最佳答案
@Kon 提供了解决这个问题所需的线索(他是值得称赞的人)。我创建了一个单独的DetectionListener 类来实现DetectionEventListener;
public class DetectionListener implements DetectionEventListener {
public DetectionListener() {
super();
}
public void motionIsDetected(DetectionEvent e) {
System.out.println("Motion Detected Awareness Listener test driver activated " +
e.getCameraId());
}
public void motionAtLocation (MotionLocation e) {
System.out.println("Test driver Motion location = " + e.getX() + ", " + e.getY());
}
public void motionHasCeased(DetectionEvent e) {
System.out.println("Motion Ceased Listener test driver activated from " +
e.getCameraId());
}
public void eventVideoNowComplete (String eventId) {
System.out.println("Event Video test driver activated");
}
}
然后在Home Automation Main中设置AwarenessAnalytics实例、DetectionListener实例,并将其添加到AwarenessAnalytics实例中;
AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics();
// establish the listener set
DetectionEventListener Del = new DetectionListener();
awarenessAnalytic.addListener(Del);
现在我需要从DetectionListener 调用main 来完成循环并对事件采取行动。
关于java - 在 java 中通过架构层向上冒泡事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174232/