通过java中的架构层向上冒泡事件

时间:2014-03-04 14:10:10

标签: java events layer

使用案例:收集较低级别的动作事件,关联它们以删除重复项目(例如,一个人在一个房子周围走过,然后在另一个摄像头前面经过,然后报告相关的检测事件)。

方法:(参见图片)我从视频分析和其他传感器启动运动事件,这些传感器由AwarenessAnalytics组件接收并关联,然后将检测事件提升到家庭自动化主站。它类似于责任链模式,但与事件相反。

我在同一个包中的不同文件中定义了两个完全独立的事件接口;

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图层中成功捕获了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);

但是,这会导致编译错误“无法在静态上下文中使用它”

我需要一个单独的监听器类吗?或其他什么?

1 个答案:

答案 0 :(得分:1)

@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来完成循环并对事件采取行动。