#include "SensorQExPRO.h"
#include "QDebug"
#include "api/seabreezeapi/SeaBreezeAPI.h"
#include "logout.h"
#include "QMetaEnum"
#include "QThread"
SensorQExPRO::SensorQExPRO(EMQEType qetype)

{
	QEType = qetype;
	if (qetype==QEPRO_QE)
	{

	}
	else if(qetype == USB2000PLUS_QE)
	{

	}
	else
	{
		OutputMod = nullptr;
		Shuteroffcom = nullptr;
		Shuteroncom = nullptr;
	}

}

SensorQExPRO::~SensorQExPRO()
{
	if (SensorInfo.isSensorInit)
	{
		sbapi_shutdown();
	}

	
}

bool SensorQExPRO::initSensor(int id)
{
	sbapi_shutdown();
	sbapi_initialize();
	sbapi_probe_devices();
	int number_of_QE = sbapi_get_number_of_device_ids();
	logout::out("找到 " + QString::number(number_of_QE) + "个设备",2);
	if (number_of_QE <= id)
	{
		return false;
	}
	Device_ids=new long[number_of_QE]();
	 sbapi_get_device_ids(Device_ids, number_of_QE);
	logout::out("设备id为：" + QString::number(Device_ids[id]),2);
	DeviceId = Device_ids[id];

	char namearr[100];
	//获取名称
	FLAG= sbapi_get_device_type(DeviceId, &ERR, namearr, 99);
	if (FLAG!=0)
	{
		QString sensorname = QString(namearr);
		QEType = getEMUbyQstring(sensorname);
		SensorType = sensorname;
		SensorInfo.SensorName = sensorname;
		logout::out("设备型号：" + sensorname,2);
	}
	Sensorid = DeviceId;
	//打开设备
	FLAG= sbapi_open_device(DeviceId, &ERR);
	logout::out("设备打开：" + QString(sbapi_get_error_string(ERR)),2);
	if (FLAG!=0)
	{
		//如果失败 返回false
		return false;
	}
	SensorInfo.isSensorInit = true;
	int maxf=sbapi_get_number_of_shutter_features(DeviceId, &ERR);;
	long fff;
// 	sbapi_get_shutter_features(DeviceId, &ERR, &fff,maxf);
// 	sbapi_shutter_set_shutter_open(DeviceId, fff,&ERR, 0);
// 	logout::out("设备打开：" + QString(sbapi_get_error_string(ERR)));
// 	qDebug() << "hello";
	SensorInfo=GetSensorInfo();


	//为温度二设置的
	Numberoftempratrue= sbapi_get_number_of_thermo_electric_features(DeviceId, &ERR);
	TemPratrueId=new long[Numberoftempratrue]();
	Numberoftempratrue=sbapi_get_thermo_electric_features(DeviceId, &ERR, TemPratrueId, Numberoftempratrue);
	sbapi_tec_set_enable(DeviceId, TemPratrueId[0], &ERR, 1);
	return true;
}

void SensorQExPRO::Settingshuttertime(float msc)
{
	if (!SensorInfo.isSensorInit)
	{
		return;
	}
	shuttertime = msc;
	sbapi_spectrometer_set_integration_time_micros(DeviceId, SpecMetersId[0], &ERR, msc*1000);
	//logout::out("设置曝光时间：" + QString(sbapi_get_error_string(ERR)),2);
}

void SensorQExPRO::SettingTemprature(float temp)
{
	sbapi_tec_set_temperature_setpoint_degrees_C(DeviceId, TemPratrueId[0], &ERR,temp);
	QThread::msleep(100);
}

STRSensorInfo SensorQExPRO::GetSensorInfo()
{
	STRSensorInfo sensorinof;
	//return sensorinof;
	if (!SensorInfo.isSensorInit)
	{
		return sensorinof;
	}
	int ERR=0;
	
	sensorinof.isSensorInit = true;
	sensorinof.BandNum= sbapi_get_number_of_spectrometer_features(DeviceId, &ERR);
	SpecMetersId = new long[sensorinof.BandNum]();
	int numm= sbapi_get_spectrometer_features(DeviceId, &ERR, SpecMetersId, sensorinof.BandNum);
	sensorinof.BandNum = sbapi_spectrometer_get_formatted_spectrum_length(DeviceId, SpecMetersId[0], &ERR);
	double *wavelenth = new double[sensorinof.BandNum]();
	sbapi_spectrometer_get_wavelengths(DeviceId, SpecMetersId[0], &ERR,wavelenth, sensorinof.BandNum);
	sensorinof.wavelenthlist.clear();
	sensorinof.WavelenthStr = "";
	int i = 0;
	for (i = 0; i < sensorinof.BandNum-1; i++)
	{
		sensorinof.WavelenthStr = sensorinof.WavelenthStr + QString::number(wavelenth[i], 'f', 4)+",";
		sensorinof.wavelenthlist.append(QString::number(wavelenth[i], 'f', 4));

	}
	sensorinof.WavelenthStr = sensorinof.WavelenthStr + QString::number(wavelenth[i], 'f', 4);
	sensorinof.wavelenthlist.append(QString::number(wavelenth[i], 'f', 4));
	sensorinof.maxValue = sbapi_spectrometer_get_maximum_intensity(DeviceId, SpecMetersId[0], &ERR);
	char namearr[100];
	//获取名称
	FLAG = sbapi_get_device_type(DeviceId, &ERR, namearr, 99);
	if (FLAG != 0)
	{
		QString sensorname = QString(namearr);
		QEType = getEMUbyQstring(sensorname);
		SensorType = sensorname;
		sensorinof.SensorName = sensorname;
		logout::out("设备型号：" + sensorname, 2);
	}
	return sensorinof;
}

