#include "Header_Files/atpFiberImager.h"

ATPFiberImager::ATPFiberImager(bool bIsUSBMode, std::string ucPortNumber, std::string strDeviceName)
{
    m_FiberSpectrometer = NULL;

    mUcPortNumber=ucPortNumber;
}

ATPFiberImager::~ATPFiberImager()
{

}

void ATPFiberImager::connectFiberSpectrometer(QString& SN, QString& pixelCount, QString& wavelengthInfo)
{
    using namespace std;

    m_FiberSpectrometer = new ZZ_ATPControl_Serial_Qt();

    m_FiberSpectrometer->Initialize(false, mUcPortNumber, "OPTOSKY");

    DeviceInfo deviceInfo;//
    DeviceAttribute deviceAttribute;

    m_FiberSpectrometer->GetDeviceInfo(deviceInfo);
    m_FiberSpectrometer->GetDeviceAttribute(deviceAttribute);//？？？？？

    SN = QString::fromStdString(deviceInfo.strSN);
    pixelCount = QString::number(deviceAttribute.iPixels);
    wavelengthInfo = QString::number(deviceAttribute.fWaveLengthInNM[0]) + "--" + QString::number(deviceAttribute.fWaveLengthInNM[deviceAttribute.iPixels - 1]);

    m_FiberSpectrometer->SetDeviceTemperature(-10);


    //设置dn值的最大值（和位深相关）
    string qepro = "QEP";//?????????????????????????????????????????????????????????????????????????????????????????
    string flame = "FLMS";//?????????????????????????????????????????????????????????????????????????????????????????
    if (deviceInfo.strSN.find(qepro) != string::npos)
    {
        m_MaxValueOfFiberSpectrometer = 200000;
    }
    else if (deviceInfo.strSN.find(flame) != string::npos)
    {
        m_MaxValueOfFiberSpectrometer = 65535;
    }
    else//没有找到匹配的仪器来设置   dn值的最大值
    {

    }

}

void ATPFiberImager::disconnectFiberSpectrometer()
{
    m_FiberSpectrometer->Close();
}

void ATPFiberImager::getDeviceAttribute(DeviceAttribute& deviceAttribute)
{
    m_FiberSpectrometer->GetDeviceAttribute(deviceAttribute);
}

void ATPFiberImager::getDeviceInfo(DeviceInfo& deviceInfo)
{
    m_FiberSpectrometer->GetDeviceInfo(deviceInfo);
}

void ATPFiberImager::setExposureTime(int iExposureTimeInMS)
{
    m_FiberSpectrometer->SetExposureTime(iExposureTimeInMS);
}

void ATPFiberImager::getExposureTime(int &iExposureTimeInMS)
{
    m_FiberSpectrometer->GetExposureTime(iExposureTimeInMS);
}

void ATPFiberImager::getDeviceTemperature(float &fTemperature)
{
    m_FiberSpectrometer->GetDeviceTemperature(fTemperature);
}

void ATPFiberImager::singleShot(DataFrame &dfData)
{
    m_FiberSpectrometer->SingleShot(dfData);
}

void ATPFiberImager::getNonlinearityCoeffs(coeffsFrame &coeffs)
{
    printf("This is ATPFiberImager.\n");
}

void ATPFiberImager::recordDark(QString path)
{
    //获取设备信息
    DeviceAttribute attribute;
    DeviceInfo deviceInfo;
    getDeviceAttribute(attribute);
    getDeviceInfo(deviceInfo);

    //采集暗帧
    singleShot(m_DarkData);

    //输出到csv
    QDateTime curDateTime = QDateTime::currentDateTime();
    QString currentTime = curDateTime.toString("yyyy_MM_dd_hh_mm_ss");
    QString fileName = path + "/" + currentTime + "_" + QString::fromStdString(deviceInfo.strSN) + "_darkSpectral_dn.csv";
    std::ofstream outfile(fileName.toStdString().c_str());

    for (int i = 0; i < attribute.iPixels; i++)
    {
        if (i==0)
        {
            outfile << m_DarkData.usExposureTimeInMS << std::endl;
        }
        outfile << attribute.fWaveLengthInNM[i] << "," << m_DarkData.lData[i] << std::endl;
    }

    outfile.close();
}

void ATPFiberImager::recordTarget(int recordTimes, QString path)
{
    //获取设备信息
    DeviceAttribute attribute;
    DeviceInfo deviceInfo;
    getDeviceAttribute(attribute);
    getDeviceInfo(deviceInfo);


    DataFrame integratingSphereData_tmp;

    for (int i = 0; i < recordTimes; i++)
    {
        singleShot(integratingSphereData_tmp);

        if (i == 0)//将integratingSphereData_tmp中的曝光时间、温度等信息传给m_IntegratingSphereData
        {
            m_IntegratingSphereData = integratingSphereData_tmp;
        }
        else
        {
            for (int i = 0; i < attribute.iPixels; i++)
            {
                m_IntegratingSphereData.lData[i] += integratingSphereData_tmp.lData[i];
            }
        }

    }

    for (int i = 0; i < attribute.iPixels; i++)
    {
        m_IntegratingSphereData.lData[i] = m_IntegratingSphereData.lData[i] / recordTimes;
    }


    //输出到csv
    QDateTime curDateTime = QDateTime::currentDateTime();
    QString currentTime = curDateTime.toString("yyyy_MM_dd_hh_mm_ss");
    QString fileName = path + "/" + currentTime + "_" + QString::fromStdString(deviceInfo.strSN) + "_integratingSphereSpectral_dn.csv";
    std::ofstream outfile(fileName.toStdString().c_str());

    for (int i = 0; i < attribute.iPixels; i++)
    {
        if (i==0)
        {
            outfile << m_IntegratingSphereData.usExposureTimeInMS << std::endl;
        }
        outfile << attribute.fWaveLengthInNM[i] << "," << m_IntegratingSphereData.lData[i] << std::endl;
    }

    outfile.close();
}

void ATPFiberImager::autoExpose()
{
    float fPredictedExposureTime;
    m_FiberSpectrometer->PerformAutoExposure(0.6,0.9,fPredictedExposureTime);
}

ZZ_S32 ATPFiberImager::GetMaxValue(ZZ_S32 * dark, int number)
{
    ZZ_S32 max = 0;
    for (size_t i = 0; i < number; i++)
    {
        if (dark[i] > max)
        {
            max = dark[i];
        }
    }
    //std::cout << "本帧最大值为" << max << std::endl;
    return max;
}