StatusCache implements DeployerCommandListener, DeployerSensorListener { public synchronized void update(Event event) { if (isShutdownInProcess) { log.debug( "Device state cache is shutting down. Ignoring update from ''{0}'' (ID = ''{1}'').", event.getSource(), event.getSourceID() ); return; } EventContext ctx = new EventContext(this, event, eventProcessorChain.getCommandFacade()); eventProcessorChain.push(ctx); if (!ctx.hasTerminated()) { sensorMap.update(ctx.getEvent()); } else { log.debug( "Event {0} was terminated by event processors. No update was made to device state cache.", ctx.getEvent() ); } } StatusCache();  StatusCache(ChangedStatusTable cst, EventProcessorChain epc); @Override void onCommandsDeployed(Set<Command> commands); @Override void onSensorsDeployed(Set<Sensor> sensors); void start(); synchronized void registerSensor(Sensor sensor); Sensor getSensor(int id); synchronized void shutdown(); synchronized void update(Event event); Map<Integer, String> queryStatus(Set<Integer> sensorIDs); String queryStatus(Integer sensorID); Event queryStatus(String name); Iterator<Event> getStateSnapshot(); void initializeEventContext(Set<Command> commands); Integer sensorIDFromName(String sensorName);  }
@Test public void testUpdate() { ChangedStatusTable cst = new ChangedStatusTable(); EventProcessorChain epc = new EventProcessorChain(); List<EventProcessor> processors = new ArrayList<EventProcessor>(); TestProcessor tp = new TestProcessor(); processors.add(tp); epc.setEventProcessors(processors); StatusCache cache = new StatusCache(cst, epc); DummyCommand cmd = new DummyCommand("on"); Sensor s = new SwitchSensor("test-sensor", 2, cache, cmd, 1); cache.registerSensor(s); Assert.assertTrue(Sensor.isUnknownSensorValue(cache.queryStatus(2))); s.start(); waitForUpdate(); Assert.assertTrue(cache.queryStatus(2).equals("on")); }
@Test public void testUpdateStatusChange() { ChangedStatusTable cst = new ChangedStatusTable(); EventProcessorChain epc = new EventProcessorChain(); StatusCache cache = new StatusCache(cst, epc); Event evt = new Switch(1, "test1", "foo", Switch.State.OFF); Set<Integer> pollingSensorIDs = new TreeSet<Integer>(); pollingSensorIDs.add(1); cst.insert(new ChangedStatusRecord("My Device-1", pollingSensorIDs)); cache.update(evt); ChangedStatusRecord record = cst.query("My Device-1"); Assert.assertTrue(record.getRecordKey().equals("My Device-1")); Assert.assertTrue(record.getPollingSensorIDs().contains(1)); Assert.assertTrue(record.getPollingSensorIDs().size() == 1); Assert.assertTrue(record.getStatusChangedSensorIDs().contains(1)); Assert.assertTrue(record.getStatusChangedSensorIDs().size() == 1); evt = new Level(30, "test30", 30); pollingSensorIDs = new TreeSet<Integer>(); pollingSensorIDs.add(10); pollingSensorIDs.add(20); pollingSensorIDs.add(30); pollingSensorIDs.add(40); cst.insert(new ChangedStatusRecord("Another Device-10,20,30,40", pollingSensorIDs)); cache.update(evt); pollingSensorIDs.remove(new Integer(10)); record = cst.query("Another Device-10,20,30,40"); Assert.assertTrue(record.getRecordKey().equals("Another Device-10,20,30,40")); Assert.assertTrue(record.getPollingSensorIDs().contains(20)); Assert.assertTrue(record.getPollingSensorIDs().contains(30)); Assert.assertTrue(record.getPollingSensorIDs().contains(40)); Assert.assertTrue(record.getPollingSensorIDs().size() == 3); Assert.assertTrue(record.getStatusChangedSensorIDs().contains(30)); Assert.assertTrue(record.getStatusChangedSensorIDs().size() == 1); Event evt1 = new Range(100, "test100", 100, -1000, 1000); Event evt2 = new CustomState(200, "test200", "acme"); pollingSensorIDs = new TreeSet<Integer>(); pollingSensorIDs.add(100); pollingSensorIDs.add(200); pollingSensorIDs.add(300); pollingSensorIDs.add(400); cst.insert(new ChangedStatusRecord("Third Device-100,200,300,400", pollingSensorIDs)); cache.update(evt1); cache.update(evt2); record = cst.query("Third Device-100,200,300,400"); Assert.assertTrue(record.getRecordKey().equals("Third Device-100,200,300,400")); Assert.assertTrue(record.getPollingSensorIDs().contains(100)); Assert.assertTrue(record.getPollingSensorIDs().contains(200)); Assert.assertTrue(record.getPollingSensorIDs().contains(300)); Assert.assertTrue(record.getPollingSensorIDs().contains(400)); Assert.assertTrue(record.getPollingSensorIDs().size() == 4); Assert.assertTrue(record.getStatusChangedSensorIDs().contains(100)); Assert.assertTrue(record.getStatusChangedSensorIDs().contains(200)); Assert.assertTrue(record.getStatusChangedSensorIDs().size() == 2); }