void SensorQExPRO::SettingShutterOpen(bool isopen)
{
	//树莓派 4针脚
	system("gpio mode 4 output");
	if (isopen)
	{
		system("gpio write 4 1");
		logout::out("打开 Shutter gpio",2);
	}
	else
	{
		system("gpio write 4 0");
		logout::out("关闭 Shutter gpio",2);
	}
	if (QEType==QEPRO_QE)
	{

		SetQEProShutter(isopen);
	}
	QThread::msleep(100);

}

void SensorQExPRO::SettingSwitchShutter(bool isright)
{
	system("gpio mode 5 output");
	if (isright)
	{
		system("gpio write 5 1");
		logout::out("切换至SKY",2);
	}
	else
	{
		system("gpio write 5 0");
		logout::out("切换至VEG",2);
	}

}

void SensorQExPRO::GetOnePlot(STROnePlot &OnePlot, bool Dark)
{
	if (!SensorInfo.isSensorInit)
	{
		return;
	}
	if (!OnePlot.isInit || OnePlot.BandNum != SensorInfo.BandNum)
	{
		if (OnePlot.isInit)
		{

			delete[] OnePlot.Plot;
		}
		OnePlot.Plot = new float[SensorInfo.BandNum];
		OnePlot.BandNum = SensorInfo.BandNum;
		
	}
	if (!SensorInfo.isSensorInit)
	{
		return;

	}
	double *tempvalue = new double[SensorInfo.BandNum];
	OnePlot.shutter = shuttertime;
	sbapi_spectrometer_get_formatted_spectrum(DeviceId,SpecMetersId[0], &ERR, tempvalue, SensorInfo.BandNum);
	logout::out("获取数据：" + QString(sbapi_get_error_string(ERR)),5);
	if (QEType != USB2000PLUS_QE)
	{
		OnePlot.temp = GettingTempratrue();
		logout::out("获取温度：" + QString(sbapi_get_error_string(ERR)) + " 温度为：" + QString::number(OnePlot.temp, 'f', 2) + "℃",2);
	}


	for (size_t i = 0; i < SensorInfo.BandNum; i++)
	{
		OnePlot.Plot[i] = tempvalue[i];

	}
	OnePlot.isInit = true;
	delete[] tempvalue;
}

float SensorQExPRO::GettingTempratrue()
{
	if (!SensorInfo.isSensorInit)
	{
		return -1000;
	}
	 float temperature = (float)sbapi_tec_read_temperature_degrees_C(DeviceId, TemPratrueId[0], &ERR);
	 logout::out("当前温度为："+QString::number(temperature,'f',2),2);
	 return temperature;
}

void SensorQExPRO::CloseSensor()
{
	if (SensorInfo.isSensorInit)
	{
		sbapi_shutdown();
		SensorInfo.isSensorInit = false;
	}
}

void SensorQExPRO::GetOneDark(STROnePlot &darkplot)
{

}

EMQEType SensorQExPRO::getEMUbyQstring(QString str)
{
	if (str=="USB2000PLUS")
	{
		return USB2000PLUS_QE;
	}
	if (str == "QE65PRO")
	{
		return QE65PRO_QE;
	}
	if (str == "QE-PRO")
	{
		return QEPRO_QE;
	}
	if (str == "QE65000")
	{
		return QE65000_QE;
	}
	return NONQE;
}

void SensorQExPRO::SetQEProShutter( bool isopen)
{
	unsigned char GPIO4output[64] = { 0xc1, 0xc0, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00,
											0x10, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
											0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
											0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
											0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
											0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
											0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
											0x00, 0x00, 0x00, 0x00, 0xc5, 0xc4, 0xc3, 0xc2
	};
	unsigned char GPIO4on[64] = { 0xc1, 0xc0, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00,
									  0x10, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
									  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
									  0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
									  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									  0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									  0x00, 0x00, 0x00, 0x00, 0xc5, 0xc4, 0xc3, 0xc2
	};
	unsigned char GPIO4_off[64] = { 0xc1, 0xc0, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00,
									   0x10, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
									   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
									   0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
									   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									   0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
									   0x00, 0x00, 0x00, 0x00, 0xc5, 0xc4, 0xc3, 0xc2
	};

	long FNid;
	int FN = sbapi_get_number_of_raw_usb_bus_access_features(DeviceId, &ERR);
	FN = sbapi_get_raw_usb_bus_access_features(DeviceId, &ERR, &FNid, FN);
	QThread::msleep(100);
	int lenth = sbapi_raw_usb_bus_access_write(DeviceId, FNid, &ERR, GPIO4output, 64, 0x01);
	logout::out("设置shutter 为gpio",2);
	if (isopen)
	{
		lenth = sbapi_raw_usb_bus_access_write(DeviceId, FNid, &ERR, GPIO4on, 64, 0x01);
		logout::out("打开内部Shutter",2);
	}
	else
	{
		lenth = sbapi_raw_usb_bus_access_write(DeviceId, FNid, &ERR, GPIO4_off, 64, 0x01);
		logout::out("关闭内部Shutter",2);
	}
	

}
