def __init__(self, settings): print 'Test mode' queue = Queue.Queue() self._receive = Receive(settings, queue) self._signal = signal.signal(signal.SIGINT, self.__close) events.Post(queue).scan_start() while self._receive.isAlive(): if not queue.empty(): self.__process_queue(settings, queue) self.__close()
class TestMode(object): def __init__(self, settings): print 'Test mode' queue = Queue.Queue() self._receive = Receive(settings, queue) self._signal = signal.signal(signal.SIGINT, self.__close) events.Post(queue).scan_start() while self._receive.isAlive(): if not queue.empty(): self.__process_queue(settings, queue) self.__close() def __process_queue(self, settings, queue): event = queue.get() eventType = event.get_type() if eventType == events.SCAN_START: print 'Scanning...' self._receive.receive() if eventType == events.STATUS_PROCESS: print 'Processing...' elif eventType == events.SCAN_DONE: collars = event.get_arg('collars') if collars is not None: print 'Signals:' if len(collars): for collar in collars: summary = '\t{:8.4f}MHz {:2} {:4.1f}PPM, {:.4f}dB' level = 10 * log10(collar.level) print summary.format(collar.freq / 1e6, MOD_DESC[collar.mod], collar.rate, level) else: print '\tNo signals found' events.Post(queue).scan_start() elif eventType == events.WARN: warning = 'Warning: {}'.format(event.get_arg('warning')) print warning elif eventType == events.ERR: error = event.get_arg('error') sys.stderr.write(error) self.__close() exit(3) try: time.sleep(0.1) except IOError: pass def __close(self, _signal=None, _frame=None): signal.signal(signal.SIGINT, self._signal) print '\nExiting\n' if self._receive is not None: self._receive.stop()
def __init__(self): settings = Settings(self.__arguments()) self._settings = settings print 'Harrier\n' try: print 'Host :\t\t{} ({})'.format( socket.gethostname(), socket.gethostbyname(socket.getfqdn())) except socket.gaierror: pass queue = Queue.Queue() if settings.test: TestMode(settings) return print 'Survey:\t\t{}'.format(settings.survey) self._gps = None self._database = Database(settings.db, queue) self._receive = Receive(settings, queue) self._status = Status(self._database) self._server = Server(queue, self._status, self._database, settings) self._isScanning = False self._cancel = False self._signal = signal.signal(signal.SIGINT, self.__close) halfBand = SAMPLE_RATE / 2e6 print 'Scan range:\t{:.2f}-{:.2f}MHz'.format(settings.freq - halfBand, settings.freq + halfBand) if settings.delay is None: mode = 'Remote' else: mode = 'Automatic, after {}s'.format(settings.delay) print 'Scan mode:\t{}'.format(mode) events.Post(queue).gps_open(0) if settings.delay is not None: events.Post(queue).scan_start() while not self._cancel: if not queue.empty(): self.__process_queue(settings, queue) else: try: time.sleep(0.1) except IOError: pass print '\nExiting...' waiting = self._status.get_wait() if waiting is not None: print '(Waiting for {} to finish)'.format(self._status.get_wait()) self._cancel = True if self._server is not None: self._server.stop() if self._gps is not None: self._gps.stop() if self._receive is not None: self._receive.stop() if self._database is not None: self._database.stop()
class Harrier(object): def __init__(self): settings = Settings(self.__arguments()) self._settings = settings print 'Harrier\n' try: print 'Host :\t\t{} ({})'.format( socket.gethostname(), socket.gethostbyname(socket.getfqdn())) except socket.gaierror: pass queue = Queue.Queue() if settings.test: TestMode(settings) return print 'Survey:\t\t{}'.format(settings.survey) self._gps = None self._database = Database(settings.db, queue) self._receive = Receive(settings, queue) self._status = Status(self._database) self._server = Server(queue, self._status, self._database, settings) self._isScanning = False self._cancel = False self._signal = signal.signal(signal.SIGINT, self.__close) halfBand = SAMPLE_RATE / 2e6 print 'Scan range:\t{:.2f}-{:.2f}MHz'.format(settings.freq - halfBand, settings.freq + halfBand) if settings.delay is None: mode = 'Remote' else: mode = 'Automatic, after {}s'.format(settings.delay) print 'Scan mode:\t{}'.format(mode) events.Post(queue).gps_open(0) if settings.delay is not None: events.Post(queue).scan_start() while not self._cancel: if not queue.empty(): self.__process_queue(settings, queue) else: try: time.sleep(0.1) except IOError: pass print '\nExiting...' waiting = self._status.get_wait() if waiting is not None: print '(Waiting for {} to finish)'.format(self._status.get_wait()) self._cancel = True if self._server is not None: self._server.stop() if self._gps is not None: self._gps.stop() if self._receive is not None: self._receive.stop() if self._database is not None: self._database.stop() def __arguments(self): parser = argparse.ArgumentParser(description='Harrier', formatter_class=ArgparseFormatter) dirUser = os.path.expanduser('~') parser.add_argument('-f', '--frequency', help='Centre frequency (MHz)', type=float, required=True) parser.add_argument('-g', '--gain', help='Gain (dB)', type=float) parser.add_argument('-c', '--conf', help='Configuration file', default=os.path.join(dirUser, 'harrier.conf')) groupNomal = parser.add_argument_group('Scan mode') groupNomal.add_argument('-s', '--survey', help='Survey name', type=str, default='Survey ' + time.strftime('%c')) groupNomal.add_argument('file', help='File', nargs='?', default=os.path.join(dirUser, 'harrier.wfh')) groupTest = parser.add_argument_group('Test mode') groupTest.add_argument('-t', '--test', help='Test mode', action='store_true') args = parser.parse_args() if not os.path.exists(args.conf): error = 'Configuration file {} not found\n'.format(args.conf) sys.stderr.write(error) parser.exit(1) return args def __process_queue(self, settings, queue): if self._cancel: return event = queue.get() eventType = event.get_type() # Start scan if eventType == events.SCAN_START: location = self._status.get_location() if location is None or time.time() - location[1] > GPS_AGE: self._status.set_status(events.STATUS_WAIT) events.Post(queue).scan_start(1) elif not self._isScanning: self._receive.receive() self._server.send_status() # Scan finished elif eventType == events.SCAN_DONE: self._isScanning = False timeStamp = event.get_arg('time') collars = event.get_arg('collars') if collars is not None: self._status.set_signals(len(collars)) for collar in collars: location = self._status.get_location()[0] collar.lon = location[0] collar.lat = location[1] self._database.append_signal(timeStamp, collar, settings.freq, self._settings.survey) else: self._status.set_signals(0) self._server.send_signals(timeStamp, collars) log = 'Found {} signals'.format(len(collars)) logTime = self._database.append_log(log) self._server.send_log(logTime, log) if settings.delay is not None: events.Post(queue).scan_start(settings.delay) self._server.send_status() # Open GPS elif eventType == events.GPS_OPEN: if self._gps is not None: self._gps.stop() self._gps.join() if self._gps is None: print '\nStarting GPS' self._gps = Gps(settings.gps, queue) # GPS location elif eventType == events.GPS_LOC: self._status.set_location(event.get_arg('location')) self._server.send_status() # GPS satellites elif eventType == events.GPS_SATS: self._status.set_sats(event.get_arg('satellites')) self._server.send_sats() # GPS error elif eventType == events.GPS_ERR: self._gps = None error = 'GPS error: {}'.format(event.get_arg('error')) print '\n' + error print 'Retry in {}s'.format(GPS_RETRY) logTime = self._database.append_log(error) self._server.send_log(logTime, error) self._status.clear_gps() self._server.send_status() events.Post(queue).gps_open(GPS_RETRY) # Info elif eventType == events.INFO: self._status.clear_gps() info = 'Info: {}'.format(event.get_arg('info')) print '\n' + info logTime = self._database.append_log(info) self._server.send_log(logTime, info) # Warning elif eventType == events.WARN: self._status.clear_gps() warning = 'Warning: {}'.format(event.get_arg('warning')) print '\n' + warning logTime = self._database.append_log(warning) self._server.send_log(logTime, warning) # Error elif eventType == events.ERR: error = event.get_arg('error') sys.stderr.write(error) logTime = self._database.append_log(error) self._server.send_log(logTime, error) self.__close() else: self._status.set_status(eventType) self._server.send_status() def __close(self, _signal=None, _frame=None): signal.signal(signal.SIGINT, self._signal) self._cancel = True
class TestMode(object): def __init__(self, settings): print 'Test mode' queue = Queue.Queue() self._receive = Receive(settings, queue) self._signal = signal.signal(signal.SIGINT, self.__close) events.Post(queue).scan_start() while self._receive.isAlive(): if not queue.empty(): self.__process_queue(settings, queue) self.__close() def __process_queue(self, _settings, queue): event = queue.get() eventType = event.get_type() if eventType == events.SCAN_START: print 'Scanning...' self._receive.receive() if eventType == events.STATUS_PROCESS: print 'Processing...' elif eventType == events.SCAN_DONE: collars = event.get_arg('collars') if collars is not None: print 'Signals:' if len(collars): for collar in collars: summary = '\t{:8.4f}MHz {:2} {:4.1f}PPM, {:.4f}dB' level = 10 * log10(collar.level) print summary.format(collar.freq / 1e6, MOD_DESC[collar.mod], collar.rate, level) else: print '\tNo signals found' events.Post(queue).scan_start() elif eventType == events.WARN: warning = 'Warning: {}'.format(event.get_arg('warning')) print warning elif eventType == events.ERR: error = event.get_arg('error') sys.stderr.write(error) self.__close() exit(3) try: time.sleep(0.1) except IOError: pass def __close(self, _signal=None, _frame=None): signal.signal(signal.SIGINT, self._signal) print '\nExiting\n' if self._receive is not None: self._receive.stop()