예제 #1
0
def run_cli(source, sample_rate, file_length_sec, debug, display_channel,
            threshold_db, markfreq_hz, threshold_steps, nfft, device_name):
    log_dir = source
    if not os.path.isdir(log_dir):
        print('Must provide valid log directory! source=%s' % str(log_dir))
        exit(2)

    stdscr, curses = config_curses()
    console_height, console_width = stdscr.getmaxyx()
    # setup dimensions for window
    columns_of_data = int(nfft / 2)
    min_width = columns_of_data + extra_column_buffer
    while min_width > console_width:
        nfft -= 10
        columns_of_data = int(nfft / 2)
        min_width = columns_of_data + extra_column_buffer

    # make sure window is wide enough to fit the menu
    if min_width <= menu_column_buffer:
        min_width = menu_column_buffer

    min_height = console_height
    max_rows_specgram = min_height - menu_row_buffer
    max_rows_specgram_no_menu = min_height

    # create Ui object
    ui = Ui(min_width,
            min_height,
            time.time(),
            curses.color_pair,
            max_rows_specgram,
            max_rows_specgram_no_menu,
            file_length_sec=file_length_sec,
            sample_rate=sample_rate)
    # create specgram object
    specgram = Specgram(sample_rate,
                        file_length_sec,
                        display_channel,
                        device_name=device_name,
                        scale='dB',
                        threshdb=threshold_db,
                        threshdb_steps=threshold_steps,
                        markfreq=markfreq_hz,
                        nfft=nfft,
                        max_lines=ui.specgram_max_lines,
                        color_pair=curses.color_pair,
                        voltage_bar_width=voltage_bar_width)

    # now dow stuff
    try:
        count = 0
        latest_file = ui.get_file(stdscr, source)
        previous_file = latest_file
        is_dup = True
        current_time = time.time()
        previous_time = current_time
        # setup the ui with the curses window and specgram object
        stdscr, specgram = ui.spin(stdscr, specgram)
        while True:
            current_time = time.time()
            if debug:
                # start context manager for log file
                with open(
                        'log_{0}.txt'.format(
                            unix_epoch_to_local(time.time(), no_date=True)),
                        'w+') as log_file:
                    if (current_time - previous_time) > (file_length_sec *
                                                         1000):
                        message = "iteration: {0} time: {1} last iteration happened, {2} seconds ago.\n".format(
                            count, unix_epoch_to_local(current_time),
                            (current_time - previous_time) * 0.001)
                        log_file.write(message)
                        previous_time = current_time

            latest_file = ui.get_file(stdscr, source)
            #
            # if DAQ isn't running, new files aren't being added to the log dir
            # - Let user know they are looking at the specgram of the same file over and over
            # - Let's user know when they are looking at new streaming data
            #
            if latest_file == previous_file:
                is_dup = True
            else:
                is_dup = False
            previous_file = latest_file

            # clear out data list
            specgram.clear()
            # take the file and parse into specgram object
            rc = specgram.parse_file(latest_file)
            # if rc == None:
            #     ui.message_buffer.append('Unable to read file...')
            #     # draw everything in the buffer
            #     stdscr.refresh()
            #     continue
            # clear curses window
            stdscr.erase()
            try:
                specgram.display(stdscr)
                ui.update(stdscr, specgram, is_dup, count)
                # spin ui will display menu and handle user inputs
                stdscr, specgram = ui.spin(stdscr, specgram)
            except curses.error as err:
                ui.hard_reset(stdscr, specgram, max_rows_specgram,
                              max_rows_specgram_no_menu)

            # draw everything in the buffer
            stdscr.refresh()

            count += 1
            if count % 100 == 0:
                ui.message_buffer = []
                ui.message_buffer.append('CLEARED!')

    except KeyboardInterrupt:
        print('\n\tExiting...\n\n')
        exit(1)

    except ConfigError as err:
        pass

    finally:
        curses.nocbreak()
        stdscr.keypad(False)
        curses.echo()
        curses.endwin()