import time, os

import numpy as np

from config.config import get_config
from .compact_daq import CompactDAQ


def process_data(data: np.ndarray, t):
    v = np.mean(data[0, :])
    i = np.mean(data[1, :])
    str_pot = np.mean(data[2, :])
    encoder = np.mean(data[3,:])
    print("delta t={:5.2f}s   V={:5.2f} I={:5.2f} X={:5.2f} encoder={:5.2f}".format(t, v, i, str_pot, encoder))

cfg = get_config(validate_present=['data_folder'])
if cfg:
    folder = cfg['data_folder']
else:
    print('Config is missing the location of the data folder. Exiting.')
    exit()
    
with CompactDAQ(folder) as daq:
    daq.start_task()
    tic = time.time()

    for n in range(5):
        time.sleep(0.25)
        data = daq.get_data()
        toc = time.time()
        process_data(data,toc-tic)
        tic = toc
    
    daq.start_logging()
    for n in range(5):
        time.sleep(0.25)
        data = daq.get_data()
        toc = time.time()
        process_data(data,toc-tic)
        tic = toc
    daq.stop_logging()

    for n in range(5):
        time.sleep(0.25)
        data = daq.get_data()
        toc = time.time()
        process_data(data,toc-tic)
        tic = toc

    daq.start_logging()
    for n in range(5):
        time.sleep(0.25)
        data = daq.get_data()
        toc = time.time()
        process_data(data,toc-tic)
        tic = toc
    daq.stop_logging()

