예제 #1
0
def get_system_state():
    mode = settings.get_mode()

    if mode == MODE_OFF:
        return dict(mode=MODE_OFF, time=current_time_s(), sensors=[])

    last_update = storage.get_last_update()
    if not last_update:
        last_update = dict(sensors=[])
        last_update['time'] = current_time_s()

    last_update['mode'] = settings.get_mode()
    last_update['state'] = __state(last_update['sensors'])
    return last_update
예제 #2
0
    def test_alarm_flow(self, mock_get_mode, mock_current_time, mock_notifier):
        last_update = update(1, 0, 0)
        last_update['time'] = 1453306171
        storage.save_last_update(last_update)

        handler.on_new_update(update(0, 1, 0))

        expected_system_state = update(1, 1, 0)
        expected_system_state['mode'] = 'guard'
        expected_system_state['state'] = 'alarm'
        expected_system_state['time'] = 1453306171

        actual_system_state = mock_notifier.notify.call_args[0][1]
        self.assertEqual('guard', actual_system_state['mode'])
        self.assertEqual('alarm', actual_system_state['state'])
        self.assertEqual(1453306171, actual_system_state['time'])
        self.assertCountEqual(expected_system_state['sensors'], actual_system_state['sensors'])

        actual_stored_last_update = storage.get_last_update()
        expected_stored_last_update = update(1, 1, 0)

        self.assertCountEqual(expected_stored_last_update['sensors'], actual_stored_last_update['sensors'])
예제 #3
0
def on_new_update(new_update):
    new_update['time'] = current_time_s()
    last_update = storage.get_last_update()

    if settings.get_mode() == MODE_GUARD and last_update:
        new_update['sensors'] = utils.merge_sensors(new_update['sensors'], last_update['sensors'])

    storage.save_last_update(new_update)

    if not last_update:
        return

    result, sensors = Comparator().compare(last_update, new_update)

    if result != Comparator.RESULT_EQUALS:
        system_state = get_system_state()
        notifier.notify(result, system_state)
        saved_rows = storage.add_to_log(system_state)

        if saved_rows == 0:
            raise OperationalError

    else:
        log.d('No changes since last update')