【发布时间】:2014-03-04 14:10:09
【问题描述】:
用例:收集较低级别的 Motion 事件,将它们关联起来以删除重复项(例如,一个人在房子周围走动,经过一个摄像头,然后经过另一个摄像头),然后报告相关的检测事件。
方法:(见图)我从视频分析和其他传感器启动运动事件,这些传感器由 AwarenessAnalytics 组件接收并关联,然后向 Home Automation Main 发起检测事件。它类似于责任链模式,但与事件相反。
我在同一个包的不同文件中定义了两个完全独立的事件接口;
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 层成功捕获了 Motion 事件,如果还没有正在进行的事件,我将在该层创建一个新的检测事件;
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());
}
设计画报:
问题:
我尝试在 Home Automation Main 中捕获事件,如下所示;
AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics();
// establish the listener set
awarenessAnalytic.addListener(this);
但是,这会导致编译错误“不能在静态上下文中使用它”
我需要一个单独的监听器类吗?还是别的什么?
【问题讨论】:
-
您正在从静态方法运行问题代码,该方法没有隐式
this引用。要么向侦听器传递一些显式的类实例,要么从实例方法调用代码。 -
谢谢你,这帮助很大!