Example #1
0
    def test_merge_sensors(self):
        s1 = [{'name': 's_1', 'state': 0},
              {'name': 's_2', 'state': 1},
              {'name': 's_3', 'state': 0}]

        s2 = [{'name': 's_4', 'state': 0},
              {'name': 's_2', 'state': 1},
              {'name': 's_3', 'state': 1}]

        expected = [{'name': 's_1', 'state': 0},
                    {'name': 's_2', 'state': 1},
                    {'name': 's_3', 'state': 1},
                    {'name': 's_4', 'state': 0}]

        actual = merge_sensors(s1, s2)
        self.assertCountEqual(expected, actual)
Example #2
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')