async def _live_reader(session: BaseSession, my_stream: DataStream, pipe_out: Pipe): log.info("requesting live connection to [%s]" % my_stream.name) params = {'id': my_stream.id, 'subscribe': '1'} try: raw_session = await session.get_session() async with raw_session.get(session.url + "/data", params=params, ssl=session.ssl_context) as response: if response.status != 200: # pragma: no cover msg = await response.text() log.error("Error reading input [%s]: %s" % (my_stream.name, msg)) await pipe_out.close() return pipe_out.change_layout(response.headers['joule-layout']) pipe_out.decimation_level = int( response.headers['joule-decimation']) pipe_in = InputPipe(layout=pipe_out.layout, stream=my_stream, reader=response.content) while True: data = await pipe_in.read() pipe_in.consume(len(data)) await pipe_out.write(data) if pipe_in.end_of_interval: await pipe_out.close_interval() except (asyncio.CancelledError, EmptyPipe, aiohttp.ClientError): pass except Exception as e: print("unexpected exception: ", e) raise e await pipe_out.close()
async def _historic_reader(session: BaseSession, my_stream: DataStream, pipe_out: Pipe, start_time: int, end_time: int, max_rows: Optional[int]): log.info("requesting historic connection to [%s]" % my_stream.name) params = {'id': my_stream.id} if max_rows is not None: params['max-rows'] = max_rows if start_time is not None: params['start'] = int(start_time) if end_time is not None: params['end'] = int(end_time) try: my_session = await session.get_session() async with my_session.get(session.url + "/data", params=params, ssl=session.ssl_context) as response: if response.status != 200: # pragma: no cover msg = await response.text() log.error("Error reading input [%s]: %s" % (my_stream.name, msg)) await pipe_out.close() return pipe_out.change_layout(response.headers['joule-layout']) pipe_out.decimation_level = int( response.headers['joule-decimation']) pipe_in = InputPipe(layout=pipe_out.layout, stream=my_stream, reader=response.content) while True: data = await pipe_in.read() pipe_in.consume(len(data)) await pipe_out.write(data) if pipe_in.end_of_interval: await pipe_out.close_interval() except (asyncio.CancelledError, EmptyPipe): pass except Exception as e: print("unexpected exception: ", e) raise e finally: await pipe_out.close()