#include "hw.h"

HW::HW(QObject *parent) : QObject(parent)
{

    /*eventBuilder.moveToThread(&eventBuilderThread);
    eventBuilderThread.start();
    eventBuilder.init();*/

}

HW::~HW()
{
    eventBuilder.stopLogging();
    removeDevices();

}

Device& HW::operator[](int nr)
{
    return *(devices[nr]);

}

void HW::addDevices(int nr_devices)
{
    for (int i = 0; i < nr_devices; i++)
    {
        Device* new_dev = new Device;
        devices.push_back(new_dev);
        eventBuilder.addSource(&(new_dev->dataReceiver));
    }
}

void HW::removeDevices()
{
    eventBuilder.deleteSources();
    for (int i = 0; i < devices.length(); i++)
        delete devices[i];
    devices.clear();
}


void HW::connectDevices()
{
    qInfo("Connecting devices...");
    for (int i = 0; i < devices.length(); i++)
        devices[i]->connectDevice();
}

void HW::disconnectDevices()
{
    qInfo("Disconnecting devices...");
    for (int i = 0; i < devices.length(); i++)
        devices[i]->disconnectDevice();
}

void HW::run()
{
    //No need to start EVB. It's running all the time.

        //run slave(s)
    for (int i = 0; i < devices.length(); i++)
        if (devices[i]->deviceConfig.master == 0)
            devices[i]->startAcq();

        //run master(s)
    for (int i = 0; i < devices.length(); i++)
        if (devices[i]->deviceConfig.master != 0)
            devices[i]->startAcq();
}

void HW::stop()
{
        //stop master(s)
    for (int i = 0; i < devices.length(); i++)
        if (devices[i]->deviceConfig.master != 0)
            devices[i]->stopAcq();

        //stop slave(s)
    for (int i = 0; i < devices.length(); i++)
        if (devices[i]->deviceConfig.master == 0)
            devices[i]->stopAcq();

    //EVB is running all the time.
}

QString HW::report()
{
    int nr_devices = devices.length();
    QString result;

    for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++)
    {
        double buffer_ocupancy = 100.0 * (double)(devices[dev_nr]->dataReceiver.dataBuffer.nrItems()) / (double)RECEIVER_BUFFER_SIZE;
        double ksps = (double)(devices[dev_nr]->getFrameRate())/1000.0;
        result += QString("%1: %2kfps, %3% ").arg(dev_nr).arg(ksps,5,'f',2,'0').arg(buffer_ocupancy,4,'f',1,'0');
    }

    return result;
}
