Ejemplo n.º 1
0
 def test_log_event_to_server(self):
     # This should fail gracefully with a bad URL, no exceptions
     # should be raised
     
     event = Event(0)
     system_controller = SystemController(self.event_manager,
                                          'http://localhost:10000/')
     system_controller.log_event_to_server(event)
Ejemplo n.º 2
0
 def setUp(self):
     event_manager = EventManager([])
     self.system_controller = SystemController(event_manager)
Ejemplo n.º 3
0
class TestSystemController(unittest.TestCase):
    def setUp(self):
        event_manager = EventManager([])
        self.system_controller = SystemController(event_manager)
    
    def test_handle_bad_events(self):
        event = Event(1000)
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)
    
    def test_getters(self):
        inputs = self.system_controller.get_input_devices()
        self.assertEqual(inputs, [])
        
        user_list = self.system_controller.get_user_list()
        self.assertEqual(user_list, [])

    def test_system_state_and_keypad_events(self):
        # Check controller's initial state
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED) 
        
        # Try to disarm with KEYPAD_EVENT
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'd')
        self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(), 
                         SystemState.DISARMED)

        # Try to arm with KEYPAD_EVENT
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'a')
        ret_value = self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED)
        self.assertTrue(ret_value)
        
        # Turn off alarms
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 's')
        ret_value = self.system_controller.handle_event(event)
        self.assertTrue(ret_value)

        # Bad input
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, "r3d")
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)



    def test_system_state_caps(self):
        # Check controller's initial state
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED) 
        
        # Try to disarm with KEYPAD_EVENT
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'D')
        self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(), 
                         SystemState.DISARMED)

        # Try to arm with KEYPAD_EVENT
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED)
    
    def test_door_event_handler_closed(self):
        # Arm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)
        
        event = DoorSensorEvent(1, 2, False)
        ret_value = self.system_controller.handle_event(event)
        self.assertEqual(ret_value, False) 
        
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'D')
        self.system_controller.handle_event(event)

        event = DoorSensorEvent(1, 2, False)
        ret_value = self.system_controller.handle_event(event)
        self.assertEqual(ret_value, False) 

    def test_door_event_handler_opened_armed(self):
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)
        
        event = DoorSensorEvent(1, 2, True)
         
        m = mox.Mox()
        mock_raise_alarm = m.CreateMockAnything()
        self.system_controller.door_timer_delay = 0.1
        self.system_controller.raise_alarm = new.instancemethod(mock_raise_alarm,
                                                        self.system_controller)
        mock_raise_alarm(self.system_controller,
                            AlarmEvent(AlarmSeverity.MAJOR_ALARM,"",""))

        m.ReplayAll()
        self.system_controller.handle_event(event)
        thread_count = threading.active_count()
        while threading.active_count() == thread_count:
            time.sleep(0)

        m.VerifyAll()
    
    def test_door_event_handler_opened_disarmed(self):
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)

        event = DoorSensorEvent(1,2,True)
         
        self.system_controller.door_timer_delay = 0.2
        m = mox.Mox()
        mock_raise_alarm = m.CreateMockAnything()
        self.system_controller.raise_alarm = new.instancemethod(mock_raise_alarm,
                                                        self.system_controller)
        m.ReplayAll()
        self.system_controller.handle_event(event)

        # Disarm the system in the mean time.
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'd')
        self.system_controller.handle_event(event)

        thread_count = threading.active_count()
        while threading.active_count() == thread_count:
            time.sleep(0)

        m.VerifyAll()

    def test_window_event_handler(self):
        # Arm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED)
        
        # Test an window opening with system armed
        event = WindowSensorEvent(1, 1, True)
        ret_value = self.system_controller.handle_event(event)
        self.assertTrue(ret_value)
        
        # Test a closed window...
        event = WindowSensorEvent(1,1,False)
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)

        # Disarm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'd')
        self.system_controller.handle_event(event)

         # Test an window opening with system disarmed
        event = WindowSensorEvent(1, 1, True)
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)

        # Test a closed window...
        event = WindowSensorEvent(1,1,False)
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)

    def test_flood_event_handler(self):
        # Arm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'A')
        self.system_controller.handle_event(event)
        self.assertEqual(self.system_controller.get_system_state(),
                         SystemState.ARMED)
        test_vector = [
                        {'height' : 0 , 'delta' : 0, 'ret_value' : False},
                        {'height' : 1 , 'delta' : 0, 'ret_value' : True},
                        {'height' : 0 , 'delta' : 1, 'ret_value' : True},
                        {'height' : 3 , 'delta' : 0, 'ret_value' : True},
                        {'height' : 1 , 'delta' : 3, 'ret_value' : True}
                      ]

        for test in test_vector:
            event = FloodSensorEvent(1,test['height'], test['delta'])
            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, test['ret_value'])

        # Disarm the system -- Nothing should change in the test cases...
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'D')
        self.system_controller.handle_event(event)
        
        for test in test_vector:
            event = FloodSensorEvent(1,test['height'], test['delta'])
            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, test['ret_value'])

    def test_temp_sensor_event(self):
        # TODO :: setup mox to check the alarm event severity...

        # Arm the system.
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'a')
        self.system_controller.handle_event(event)
        
        test_vector = [
                        {'temp' : 0, 'delta' : 0, 'ret_value' : True},
                        {'temp' : 18, 'delta' : 0, 'ret_value' : False},
                        {'temp' : 25, 'delta' : 0, 'ret_value' : False},
                        {'temp' : 27, 'delta' : 0, 'ret_value' : True},
                        {'temp' : 15, 'delta' : 0, 'ret_value' : True},
                        {'temp' : 33, 'delta' : 0, 'ret_value' : True},
                        {'temp' : 25, 'delta' : 2, 'ret_value' : False},
                        {'temp' : 25, 'delta' : 3, 'ret_value' : True},
                        {'temp' : 25, 'delta' : 5, 'ret_value' : True},
                        {'temp' : 25, 'delta' : 10, 'ret_value' : True}
                      ]
        
        for test in test_vector:
            event = TempSensorEvent(1, test['temp'], test['delta'], )
            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, test['ret_value'])
        
        # Disarm the system, ensure that it is system state independent...
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, 'd')
        self.system_controller.handle_event(event)

        for test in test_vector:
            event = TempSensorEvent(1, test['temp'], test['delta'])
            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, test['ret_value'])
        
    def test_motion_event(self):
        # Arm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, "a")
        self.system_controller.handle_event(event)
        
        test_vector = [
                        # Start a motion event, but it doesn't have an end time
                        {'threshold' : 10, 'start_time' : datetime.utcnow(),
                        'end_time' : None, 'ret_value' : False}, 
                        # Motion event that will send an alarm
                        {'threshold' : 10, 'start_time' : datetime.utcnow(),
                        'end_time' : datetime.utcnow() + timedelta(0,3600),
                        'ret_value' : True},
                        # Motion event representing a glitch
                        {'threshold' : 10, 'start_time' : datetime.utcnow(),
                        'end_time' : datetime.utcnow() + timedelta(0,5), 
                        'ret_value' : False},
                        # Motion event in the past that will send an alarm
                        {'threshold' : 10, 
                        'start_time' : datetime(2012, 3, 15, 23, 33),
                        'end_time' : datetime(2012,3,15,23,35), 
                        'ret_value' : True},
                        # Motion event in the past that will not send an alarm
                        {'threshold' : 10, 
                        'start_time' : datetime(2012, 3, 15, 23, 33),
                        'end_time' : datetime(2012,3,15,23,33,5),
                        'ret_value' : False}
                      ]

        # Motion started
        for test in test_vector:
            event = MotionSensorEvent(1,
                                      test['threshold'],
                                      test['start_time'],
                                      test['end_time'])
            

            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, test['ret_value']) 

        # Disarm the system
        event = KeypadEvent(EventType.KEYPAD_EVENT, 1, "d")
        self.system_controller.handle_event(event)
        
        # Everything should return False
        for test in test_vector:
            event = MotionSensorEvent(1, test['threshold'], test['start_time'],
                                      test['end_time'])
            ret_value = self.system_controller.handle_event(event)
            self.assertEqual(ret_value, False)
    
    def test_alarm_handler(self):
        event = AlarmEvent(AlarmSeverity.MINOR_ALARM, "","")
        ret_value = self.system_controller.handle_event(event)
        self.assertTrue(ret_value)
    
    def test_nfc_event_handler(self):
        # Should just return False, nothing should happen in this case since it
        # is outside of implementation scope. This is for complete coverage.
        event = NFCEvent(1,"test")
        ret_value = self.system_controller.handle_event(event)
        self.assertFalse(ret_value)
Ejemplo n.º 4
0
 def test_log_event_to_file(self):
     # Same conditions as test_log_event_to_server()
     event = Event(0)
     system_controller = SystemController(self.event_manager, "")
     system_controller.log_event_to_file(event)
Ejemplo n.º 5
0
    args = parser.parse_args()
    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    # Parse peer list
    peer_list = []
    if args.peers:
        for peer in args.peers.split(','):
            peer_list.append(tuple(peer.split(':')))

    # Setup EventManager and Controllers
    event_manager = EventManager(peer_list, args.port)
    sensor_controller = SensorController(event_manager)
    system_controller = SystemController(event_manager, args.remote_url)

    # Start controllers
    controllers = (system_controller, sensor_controller)

    threads = []
    for controller in controllers:
        thread = threading.Thread(target=controller.run)
        thread.start()
        threads.append(thread)

    logging.info('Controllers started')

    system_controller.log_event_to_server(AlarmEvent(AlarmSeverity.MAJOR_ALARM,
                                                     'GARTH starting up', 
                                                     ''))