示例#1
0
async def test_fileio_multi_read_write(
        data_list: List[Tuple[bytes, str]]) -> None:
    """"""
    filehandler = fileio.Handler()
    filehandler.props.filedir = FILES_PATH
    for data, filename in data_list:
        filehandler.set_props(filename=filename, mode='wb')
        await filehandler.open()
        async with filehandler:
            await filehandler.send(data)
        filehandler.set_props(filename=filename, mode='rb')
        await filehandler.open()
        async with filehandler:
            output = await filehandler.receive()
        assert output == data
示例#2
0
async def test_fileio_read_new(data: str, _: bytes) -> None:
    """"""
    filehandler = fileio.Handler()
    filehandler.props.filedir = FILES_PATH
    filename = "fileio_read_new"
    if os.path.exists(os.path.join(FILES_PATH, filename + '.pb')):
        os.remove(os.path.join(FILES_PATH, filename + '.pb'))
    filehandler.set_props(filename=filename, mode='rb')
    with pt.raises(OSError):
        await filehandler.open()

    filehandler.set_props(filename=filename, mode='wb')
    await filehandler.open()
    async with filehandler:
        await filehandler.send(data)
示例#3
0
async def test_fileio_read_repeated(data: str, _: bytes) -> None:
    """"""
    filehandler = fileio.Handler()
    filehandler.props.filedir = FILES_PATH
    filename = "fileio_read_repeated"
    filehandler.set_props(filename=filename, mode='wb')
    await filehandler.open()
    async with filehandler:
        await filehandler.send(data)

    filehandler.set_props(filename=filename, mode='rb')
    for _ in range(3):
        await filehandler.open()
        async with filehandler:
            output = await filehandler.receive()
        assert output == data
示例#4
0
async def main() -> None:
    """Set up wiring between subsystems and process until completion."""
    # Sans-I/O Protocols
    protocol = server.Protocol()

    # I/O Endpoints
    serial_endpoint = _serial.Driver()
    websocket_endpoint = websocket.Driver()
    filehandler = fileio.Handler()

    # Server Receive Outputs
    channel: channels.TrioChannel[
        server.ReceiveOutputEvent] = channels.TrioChannel()

    # Initialize State
    protocol.receive.backend.all_states[pb.Parameters] = pb.Parameters(pip=30,
                                                                       peep=10)

    report_interval = 0.03
    last_report_time = time.time()
    try:
        async with channel.push_endpoint:
            async with trio.open_nursery() as nursery:
                nursery.start_soon(
                    functools.partial(_trio.process_all,
                                      channel=channel,
                                      push_endpoint=channel.push_endpoint),
                    protocol,
                    serial_endpoint,
                    websocket_endpoint,
                    None,
                )

                while True:
                    receive_output = await channel.output()
                    if time.time() - last_report_time >= report_interval:
                        logger.info(protocol.receive.backend.all_states[
                            pb.SensorMeasurements])
                        last_report_time = time.time()
                    await _trio.process_protocol_send(
                        receive_output.server_send, protocol, serial_endpoint,
                        websocket_endpoint, filehandler)
                nursery.cancel_scope.cancel()
    except trio.EndOfChannel:
        logger.info('Finished, quitting!')
示例#5
0
async def main() -> None:
    """Set up wiring between subsystems and process until completion."""
    # Sans-I/O Protocols
    protocol = server.Protocol()

    # I/O Endpoints
    serial = endpoints.SerialDriver()
    websocket = endpoints.WebSocketDriver()
    filehandler = fileio.Handler()

    # Server Receive Outputs
    channel: channels.TrioChannel[
        server.ReceiveOutputEvent] = channels.TrioChannel()

    async with channel.pull_endpoint:
        async with trio.open_nursery() as nursery:
            nursery.start_soon(
                functools.partial(_trio.process_all,
                                  channel=channel,
                                  push_endpoint=channel.push_endpoint),
                protocol, serial, websocket, None)
            nursery.start_soon(
                functools.partial(
                    handle_receive_outputs,
                    channel,
                    channel.pull_endpoint.clone(),
                ), protocol, serial, websocket, filehandler)

            for i in range(20):
                await trio.sleep(1)
                await _trio.process_protocol_send(
                    backend.Announcement(
                        message=bytes('Quitting in {} s!'.format(20 - i -
                                                                 1), 'utf-8')),
                    protocol, serial, websocket, filehandler)
            logger.info('Finished, quitting!')
            nursery.cancel_scope.cancel()
示例#6
0
async def main() -> None:
    """Set up wiring between subsystems and process until completion."""
    # Sans-I/O Protocols
    protocol = server.Protocol()

    # I/O Endpoints
    serial_endpoint = _serial.Driver()
    websocket_endpoint = websocket.Driver()
    filehandler = fileio.Handler()

    # Server Receive Outputs
    channel: channels.TrioChannel[
        server.ReceiveOutputEvent] = channels.TrioChannel()

    try:
        async with channel.push_endpoint:
            async with trio.open_nursery() as nursery:
                nursery.start_soon(
                    functools.partial(_trio.process_all,
                                      channel=channel,
                                      push_endpoint=channel.push_endpoint),
                    protocol,
                    serial_endpoint,
                    websocket_endpoint,
                    None,
                )

                while True:
                    receive_output = await channel.output()
                    logger.debug(receive_output)
                    await _trio.process_protocol_send(
                        receive_output.server_send, protocol, serial_endpoint,
                        websocket_endpoint, filehandler)
                nursery.cancel_scope.cancel()
    except trio.EndOfChannel:
        logger.info('Finished, quitting!')
