
// FATP_FQCDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "FATP_FQC.h"
#include "FATP_FQCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

//CFATP_FQCDlg::m_filePath = {0};

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CFATP_FQCDlg 对话框




CFATP_FQCDlg::CFATP_FQCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFATP_FQCDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFATP_FQCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_list);
}

BEGIN_MESSAGE_MAP(CFATP_FQCDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(BTN_GET_LOG, &CFATP_FQCDlg::OnBnClickedGetLog)
	ON_BN_CLICKED(BTN_SHOP_INIT, &CFATP_FQCDlg::OnBnClickedShopInit)
	ON_BN_CLICKED(IDC_BUTTON1, &CFATP_FQCDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CFATP_FQCDlg 消息处理程序

BOOL CFATP_FQCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	// 初始化列表;
	m_list.InsertColumn( 0, _T("DSN"));
	m_list.SetColumnWidth( 0, 200 );
	m_list.InsertColumn( 1, _T("Log"));
	m_list.SetColumnWidth( 1, 100 );
	m_list.InsertColumn( 2, _T("percent"));
	m_list.SetColumnWidth( 2, 120);
	m_list.InsertColumn( 3, _T("Result"));
	m_list.SetColumnWidth( 3, 100);

#ifdef _DEBUG
	TCHAR szLogDir[MAX_PATH] = {0};
	_stprintf_s(szLogDir, _T("%s%s"), GLOBAL::g_szModulePath, _T("G9V28801216702RR"));
	CheckLog(szLogDir);
#endif

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CFATP_FQCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CFATP_FQCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CFATP_FQCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

BOOL CFATP_FQCDlg::CheckLog(CString strLogDir)
{
	if ( PathFileExists(strLogDir) )
	{
		INT nPassLog = 0;
		CString strLogFile;
		for ( int i = 1; i < 6; i++ )
		{
			strLogFile.Format(_T("%s\\radar_data_fan_dmesg_%d.log"), strLogDir, i);
			if ( !PathFileExists(strLogFile) )
				return FALSE;

			CStringList listStr;
			CString strData = GLOBAL::ReadFileContent(strLogFile);
			GLOBAL::GetStringList(strData, _T("\n"), listStr);
			INT nPassCount = GLOBAL::CheckListData(listStr);
			FLOAT fPercent = (float)nPassCount/(float)(listStr.GetSize()/2)*100;

			CString strResult;
			strResult.Format(_T("%s,log_%d,%d/%d=%.2f%%,%s"), 
				m_strDSN, 
				i, 
				nPassCount, 
				listStr.GetSize()/2, 
				fPercent, 
				(90.0 < fPercent) ? _T("PASS") : _T("FAIL"));
			m_listResult.AddTail(strResult);

			INT nItemCount = m_list.GetItemCount();
			m_list.InsertItem(nItemCount, m_strDSN);

			strResult.Format(_T("Log.%d"), i);
			m_list.SetItemText(nItemCount, 1, strResult);

			strResult.Format(_T("%d/%d=%.2f%%"), nPassCount, listStr.GetSize()/2, fPercent);
			m_list.SetItemText(nItemCount, 2, strResult);

			strResult.Format(_T("%s"), (90.0 < fPercent) ? _T("PASS") : _T("FAIL"));
			m_list.SetItemText(nItemCount, 3, strResult);

			if ( 90.0 < fPercent )
				nPassLog++;

			UpdateData();
		}

		GLOBAL::SaveList(m_listResult);
		m_listResult.RemoveAll();

		if ( nPassLog >= 1 )
			return TRUE;
	}

	return FALSE;
}

void CFATP_FQCDlg::OnBnClickedGetLog()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strStdOutput;
	TCHAR szApplicationName[MAX_PATH] = {0};
#ifdef _DEBUG
	_stprintf_s(szApplicationName, _T("%s%s"), GLOBAL::g_szModulePath, _T("test.bat"));
#else
	_stprintf_s(szApplicationName, _T("%s%s"), GLOBAL::g_szModulePath, _T("radar_data_collection_V03.bat"));
#endif
	if ( GLOBAL::StartProcess(szApplicationName, NULL, strStdOutput) )
	{
		// 获取DSN;
		CStringList listDSN;
		GLOBAL::GetStringList(strStdOutput, _T("\r\n"), listDSN);
		CString strDSN, strTemp;
		CString strFindStr = _T("folder is ");
		if ( GLOBAL::FindString(listDSN, strFindStr, strDSN) )
		{
			m_strDSN = strDSN.Mid(strDSN.Find(strFindStr) + strFindStr.GetLength());
			m_strDSN.Trim();

			strFindStr.Format(_T("[%s][PASS]radar fan data collection"), m_strDSN);
			if ( GLOBAL::FindString(listDSN, strFindStr, strTemp) )
			{
				CString strLogDir;
				strLogDir.Format(_T("%s%s"),GLOBAL::g_szModulePath, m_strDSN);
				if ( CheckLog(strLogDir) )
				{
					MessageBox(_T("日志统计成功,即将复位"), _T("提示"), MB_OK);
					OnBnClickedShopInit();
				}
				else
				{
					MessageBox(_T("日志统计失败"), _T("提示"), MB_ICONERROR);
				}
			}
			else
			{
				MessageBox(_T("未找到关键字<[PASS]radar fan data collection]>,执行结果失败"), _T("提示"), MB_ICONERROR);
			}
		}
		else
		{
			MessageBox(_T("未找到关键字<folder is>"), _T("提示"), MB_ICONERROR);
		}
	}
	else
	{
		MessageBox(_T("运行BAT失败"), _T("提示"), MB_ICONERROR);
	}
}

void CFATP_FQCDlg::OnBnClickedShopInit()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strStdOutput;
	TCHAR szApplicationName[MAX_PATH] = {0};
	_stprintf_s(szApplicationName, _T("%s%s"), GLOBAL::g_szModulePath, _T("radar_tv_set_shop_init_V03.bat"));
	if ( GLOBAL::StartProcess(szApplicationName, NULL, strStdOutput) )
	{
		// 获取DSN;
		CStringList listStr;
		GLOBAL::GetStringList(strStdOutput, _T("\r\n"), listStr);
		CString strResult;
		CString strFindStr = _T("[PASS]TV shop init finished ...[PASS]");
		if ( GLOBAL::FindString(listStr, strFindStr, strResult) )
		{
			MessageBox(_T("TV shop init finished ...[PASS]"), _T("提示"), MB_OK);
		}
		else
		{
			MessageBox(_T("未找到关键字<[PASS]TV shop init finished ...[PASS]>"), _T("提示"), MB_ICONERROR);
		}
	}
	else
	{
		MessageBox(_T("运行BAT失败"), _T("提示"), MB_ICONERROR);
	}
}

void CFATP_FQCDlg::OnBnClickedButton1()
{
	m_list.DeleteAllItems();
	TCHAR szFolderPath[MAX_PATH] = {0};
	GLOBAL::GetFilePath(szFolderPath, m_hWnd, _T("请选择Log文件夹！"), BIF_USENEWUI | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS);
	if ( !CheckLog(szFolderPath) )
	{
		MessageBox(_T("日志统计失败"), _T("提示"), MB_ICONERROR);
	}
	else
	{
		MessageBox(_T("日志统计成功"), _T("提示"), MB_OK);
	}
}
