#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# SPDX-License-Identifier: GPL-3.0
# Author: <NAME>
# GNU Radio Python Flow Graph
# Title: Not titled yet
# GNU Radio version: 3.8.3.1

from gnuradio import blocks
from gnuradio import filter
from gnuradio.filter import firdes
from gnuradio import gr
import sys
import signal
from argparse import ArgumentParser
from gnuradio.eng_arg import eng_float, intx
from gnuradio import eng_notation
from gnuradio import uhd
import time
import os
import datetime


class DataCollection(gr.top_block):

    def __init__(self):
        gr.top_block.__init__(self, "Not titled yet")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 2e6  #sampling rate for the B200
        self.Bandwidth = Bandwidth = 1e6 #bandwidth used for the B200 (single sided)

        ##################################################
        # Blocks
        ##################################################
        self.uhd_usrp_source_0 = uhd.usrp_source(
            ",".join(("", "")),
            uhd.stream_args(
                cpu_format="fc32",
                args='',
                channels=list(range(0,1)),
            ),
        )
        self.uhd_usrp_source_0.set_center_freq(2476e6, 0) #set the center frequency for the B200, 2.476GHz for nRF2.4
        self.uhd_usrp_source_0.set_rx_agc(False, 0)
        self.uhd_usrp_source_0.set_gain(75, 0) #set receiving gain to upper limit
        self.uhd_usrp_source_0.set_antenna('TX/RX', 0)
        self.uhd_usrp_source_0.set_samp_rate(samp_rate)
        # No synchronization enforced.
        self.fft_filter_xxx_0 = filter.fft_filter_ccc(1, firdes.low_pass(1, samp_rate, Bandwidth, 10e3, firdes.WIN_HAMMING, 6.76), 6)
        self.fft_filter_xxx_0.declare_sample_delay(0)
        self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_gr_complex*1, 'data/data.bin', False)
        self.blocks_file_sink_0.set_unbuffered(False)


        ##################################################
        # Connections
        ##################################################
        self.connect((self.fft_filter_xxx_0, 0), (self.blocks_file_sink_0, 0))
        self.connect((self.uhd_usrp_source_0, 0), (self.fft_filter_xxx_0, 0))


    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.fft_filter_xxx_0.set_taps(firdes.low_pass(1, self.samp_rate, self.Bandwidth, 10e3, firdes.WIN_HAMMING, 6.76))
        self.uhd_usrp_source_0.set_samp_rate(self.samp_rate)

    def get_Bandwidth(self):
        return self.Bandwidth

    def set_Bandwidth(self, Bandwidth):
        self.Bandwidth = Bandwidth
        self.fft_filter_xxx_0.set_taps(firdes.low_pass(1, self.samp_rate, self.Bandwidth, 10e3, firdes.WIN_HAMMING, 6.76))





def main(top_block_cls=DataCollection, options=None):
    #begin constructing filepath for data file
    folderPath = 'data/'
    dataFile = folderPath + 'data.bin'
    
    tb = top_block_cls()
    
    def sig_handler(sig=None, frame=None):
        tb.stop()
        tb.wait()

        sys.exit(0)

    signal.signal(signal.SIGINT, sig_handler)
    signal.signal(signal.SIGTERM, sig_handler)

    tb.start()

    try:
        input('Press Enter to quit: ')
    except EOFError:
        pass
    tb.stop()
    tb.wait()
    
    #note that the above section is automatically generated by the GNU Radio Companion
    saveFile()  #call function to save the data file with appropriate deviceID and timestamp 

def saveFile():
    timeStamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    exitInvalid = input("Enter invalid status (1 for invalid or 0 for valid) >>>  ")
    if exitInvalid == '0':
        deviceID = input("Enter the device ID: ")
        changedFileName = 'data/' + deviceID + "_" + timeStamp + '.bin'
        os.rename('data/data.bin', changedFileName)

if __name__ == '__main__':
    main()
