#include "SensorOptoSky.h"
#include "OptoSkyType.h"
#include "logout.h"
#include "QtEndian"
using namespace OptoSky;
SensorOptoSky::SensorOptoSky(OPTOSENSORTYPE type, QString comname, qint32 bandrate)

{
	sensortype = type;
		SensorType = "opt";
		SensorInfo.SensorName = "opotsky";
		COMname = comname;
		BandRate = bandrate;
		shutter.resize(2);

		//SendComment(comi);
}

SensorOptoSky::SensorOptoSky(OPTOSENSORTYPE type, QString comname)
{
	
	sensortype = type;
	SensorType = "opt";
	SensorInfo.SensorName = "opotsky";
	COMname = comname;
	BandRate = 115200;
		shutter.resize(2);
}

bool SensorOptoSky::initSensor(int id /*= 0*/)
{


	OptoSky::SetPortName(COMname, BandRate);
	if (!OptoSky::isSensorInit())
	{
		return false;
	}

	SensorInfo = GetSensorInfo();

	float a = GettingTempratrue();
	//SendComment()
	return true;
}

void SensorOptoSky::Settingshuttertime(float msc)
{
	if (!OptoSky::isSensorInit())
	{
		return;
	}
	shuttertime = (int)msc;
	shutter[0] = (int)msc / 256;
	shutter[1] = (int)msc % 256;
	QByteArray temp;
	SendComment(SET_INTEGRAL_TIME,shutter);
	GetReturn(SET_INTEGRAL_TIME,temp);
}

void SensorOptoSky::SettingTemprature(float temp)
{
	QByteArray buf;
	buf.resize(2);

	if (temp<0)
	{
		buf[0] = 0xff;
	}
	else
	{
		buf[1] = 0x00;

	}
	buf[1] = (int)abs(temp);
	SendComment(SET_TEC_TEMP, buf);
	GetReturn(SET_TEC_TEMP, buf);

}

float SensorOptoSky::GettingTempratrue()
{
    return 0.0;
	if (!OptoSky::isSensorInit())
	{
		return 0;
	}
	QByteArray buf;
	SendComment(GET_TEC_TEMP);
	GetReturn(GET_TEC_TEMP, buf);
	QString str;
	str.append(buf);
	float ret = str.toFloat();
	return ret;
}

STRSensorInfo SensorOptoSky::GetSensorInfo()
{
	STRSensorInfo setem;
	if (!OptoSky::isSensorInit())
	{
		return setem;
	}
	STRSensorInfo se;
	QByteArray buf;
	SendComment(GET_PN_NUMBER);
	GetReturn(GET_PN_NUMBER, buf);
	QString str;
	str.append(buf);
	se.SensorName = str;
	logout::out("设备型号为：" + str);
	SendComment(GET_SN_NUMBER);
	GetReturn(GET_SN_NUMBER, buf);
	str.clear();
	str.append(buf);
	logout::out("序列号为：" + str);
	SendComment(GET_PIXEL_LENGTH);
	GetReturn(GET_PIXEL_LENGTH, buf);
	se.BandNum = buf[0] * 256 + buf[1];
	//////////////////////////模块分割////////////////////////////////////////////////
	Settingshuttertime(6);
	SendComment(SYNCHRONIZATION_GET_DATA);
	GetReturn(SYNCHRONIZATION_GET_DATA, buf);
	se.BandNum = (buf.size() - 1)/2;
	//////////////////////////模块分割////////////////////////////////////////////////
	getwavelenthlist(se);
	//波长获取仍无法获取 最大值仍未获取
	//////////////////////////模块分割////////////////////////////////////////////////
	QByteArray bbb;
	bbb.resize(2);
	bbb[0] = 0x00;
	bbb[1] = 0x01;
	SendComment(SET_AVERAGE_NUMBER, bbb);
	GetReturn(SET_AVERAGE_NUMBER, bbb);
	se.maxValue = 65535;
    bbb[0] = 0x00;
    bbb[1] = 0x01;
    SendComment(SET_AVERAGE_NUMBER,bbb);
	return se;
}

void SensorOptoSky::SettingShutterOpen(bool isopen)
{

}

void SensorOptoSky::SettingSwitchShutter(bool isright)
{

}

