#include "OptoSkyType.h"
#include <QSerialPort>  
#include <QSerialPortInfo> 
#include "QTime"
#include "logout.h"
QSerialPort *optoskyserial = nullptr;
bool IsOptoskyinit = false;
#include"qdebug.h"
void OptoSky::SetPortName(QString comname, qint32 bandrate)
{

	QSerialPortInfo info;
	QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
	int i = 0;
	qDebug() << comname;
	foreach(info, infos) {
		if (info.portName() == comname) break;
		qDebug() << info.portName();
		i++;
	}
	if (i != infos.size())
	{
		if (optoskyserial == nullptr)
		{
			optoskyserial = new QSerialPort;
		}
		optoskyserial->close();
		optoskyserial->setPort(info);
		optoskyserial->open(QIODevice::ReadWrite);
		qint32 b = bandrate;
		optoskyserial->setBaudRate(b);
	}
	else
	{
		IsOptoskyinit = false;
		return;
	}
	IsOptoskyinit = true;

}

void OptoSky::closeport()
{
	if (optoskyserial!=nullptr)
	{
		delete optoskyserial;
		optoskyserial = nullptr;
	}
}

void OptoSky::SendComment(QByteArray Comment)
{
	unsigned short lenth = Comment.size() + 3;
	QByteArray header;
	header.resize(4);
	header[0] = 0xaa;
	header[1] = 0x55;
	header[2] = lenth / 256;
	header[3] = lenth % 256;
	header.append(Comment);
	int sum = 0;
	for (int i = 0; i < lenth-1; i++)
	{
		sum = sum +header[i+2];
	}
	header.append(sum % 256);
	if (IsOptoskyinit)
	{
		optoskyserial->readAll();
		optoskyserial->write(header);
		optoskyserial->waitForBytesWritten(200);
		
	}
	//char *a = header.data();
	//int c=sizeof(header);

}

bool OptoSky::GetReturn(char id, QByteArray &data)
{
	QByteArray b;
	b.append(id);
	return GetReturn(b, data);
}

bool OptoSky::GetReturn(QByteArray &data)
{
	data.clear();

	data = optoskyserial->readAll();
	QTime time;
	time.start();
lable1:
	while (data.size() < 5)
	{
		optoskyserial->waitForReadyRead(50);
		QByteArray tempbuf = optoskyserial->readAll();
		data.append(tempbuf);

		if (time.elapsed() > 100000)
		{
			logout::out("long time no data retrun ");
			return false;

		}
	}
	if ((unsigned char)data[0] != 0xaa || (unsigned char)data[1] != 0x55)
	{
		data.remove(0, 1);
		goto lable1;
	}

	int datalenth = data[2] * 256 + data[3] + 2;

	while (data.size() < datalenth)
	{
		optoskyserial->waitForReadyRead(50);
		data.append(optoskyserial->readAll());

		if (time.elapsed() > 150000)
		{
			logout::out("long time no data retrun " );
			return false;

		}
	}

	if (data.size() > datalenth)
	{
		data.remove(datalenth - 1, data.size() - datalenth);
	}
	int checkleth = datalenth - 3;
	unsigned int checknum = 0;
	for (size_t i = 0; i < checkleth; i++)
	{

		checknum = checknum + data.at(i + 2);
	}
	checknum = checknum % 256;
	if (checknum != (unsigned char)data[data.size() - 1])
	{
		logout::out("checkerro  " );
		//data.clear();
		//return false;
	}
	return true;

}

bool OptoSky::GetReturn(QByteArray id, QByteArray &data)
{
	if (!GetReturn(data))
	{
		return false;
	}
	
	if (data[4]!=id[0])
	{
		logout::out("retrun is not correct with ComId:" + QString::number(id[0], 16));
		return false;//������ز�����Ҫ���򷵻ش���
	}

	RemoveHeaderandEnd(data);
	return true;

}

void OptoSky::SendComment(char comid, QByteArray Data)
{
	QByteArray tem;
	tem.append(comid);
	tem.append(Data);
	SendComment(tem);
}

void OptoSky::SendComment(char com)
{
	QByteArray temp;
	temp.append(com);
	SendComment(temp);
}

bool OptoSky::isSensorInit()
{
	return IsOptoskyinit;
}

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

void OptoSky::SendComment(QByteArray Comment, QByteArray Data)
{
	Comment.append(Data);
	SendComment(Comment);
}


