async def publish_data(data_queue: deque, data_lock: asyncio.Lock, publisher: Publisher): """Publishes the data to the wamp topic :param data_queue: queue containing the several item of HouseData :param data_lock: lock used to synchronize the usage of the queue :param publisher: Publisher used to publish data on topic """ log.info('Publishing data') data_per_house = [] await data_lock.acquire() try: log.info(f"Received {len(data_queue)} data in the last hour") while data_queue: data = data_queue.pop() data_per_house.append(data) finally: data_lock.release() for h in data_per_house: # TODO do some transformation like data avg etc log.debug(f"publishing to topic : {h}") publisher.publish('conti.tutorial.hello', {'price': h}) log.info(f"Published {len(data_per_house)} events")
def publish(self, topic, payload): """Publish the payload on the specified topic :param topic: target topic :param payload: payload to be published """ self.session.publish(topic, payload) log.debug(f"data is published to : {topic}")
def seconds_until_next_hour(): """Counts the number of seconds until the next "round" hour. e.g. if it is 14:25:35, there are 2065s until 15:00:00 :return number of seconds until next hour """ now = datetime.datetime.now() now_minute = now.minute now_second = now.second seconds_left = (60 - now_minute) * 60 - now_second log.debug(f"{seconds_left} seconds left until next round hour") return seconds_left
async def connect(self, crossbar_url): """Method used to initialize a session for the publisher :param crossbar_url: url hosting the crossbar instance """ log.debug(f"Connecting to {crossbar_url}") self.component = Component(transports=crossbar_url, realm='realm1') loop = asyncio.get_event_loop() txaio.config.loop = loop session_ready = asyncio.Event() async def setup_session(created_session, _details): """Callback method used to retrieve a session and notify interested parties with an event""" self.session = created_session nonlocal session_ready session_ready.set() self.component.start(loop=loop) self.component.on_join(setup_session) await session_ready.wait()
async def receive_house_data(host, key, data_queue: deque, data_lock: asyncio.Lock): """ Receive data from S3 data source for house price and put it in a queue :param host: url of the data source to connect to :param key: token that will be used to authenticate against S3 :param data_queue: queue that will contain the data from house data source :param data_lock: lock used to synchronize the queue's usage """ log.info(f"Opening data from {host}") #url = f"https://{host}/id={key}" url = f"http://{host}" log.debug(f" url : {url}") async with aiohttp.ClientSession(headers=_headers) as session: async with session.get(url, timeout=None) as resp: if resp.status != 200: log.error( f"Connection to house data not successful: {resp.status} {resp.reason}" ) raise RuntimeError( f"Connection to house data not successful: {resp.status} {resp.reason}" ) async for line in resp.content: line = line.decode('utf8') log.debug(f"Received raw data") cr = csv.reader(line.splitlines(), delimiter=',') my_list = list(cr) for row in my_list[:5]: await data_lock.acquire() try: data_queue.append(row) finally: data_lock.release() log.debug(f"row = {row}")