2014-03-04 52 views
0

使用案例:收集較低級別的動作事件,將它們關聯以刪除重複項(例如,在房子周圍走過的人通過一個攝像頭,然後通過另一個攝像頭),然後報告關聯檢測事件。在java中通過建築圖層冒泡事件

方法:(請參閱圖片)我從AwarenessAnalytics組件接收並關聯的視頻分析和其他傳感器中啓動Motion Events,然後將檢測事件引發到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線程中創建Motion事件;

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層,在那裏我createa新的檢測事件,如果沒有已經正在進行的事件;

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()); 
} 

設計圖案:

問題:

我試圖趕上家庭自動化的主要事件如下:

AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics(); 

// establish the listener set 
awarenessAnalytic.addListener(this); 

然而,這會導致編譯錯誤

我需要爲這個單獨的偵聽器類「不能在靜態環境中使用該」?或者是其他東西?

+1

您正在從靜態方法運行問題代碼,該方法沒有隱含的'this'引用。通過偵聽器一些顯式的類實例,或從實例方法調用代碼。 – Kon

+1

謝謝,這非常有幫助! – Will

回答

1

@Kon提供瞭解決此問題所需的線索(他是值得信任的人)。我創建了一個實現了DetectionEventListener的分離的DetectionListener類;

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"); 
    } 
} 

然後在家庭自動化主要建立AwarenessAnalytics例如,DetectionListener實例,並把它添加到AwarenessAnalytics實例;

AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics(); 
// establish the listener set 
DetectionEventListener Del = new DetectionListener(); 
awarenessAnalytic.addListener(Del); 

現在我需要從DetectionListener調用main來完成該圓並對事件採取行動。