【问题标题】:Bubbling events up through architectural layers in java通过java中的架构层冒泡事件
【发布时间】: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 引用。要么向侦听器传递一些显式的类实例,要么从实例方法调用代码。
  • 谢谢你,这帮助很大!

标签: java events layer


【解决方案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 来完成循环并对事件采取行动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2011-10-20
    • 2011-08-23
    相关资源
    最近更新 更多