class Client: HOST = environment.listener_host PORT = environment.listener_port logger = None def __init__(self): self.logger = LoggerService().get_instance() def handle(self): self.logger.info('Will send data to {}:{}'.format( self.HOST, self.PORT)) try: self.logger.info('Starting acarsdec.') acarsdec_cmd = ('acarsdec', '-A', '-j', '{}:{}'.format(self.HOST, self.PORT), '-o0', '-r', '0', *settings.acars_frequencies) self.logger.info('Executing: {}'.format(' '.join(acarsdec_cmd))) os.execlp(*acarsdec_cmd) except OSError as msg: self.logger.error('Failed to start acarsdec. Error:' + str(msg)) sys.exit() except (KeyboardInterrupt, SystemExit): self.logger.warning('Exiting gracefully.') sys.exit()
class ClientFake: HOST = environment.listener_host PORT = environment.listener_port MESSAGES = [ '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-25,"error":0,"mode":"2","label":"Q0","block_id":"0","ack":false,"tail":"VP-BOA","flight":"FV5786","msgno":"S98A"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-24,"error":0,"mode":"2","label":"Q0","block_id":"1","ack":false,"tail":"VP-BOA","flight":"FV5786","msgno":"S99A"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-25,"error":0,"mode":"H","label":"16","block_id":"2","ack":false,"tail":"VP-BOA","flight":"FV5786","msgno":"M87A","text":"143218,36000,1807, 267,N 38.027 E 24.548"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-25,"error":0,"mode":"H","label":"SA","block_id":"3","ack":false,"tail":"VP-BOA","flight":"FV5786","msgno":"S01A","text":"0EV143628V/"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-26,"error":0,"mode":"H","label":"_d","block_id":"1","ack":false,"tail":"EC-MIH","flight":"UX1302","msgno":"S21A"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-25,"error":0,"mode":"H","label":"83","block_id":"4","ack":false,"tail":"EC-MIH","flight":"UX1302","msgno":"M22A","text":"LLBG,LEMD,071445, 37.14, 23.05,40000,256,- 70.9, 20200"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-25,"error":0,"mode":"X","label":"B9","block_id":"1","ack":false,"tail":"CEPL21","flight":"XA0001","msgno":"L03A","text":"/LTAI.TI2/024LTAIA5853"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-23,"error":0,"mode":"X","label":"B9","block_id":"1","ack":false,"tail":"CEPL21","flight":"XA0001","msgno":"L03A","text":"/LTAI.TI2/024LTAIA5853"}}', '{{"timestamp":{},"station_id":"client","channel":2,"freq":131.725,"level":-19,"error":0,"mode":"H","label":"H1","block_id":"9","ack":false,"tail":"OKVEA","flight":"GS0943","msgno":"104P","text":"#T1B CLIMB 1 B\r\n48044824062.0064.8079081051053084.8080.310310204.403.630730900\r\n094.9094.9090.0090.200000000.800.8000000001100110110404950950\r\n"}}' ] logger = None def __init__(self): self.logger = LoggerService().get_instance() def handle(self): # create dgram udp socket try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.logger.info('Socket created.') except OSError as msg: self.logger.error('Failed to create socket. Error:' + str(msg)) sys.exit() while True: current_timestamp = datetime.strftime(datetime.utcnow(), '%s') msg = random.choice(self.MESSAGES).format(current_timestamp) try: sock.sendto(msg.encode(), (self.HOST, self.PORT)) self.logger.info('Data sent.') except (KeyboardInterrupt, SystemExit): self.logger.warning('Exiting gracefully.') break except OSError as msg: self.logger.error('Error sending data: ' + str(msg)) sys.exit() time.sleep(5) sock.close()
class ImageDownload: adapter = None logger = None def __init__(self): self.adapter = SqliteAdapter.get_instance() self.logger = LoggerService().get_instance() def handle(self): try: connection = pika.BlockingConnection( pika.ConnectionParameters(host=environment.queue_host)) channel = connection.channel() channel.queue_declare(queue=settings.queue_name_image_download, durable=True) channel.basic_qos(prefetch_count=1) channel.basic_consume(queue=settings.queue_name_image_download, on_message_callback=self.callback) channel.start_consuming() except (AMQPConnectionError, AMQPChannelError, ChannelClosed, ConnectionClosed, DuplicateConsumerTag, NoFreeChannels) as ex_consume: self.logger.error(str(ex_consume)) return None self.logger.info('RabbitMQ connection has been set up.') def callback(self, channel, method, properties, body): try: body_dict = json.loads(body) aircraft = Aircraft([ body_dict['id'], body_dict['registration'], body_dict['image'], body_dict['first_seen'], body_dict['last_seen'] ]) ImageService(self.adapter, self.logger).handle(aircraft) channel.basic_ack(delivery_tag=method.delivery_tag) self.logger.info('RabbitMQ job done!') except (AMQPConnectionError, AMQPChannelError, ChannelClosed, ConnectionClosed, NoFreeChannels) as ex_ack: self.logger.error(str(ex_ack)) try: channel.basic_nack(delivery_tag=method.delivery_tag) return None except (AMQPConnectionError, AMQPChannelError, ChannelClosed, ConnectionClosed, NoFreeChannels) as ex_nack: self.logger.error(str(ex_nack)) return None