#include "pch.h"
#include "ATPControl_Serial_QT.h"
#include "Logger.h"
#include "SystemConfigger.h"
#include "MainDataGrabber.h"
#include "DataFileProcessor.h"
#include "Scheduler.h"
#include "GY39Controller.h"
using namespace std;
#pragma once


int main(int argc, char *argv[])
{
	using namespace ZZ_MISCDEF::ZZ_RUNPARAMS;
	using namespace ZZ_MISCDEF::MISC_DETECTOR;

	QCoreApplication a(argc, argv);

	system("gpio mode 1 output");//控制针脚初始化
	qDebug() << "gpio mode 1 output......" << endl;
	//////////////////////////////////////////////////////////////////////////
	////
	GY39Controller           m_ctrlHumitureDetector;
	DataFileProcessor        m_dfpSaver;
	ZZ_SysConfigger          m_scConfiggerLoader;
	Scheduler                m_sTimer;
	CMainDataGrabber         m_mdgGrabber;

	////
	bool bRes;
	float m_fChassisTemp, m_fChassisHum;

	RunTimeGrabberParams m_struRuntimeParams;
	EContext             m_struEC;
	MEContext            m_struMEC;
	HumitureDeviceInfo   m_struHumitureDI;
	//////////////////////////////////////////////////////////////////////////logger
	QT_LOG::ZZ_InitLogger("/home/data/Log/");
	
	
	//m_test123;
	//m_test123.Initialize("COM11");
	//m_test123.GetHumiture(fTemp, fHum);
	//////////////////////////////////////////////////////////////////////////config
	system("gpio write 1 1");//设备上电
	qDebug() << "gpio write 1 1......" << endl;

	bRes = m_scConfiggerLoader.Initialize();
	if (bRes)
	{
		return -1;
	}

	m_scConfiggerLoader.GetParams(m_struRuntimeParams, m_struEC, m_struMEC, m_struHumitureDI);

	m_sTimer.SetAcqTimeParams(m_struRuntimeParams.atsParams);
	m_dfpSaver.SetEnvironmentContex(m_struEC);
	m_dfpSaver.SetManmadeEnviromentalContext(m_struMEC);

	m_ctrlHumitureDetector.Initialize(m_struHumitureDI.qstrInterfaceName.toStdString());
	//////////////////////////////////////////////////////////////////////////prepare
	m_sTimer.Preheating();
	m_ctrlHumitureDetector.GetHumiture_retry(m_fChassisTemp, m_fChassisHum);

	m_struEC.qstrCaseTemperature= QString("%1").arg(m_fChassisTemp);
	m_struEC.qstrCaseHumidity = QString("%1").arg(m_fChassisHum);
	//////////////////////////////////////////////////////////////////////////
	QThread* m_pqDataGrabberThreadHolder = new QThread();
	QThread* m_pqTimerThreadHolder = new QThread();

	m_mdgGrabber.moveToThread(m_pqDataGrabberThreadHolder);
	m_sTimer.moveToThread(m_pqTimerThreadHolder);

	m_mdgGrabber.SetContext(m_struRuntimeParams, m_dfpSaver, m_sTimer);
	m_mdgGrabber.Init_Normal();

	m_pqDataGrabberThreadHolder->start();
	m_pqTimerThreadHolder->start();

	m_mdgGrabber.Init_Self();
	m_sTimer.SelfStart();
	//////////////////////////////////////////////////////////////////////////test
// 	DataFileProcessor testdp;
// 	ZZ_SysConfigger test;
// 	Scheduler       testS;
// 
// 	RunTimeGrabberParams testp;
// 	EContext testEC;
// 	MEContext testMEC;
// 	test.Initialize();
// 	test.LoadSettingsFromFile_System();
// 	test.LoadSettingsFromFile_MISC();
// 	test.GetGrabberRunParams(testp);
// 	test.GetECParams(testEC);
// 	test.GetMECParams(testMEC);
// 
// 	testS.SetAcqTimeParams(testp.atsParams);
//  	testdp.SetEnvironmentContex(testEC);
//  	testdp.SetManmadeEnviromentalContext(testMEC);
 	//testdp.WriteDataFile();
 	//////////////////////////////////////////////////////////////////////////test
 	//CMainDataGrabber testDG;
 	//QThread* m_pqDataGrabberThreadHolder = new QThread();
 	//testDG.moveToThread(m_pqDataGrabberThreadHolder);
	//testDG.SetContext(testp, testdp, testS);
	//testDG.Init_Normal();
	//testDG.SetGrabberFileProcessor(testdp);
	//testDG.SetGrabberParams(testp);
	//testDG.SetTimer(testS);
	//testDG.InitThreadStatus();
	//testDG.InitializeWorkers();
	//testDG.SetupMsgPipelines();
	//testDG.StartWorkers();
	//m_pqDataGrabberThreadHolder->start();
	//testDG.Init_Self();
	//testDG.InitThreadStatus();
	//testDG.InitializeWorkers();
	//testDG.InitLS();
	//testDG.SetupMsgPipelines();
	//testDG.StartWorkers();
 	//testDG.StartGrab();
	//
 	//QThread* m_pqTimerThreadHolder = new QThread();
 	//testS.moveToThread(m_pqTimerThreadHolder);
 	//m_pqTimerThreadHolder->start();
 	//testS.SelfStart();
	//
	//QString qstrTest = "This is a test message 2";
	//qDebug() << qstrTest;
	//cout << "This is a test message";
	//
	//test.LoadSettings_Test();
	//
	//ZZ_DataGrabberThread m_test;
	//m_test.GrabOnce();
	//QThread*      m_pqDataGrabberThreadHolder;
	//
	//
	//m_pqDataGrabberThreadHolder->start();
	//m_test.TestGrabOnce();
	//m_test.StartGrabCall();
	//QThread::msleep(5000);
	//
	//m_test.StopTestGrab();
// 	ZZ_ATPControl_Serial_Qt m_ctrlATP;
// 	QByteArray qbTest;
// 	m_ctrlATP.ATPInitialize(7);
// 	m_ctrlATP.GetDeviceAttribute();
// 	m_ctrlATP.SetExposureTime(m_ctrlATP.m_adaDeviceAttr.iMinIntegrationTime);
// 	m_ctrlATP.RecvData(qbTest);
	//////////////////////////////////////////////////////////////////////////
	return a.exec();
}