示例#7
0
async def main() -> None:
    """Set up wiring between subsystems and process until completion."""
    # Configure logging
    logger = logging.getLogger()
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

    # Sans-I/O Protocols
    protocol = server.Protocol()

    # I/O Endpoints
    serial_endpoint = _serial.Driver()
    websocket_endpoint = websocket.Driver()
    filehandler = fileio.Handler()

    rotary_encoder = None
    try:
        rotary_encoder = rotaryencoder.Driver()
        try:
            await rotary_encoder.open()
        except exceptions.ProtocolError as err:
            exception = (
                "Unable to connect the rotary encoder, please check the "
                "serial connection. Check if the pigpiod service is running: %s"
            )
            rotary_encoder = None
            logger.error(exception, err)
    except NameError:
        rotary_encoder = None
        logger.warning('Running without rotary encoder support!')

    # Server Receive Outputs
    channel: channels.TrioChannel[
        server.ReceiveOutputEvent] = channels.TrioChannel()

    # Initialize states
    all_states = protocol.receive.backend.all_states
    for state in all_states:
        all_states[state] = state()
    await initialize_states_from_file(all_states, protocol, filehandler)

    try:
        async with channel.push_endpoint:
            async with trio.open_nursery() as nursery:
                nursery.start_soon(
                    # mypy only supports <= 5 args with trio-typing
                    functools.partial(_trio.process_all,
                                      channel=channel,
                                      push_endpoint=channel.push_endpoint),
                    protocol,
                    serial_endpoint,
                    websocket_endpoint,
                    rotary_encoder)

                while True:
                    receive_output = await channel.output()
                    await _trio.process_protocol_send(
                        receive_output.server_send, protocol, serial_endpoint,
                        websocket_endpoint, filehandler)

                    if receive_output.frontend_delayed:
                        nursery.start_soon(
                            frozen_frontend.kill_frozen_frontend)
                nursery.cancel_scope.cancel()
    except trio.EndOfChannel:
        logger.info('Finished, quitting!')
    except KeyboardInterrupt:
        logger.info('Quitting!')
    except trio.MultiError as exc:
        filter_multierror(exc)
        logger.info('Finished, quitting!')
示例#8
0
async def main() -> None:
    """Set up wiring between subsystems and process until completion."""
    # Configure logging
    logger = logging.getLogger()
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

    # Sans-I/O Protocols
    protocol = server.Protocol()

    # I/O Endpoints
    websocket_endpoint = websocket.Driver()

    rotary_encoder = None
    try:
        rotary_encoder = rotaryencoder.Driver()
        await rotary_encoder.open()
    except exceptions.ProtocolError as err:
        exception = (
            "Unable to connect the rotary encoder, please check the "
            "serial connection. Check if the pigpiod service is running: %s")
        rotary_encoder = None
        logger.error(exception, err)

    # I/O File
    filehandler = fileio.Handler()

    # Server Receive Outputs
    channel: channels.TrioChannel[
        server.ReceiveOutputEvent] = channels.TrioChannel()

    # Initialize states with defaults
    all_states = protocol.receive.backend.all_states
    for state in all_states:
        if state is mcu_pb.ParametersRequest:
            all_states[state] = mcu_pb.ParametersRequest(
                mode=mcu_pb.VentilationMode.hfnc,
                ventilating=False,
                fio2=60,
                flow=6)
        else:
            all_states[state] = state()
    await application.initialize_states_from_file(all_states, protocol,
                                                  filehandler)

    # Initialize events log manager
    log_manager = log.Manager(
        sender=protocol.receive.backend.log_events_sender)

    try:
        async with channel.push_endpoint:
            async with trio.open_nursery() as nursery:
                nursery.start_soon(
                    functools.partial(_trio.process_all,
                                      channel=channel,
                                      push_endpoint=channel.push_endpoint),
                    protocol, None, websocket_endpoint, rotary_encoder)
                nursery.start_soon(service_requests, all_states, log_manager)
                nursery.start_soon(simulate_states, all_states, log_manager)

                while True:
                    receive_output = await channel.output()
                    await _trio.process_protocol_send(
                        receive_output.server_send, protocol, None,
                        websocket_endpoint, filehandler)

                    if receive_output.frontend_delayed:
                        print("calling")
                        nursery.start_soon(
                            frozen_frontend.kill_frozen_frontend)

                nursery.cancel_scope.cancel()
    except trio.EndOfChannel:
        logger.info('Finished, quitting!')
    except KeyboardInterrupt:
        logger.info('Quitting!')
    except trio.MultiError as exc:
        application.filter_multierror(exc)
        logger.info('Finished, quitting!')