import vxi11
from datetime import datetime
import platform
 
class DS1202ZE(vxi11.Instrument):
    def __init__(self, host, *args, **kwargs):
        super(DS1202ZE, self).__init__(host, *args, **kwargs)
    def get_identification(self):
        return self.ask("*IDN?")
    def get_vavg_channel1(self):
        return self.ask(":MEAS:ITEM? VAVG, CHAN1\n")
    def get_vavg_channel2(self):
        return self.ask(":MEAS:ITEM? VAVG, CHAN2\n")
    
def main():
    # Setup test result file output
    # Getting the current date and time
    dt = datetime.now()

    # Get the timestamp
    ts = datetime.timestamp(dt)
    ts_c = ts + (86400 * 24107)
    ts_i = int(ts_c)
    ts_i_s = str(ts_i)
    file_name = "NEXIO_RTC_KEEPALIVE_TEST_" + ts_i_s + ".csv"

    # Datetime object containing current date and time
    now = datetime.now()
 
    # dd/mm/YY H:M:S
    date_string = now.strftime("%d/%m/%Y")
    time_string = now.strftime("%H:%M:%S")
    time_string_store = "%H:%M:%S"

    # Get scope
    instrument = DS1202ZE('192.168.45.4')

    # Create file
    # Write Header
    sys = platform.system()

    if sys == "Windows":
        fileref = open(("C:\\Users\\david.rice\\Documents\\Python\\ARRIVE\\NEXIO\\SUPERCAP_LOGGER\\TEST_RESULTS\\" + file_name), "a")

    else:
        fileref = open(("/home/david-rice/Python/supercap_test_python/TEST_RESULTS/" + file_name), "a")        
    
    instrument.get_identification()
    fileref.write("NEXIO RTC KEEP ALIVE TEST APPLICATION - TEST REPORT\r")
    fileref.write("TEST UNIT:," + "10008\r")
    fileref.write("SCOPE ID:," + instrument.get_identification() + "\r")
    fileref.write("TEST START DATE:," + date_string + "\r")
    fileref.write("TEST START TIME:," + time_string + "\r")
    fileref.write("DATE,TIME,VSCAP(V),VDD3V3(V)\r")
    fileref.close()
    
    while (True):
        # Datetime object containing current date and time
        now = datetime.now()

        # dd/mm/YY H:M:S
        date_string = now.strftime("%d/%m/%Y")
        time_string = now.strftime("%H:%M:%S")

        if time_string == time_string_store:
            time_string_store = time_string
        
        else:
            time_string_store = time_string

            v1 = instrument.get_vavg_channel2()
            v2 = instrument.get_vavg_channel1()

            v1 = round(float(v1), 3)
            v2 = round(float(v2), 3)

            # Write Results
            result_string = date_string + "," + time_string + "," + "{:.3f}".format(v1) + "," + "{:.3f}".format(v2) + "\r"

            if sys == "Windows":
                fileref = open(("C:\\Users\\david.rice\\Documents\\Python\\ARRIVE\\NEXIO\\SUPERCAP_LOGGER\\TEST_RESULTS\\" + file_name), "a")

            else:
                fileref = open(("/home/david-rice/Python/supercap_test_python/TEST_RESULTS/" + file_name), "a")  

            fileref.write(result_string) 

            fileref.close()

            print ("TIME: " + time_string + " | VSCAP (V): " + "{:.3f}".format(v1) + " | VDD3V3 (V): " + "{:.3f}".format(v2))
 
if __name__ == "__main__":
    main()
