import time, os

import numpy as np

from config.config import get_config
from .compact_daq import CompactDAQ


def process_data(data1: np.ndarray, data2: np.ndarray, t):
    d1 = data1.mean(axis=1)
    d2 = data2.mean(axis=1)
    x_motor1 = 0.01309 * d1[3]
    x_motor2 = 0.01309 * d2[3]
    print(f'{t} {x_motor1} {x_motor2}')

cfg = get_config(validate_present=['data_folder'])
if cfg:
    folder = cfg['data_folder']
else:
    print('Problem with data folder. Exiting.')
    exit()

with CompactDAQ(folder) as daq:
    daq.start_task()
    tic = time.time()

    time.sleep(3)
    while True:
        tic, data1 = daq.get_index_and_data()
        time.sleep(0.5)
        toc, data2 = daq.get_index_and_data()
        process_data(data1, data2, toc-tic)