void SensorOptoSky::GetOnePlot(STROnePlot &OnePlot, bool Dark)
{
	OnePlot.temp = GettingTempratrue();

	SendComment(SYNCHRONIZATION_GET_DATA,shutter);
	OnePlot.shutter = shuttertime;
	QByteArray buf;
	if (!GetReturn(SYNCHRONIZATION_GET_DATA, buf))
	{
		OnePlot.isInit = false;
		return;
	}
	

	if ((unsigned char )buf[0]==0x00)
	{
		//OnePlot.isInit = true;
	}
	else
	{
		OnePlot.isInit = false;
	}
	int bandsumnwo = SensorInfo.BandNum;
	unsigned short *arr = new unsigned short[bandsumnwo];
	memcpy(arr, buf.data() + 1, bandsumnwo*2);
	if (!OnePlot.isInit)
	{
		OnePlot.Plot = new float[bandsumnwo];
		OnePlot.isInit = true;
	}
	
	for (size_t i = 0; i < bandsumnwo; i++)
	{
		OnePlot.Plot[i] = qToBigEndian(arr[i]);
	}
	delete[] arr;
	OnePlot.BandNum = bandsumnwo;
	OnePlot.maxDNvalue = SensorInfo.maxValue;
	OnePlot.wavelenthlist = SensorInfo.wavelenthlist;
	if (Dark)
	{
		if (DarkNess.isInit)
		{
			if (DarkNess.shutter != shuttertime)
			{
				GetOneDark(DarkNess);
			}

		}
		else
		{
			GetOneDark(DarkNess);
		}

		for (size_t i = 0; i < bandsumnwo; i++)
		{
			OnePlot.Plot[i] = OnePlot.Plot[i] - DarkNess.Plot[i];
		}
	}

}

void SensorOptoSky::CloseSensor()
{
	closeport();
}

void SensorOptoSky::GetOneDark(STROnePlot &darkplot)
{
	STROnePlot &OnePlot = darkplot;
	OnePlot.temp = GettingTempratrue();
	OnePlot.shutter = shuttertime;
	SendComment(SYNCHRONIZATION_GET_DARK, shutter);
	QByteArray buf;
	if (!GetReturn(SYNCHRONIZATION_GET_DARK, buf))
	{
		OnePlot.isInit = false;
		return;
	}


	if ((unsigned char)buf[0] == 0x00)
	{
		//OnePlot.isInit = true;
	}
	else
	{
		OnePlot.isInit = false;
	}
	int bandsumnwo = SensorInfo.BandNum;
	unsigned short *arr = new unsigned short[bandsumnwo];
	memcpy(arr, buf.data() + 1, bandsumnwo * 2);
	if (!OnePlot.isInit)
	{
		OnePlot.Plot = new float[bandsumnwo];
		OnePlot.isInit = true;
	}
	for (size_t i = 0; i < bandsumnwo; i++)
	{
		OnePlot.Plot[i] = qToBigEndian(arr[i]);


	}
	delete[] arr;
	OnePlot.BandNum = bandsumnwo;
	OnePlot.maxDNvalue = SensorInfo.maxValue;
	OnePlot.wavelenthlist = SensorInfo.wavelenthlist;
	OnePlot.DataType = DARKNESS;
	
}

SensorOptoSky::~SensorOptoSky()
{
	CloseSensor();
}

void SensorOptoSky::RemoveHeaderandEnd(QByteArray &buf)
{
	if (buf.size()<6)
	{
		return;
	}
	buf.remove(0, 5);
	buf.remove(buf.size() - 1,1);

}

void SensorOptoSky::getwavelenthlist(STRSensorInfo &se)
{
	if (!OptoSky::isSensorInit())
	{
		return ;
	}
	QByteArray bufin;
	bufin.resize(2);
	bufin[0] = 0x00;
	bufin[1] = 0x01;	
	QByteArray buf;
	SendComment(GET_WAVELENGTH_CALIBRATION_COFF,bufin);
	GetReturn(GET_WAVELENGTH_CALIBRATION_COFF, buf);
	float a[4];
	memcpy(a, buf.data() + 16, 4 * 4);
	int bandsss = se.BandNum;
	QStringList strlist;
	QString str="";
	int i = 0;
	for ( i = 0; i <bandsss-1 ; i++)
	{
		float wavelenth = a[0] * i*i*i + a[1] * i*i + a[2] * i + a[3];
		strlist.append(QString::number(wavelenth, 'f', 2) );
		str = str + QString::number(wavelenth, 'f', 2) + ",";
		
	}

	float wavelenth = a[0] * i*i*i + a[1] * i*i + a[2] * i + a[3];
	str = str + QString::number(wavelenth, 'f', 2);
	strlist.append(QString::number(wavelenth, 'f', 2));
	se.wavelenthlist = strlist;
	se.WavelenthStr = str;
}
