Beispiel #1
0
def test_sync_new_elements() -> None:
    """Test adding new elements to a list."""
    example_sequence = [
        lists.UpdateEvent(new_element=pb.LogEvent(id=i)) for i in range(20)
    ]

    synchronizer = lists.SendSynchronizer(segment_type=pb.NextLogEvents,
                                          max_len=10,
                                          max_segment_len=5)
    assert synchronizer.output() is None
    for update_event in example_sequence:
        synchronizer.input(update_event)

    assert synchronizer.output() is None

    # The first 10 events should've been discarded
    for next_expected in range(10):
        synchronizer.input(lists.UpdateEvent(next_expected=next_expected))
        output = synchronizer.output()
        assert isinstance(output, pb.NextLogEvents)
        assert output.next_expected == next_expected
        assert output.total == 10
        assert output.remaining == 10
        for (i, event) in enumerate(output.elements):
            assert event.id == 10 + i

    # Segments should be returned as requested
    for next_expected in range(10, 20):
        print(next_expected)
        synchronizer.input(lists.UpdateEvent(next_expected=next_expected))
        output = synchronizer.output()
        assert isinstance(output, pb.NextLogEvents)
        assert output.next_expected == next_expected
        assert output.total == 10
        assert output.remaining == 10 - (next_expected - 10)
        for (i, event) in enumerate(output.elements):
            assert event.id == next_expected + i
        if next_expected <= 15:
            assert len(output.elements) == 5
        else:
            assert len(output.elements) == 5 - (next_expected - 15)

    # New elements should be in the segment resulting from a repeated request
    assert synchronizer.output() is None
    synchronizer.input(
        lists.UpdateEvent(new_element=pb.LogEvent(id=20), next_expected=19))
    output = synchronizer.output()
    assert isinstance(output, pb.NextLogEvents)
    assert output.next_expected == 19
    assert output.total == 10
    assert output.remaining == 2
    for (i, event) in enumerate(output.elements):
        assert event.id == 19 + i
    assert len(output.elements) == 2
Beispiel #2
0
def service_ventilating(
        request: mcu_pb.ParametersRequest, response: mcu_pb.Parameters,
        log_manager: log.Manager
) -> None:
    """Handle the request's ventilation operation status."""
    if response.ventilating == request.ventilating:
        return

    log_manager.add_event(mcu_pb.LogEvent(
        code=mcu_pb.LogEventCode.ventilation_operation_changed,
        type=mcu_pb.LogEventType.control,
        old_bool=response.ventilating, new_bool=request.ventilating
    ))
    response.ventilating = request.ventilating
Beispiel #3
0
def service_mode(
        request: mcu_pb.ParametersRequest, response: mcu_pb.Parameters,
        log_manager: log.Manager
) -> None:
    """Handle the request's ventilation mode."""
    if response.mode == request.mode:
        return

    log_manager.add_event(mcu_pb.LogEvent(
        code=mcu_pb.LogEventCode.ventilation_mode_changed,
        type=mcu_pb.LogEventType.control,
        old_mode=response.mode, new_mode=request.mode
    ))
    response.mode = request.mode
Beispiel #4
0
    def service_flow(
            self, request: mcu_pb.ParametersRequest,
            response: mcu_pb.Parameters, log_manager: log.Manager
    ) -> None:
        """Handle the request's flow rate."""
        if not self.FLOW_MIN <= request.flow <= self.FLOW_MAX:
            return

        if response.flow == request.flow:
            return

        log_manager.add_event(mcu_pb.LogEvent(
            code=mcu_pb.LogEventCode.flow_setting_changed,
            type=mcu_pb.LogEventType.control,
            old_float=response.flow, new_float=request.flow
        ))
        response.flow = request.flow
Beispiel #5
0
    def service_fio2(
            self, request: mcu_pb.ParametersRequest,
            response: mcu_pb.Parameters, log_manager: log.Manager
    ) -> None:
        """Handle the request's FiO2."""
        if not self.FIO2_MIN <= request.fio2 <= self.FIO2_MAX:
            return

        if response.fio2 == request.fio2:
            return

        print(request.fio2, response.fio2)
        log_manager.add_event(mcu_pb.LogEvent(
            code=mcu_pb.LogEventCode.fio2_setting_changed,
            type=mcu_pb.LogEventType.control,
            old_float=response.fio2, new_float=request.fio2
        ))
        response.fio2 = request.fio2
Beispiel #6
0
def service_limits_range(
        request: mcu_pb.Range, response: mcu_pb.Range, floor: int, ceiling: int,
        code: mcu_pb.LogEventCode, log_manager: log.Manager
) -> None:
    """Handle the request's alarm limits range."""
    if request.lower == response.lower and request.upper == response.upper:
        return

    (new_lower, new_upper) = transform_limits_range(
        floor, ceiling, request.lower, request.upper,
        response.lower, response.upper
    )
    old_response = dataclasses.replace(response)
    response.lower = new_lower
    response.upper = new_upper
    new_response = dataclasses.replace(response)
    log_manager.add_event(mcu_pb.LogEvent(
        code=code, type=mcu_pb.LogEventType.alarm_limits,
        old_range=old_response, new_range=new_response
    ))