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

#include "stdafx.h"
#include "SATHelper.h"
#include "DeviceView.h"
#include "MainFrm.h"
#include <filehelper.h>
extern CMainFrame* g_pMainFrame;
// CDeviceView

IMPLEMENT_DYNCREATE(CDeviceView, CFormView)

CDeviceView::CDeviceView()
	: CFormView(DLG_DEVICEMANAGER)
{
	ReadBaiduCfg(m_bdcfg);
	ReadRsRunCfg(m_rscfg);
	ReadTreePath();
}

CDeviceView::~CDeviceView()
{
}

void CDeviceView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_cb_chroma1);
	DDX_Control(pDX, IDC_COMBO2, m_cb_chroma2);
	DDX_Control(pDX, IDC_CHECK1, m_ck_chroma1);
	DDX_Control(pDX, IDC_CHECK2, m_ck_chroma2);
}

BEGIN_MESSAGE_MAP(CDeviceView, CFormView)
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	ON_WM_DEVICECHANGE()
	// 全步骤截图;
	ON_COMMAND(ID_CHECK_FULL_CUT, &CDeviceView::OnCheckFullCut)
	ON_UPDATE_COMMAND_UI(ID_CHECK_FULL_CUT, &CDeviceView::OnUpdateCheckFullCut)
	// 有声按钮;
	ON_COMMAND(BTN_HAD_SOUND, &CDeviceView::OnHadSound)
	ON_UPDATE_COMMAND_UI(BTN_HAD_SOUND, &CDeviceView::OnUpdateHadSound)
	// 声音默认按钮;
	ON_COMMAND(BTN_DEF_SOUND, &CDeviceView::OnDefSound)
	ON_UPDATE_COMMAND_UI(BTN_DEF_SOUND, &CDeviceView::OnUpdateDefSound)
	// 声音等级编辑框;
	// OCR账号下拉框;
	ON_COMMAND(ID_COMBOX_OCR, &CDeviceView::OnComboxOcr)
	ON_UPDATE_COMMAND_UI(ID_COMBOX_OCR, &CDeviceView::OnUpdateComboxOcr)
	// MenuTree项目下拉框;
	ON_COMMAND(ID_COMBO_PRODUCT, &CDeviceView::OnComboProduct)
	ON_UPDATE_COMMAND_UI(ID_COMBO_PRODUCT, &CDeviceView::OnUpdateComboProduct)
	// MenuTree UI下拉框;
	ON_COMMAND(ID_COMBO_UI, &CDeviceView::OnComboUi)
	ON_UPDATE_COMMAND_UI(ID_COMBO_UI, &CDeviceView::OnUpdateComboUi)
	// MenuTree 子UI下拉框;
	ON_COMMAND(ID_COMBO_SUBUI, &CDeviceView::OnComboSubUi)
	ON_UPDATE_COMMAND_UI(ID_COMBO_SUBUI, &CDeviceView::OnUpdateComboSubUi)
	// 信号仪1下拉框;
	ON_COMMAND(ID_COMBO1_22293, &CDeviceView::OnCombo1_22293)
	ON_UPDATE_COMMAND_UI(ID_COMBO1_22293, &CDeviceView::OnUpdateCombo1_22293)
	// 信号仪2下拉框;
	ON_COMMAND(ID_COMBO2_22293, &CDeviceView::OnCombo2_22293)
	ON_UPDATE_COMMAND_UI(ID_COMBO2_22293, &CDeviceView::OnUpdateCombo2_22293)
	// 信号仪1CheckBox;
	ON_COMMAND(ID_CHECK1_22293, &CDeviceView::OnCheck1_22293)
	ON_UPDATE_COMMAND_UI(ID_CHECK1_22293, &CDeviceView::OnUpdateCheck1_22293)
	// 信号仪2CheckBox;
	ON_COMMAND(ID_CHECK2_22293, &CDeviceView::OnCheck2_22293)
	ON_UPDATE_COMMAND_UI(ID_CHECK2_22293, &CDeviceView::OnUpdateCheck2_22293)
	//////////////////////////////////////////////////////////////////////////
	// 对话框控件;
	ON_BN_CLICKED(IDC_BUTTON1, &CDeviceView::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CDeviceView::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_CHECK1, &CDeviceView::OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_CHECK2, &CDeviceView::OnBnClickedCheck2)
	ON_CBN_SELCHANGE(IDC_COMBO1, &CDeviceView::OnCbnSelchangeCombo1)
	ON_CBN_SELCHANGE(IDC_COMBO2, &CDeviceView::OnCbnSelchangeCombo2)
	ON_WM_CREATE()
	
END_MESSAGE_MAP()


// CDeviceView 诊断

#ifdef _DEBUG
void CDeviceView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CDeviceView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDeviceView 消息处理程序


void CDeviceView::ReadBaiduCfg(BaiduCfg& bdcfg)
{
	TCHAR szValue[MAX_PATH] = {0};
	TCHAR szBuffer[1024] = { 0 };
	_stprintf_s(m_szBaiduCfgPath, _T("%s%s"), Global::g_szPython27Dir, _T("Lib\\site-packages\\ssat_sdk\\config\\baidu.cfg"));
	
	GetPrivateProfileString(_T("CurStatus"), _T("Cur_Count"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
	bdcfg.cur_count = szValue;

	GetPrivateProfileString(_T("CurStatus"), _T("Reset_Time"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
	bdcfg.reset_time = szValue;

	// 读取所有Section值;
	DWORD dwLen = GetPrivateProfileString(NULL, NULL, NULL, szBuffer, 1024, m_szBaiduCfgPath);
	for ( int i = 0, j = 0; i < dwLen; i++ )
	{
		if ( szBuffer[i] == '\0' )
		{
			TCHAR szData[MAX_PATH] = {0};
			memcpy(szData, &szBuffer[j], i-j);
			if ( _tcsicmp(szData, _T("CurStatus")) )
			{
				CountInfo countInfo;
				GetPrivateProfileString(szData, _T("APP_ID"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.app_id = szValue;

				GetPrivateProfileString(szData, _T("API_KEY"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.api_key = szValue;

				GetPrivateProfileString(szData, _T("SECRET_KEY"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.secret_key = szValue;

				GetPrivateProfileString(szData, _T("Ret_Count"), NULL, szValue, MAX_PATH, m_szBaiduCfgPath);
				countInfo.ret_count = szValue;

				bdcfg.list_count.insert(std::pair<std::string, CountInfo>(szData, countInfo));
			}
			OutputDebugString(szData);
			OutputDebugString("\n");
			j = i+1;
		}
	}
}

void CDeviceView::ReadRsRunCfg(RsRunCfg& rscfg)
{
	TCHAR szValue[MAX_PATH] = { 0 };
	TCHAR szBuffer[1024] = { 0 };
	_stprintf_s(m_szRsCfgPath, _T("%s%s"), Global::g_szPython27Dir, _T("Lib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"));

	GetPrivateProfileString(_T("Sound"), _T("sound_list"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sound_list = szValue;

	GetPrivateProfileString(_T("COMM"), _T("issendkeytakepicture_tester"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.full_cut = szValue;

	GetPrivateProfileString(_T("COMM"), _T("tg39_port"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.tg39_port = szValue;

	GetPrivateProfileString(_T("COMM"), _T("serial_communicator_port"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.tv_port = szValue;

	GetPrivateProfileString(_T("COMM"), _T("sat_home"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sat_home = szValue;

	GetPrivateProfileString(_T("COMM"), _T("sat_result_dir"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.sat_result_dir = szValue;

	GetPrivateProfileString(_T("COMM"), _T("chroma22293"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma22293 = szValue;

	//...
	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedchip"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_chip = szValue;

	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_style = szValue;

	GetPrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.menutree_channel = szValue;

	// SATHelper
	GetPrivateProfileString(_T("SATHelper"), _T("chroma1"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma1 = szValue;

	GetPrivateProfileString(_T("SATHelper"), _T("chroma2"), NULL, szValue, MAX_PATH, m_szRsCfgPath);
	rscfg.chroma2 = szValue;

}

void CDeviceView::ReadTreePath()
{
	// 必须在ReadRsRunCfg之后调用;
	TCHAR szRootDir[MAX_PATH] = {0};
	_stprintf_s(szRootDir, _T("%s%s"), m_rscfg.sat_home.c_str(), _T("resource\\MenuTree\\") );

	auto _get_dir = [](std::string dir){
		int nIndex = dir.find_last_of('\\');
		if (nIndex != std::string::npos)
		{
			return dir.substr(nIndex+1);
		}

		return dir;
	};

	auto _get_name = [](std::string dir) {
		int pos1 = dir.find_last_of('\\');
		if (pos1 != std::string::npos)
		{
			dir = dir.substr(pos1 + 1);
			int pos2 = dir.find_last_of('.');
			if (pos2 != std::string::npos)
			{
				dir = dir.substr(0, pos2);
				int pos3 = dir.find("MenuTree_");
				if (pos3 != std::string::npos)
				{
					return dir.substr(strlen("MenuTree_"));
				}
			}
		}

		return std::string();
	};

	// 查找根目录下的目录;
	filehelper fh;
	STR_VEC vt_dir;
	fh.getsubfolder(szRootDir, &vt_dir);
	for (auto it : vt_dir )
	{
		STR_VEC vt_subdir;
		std::vector<UITree> vtTree;
		fh.getsubfolder(it.c_str(), &vt_subdir);
		for ( auto ui : vt_subdir)
		{
			// 查找xls文件;
			UITree uitree;
			uitree.ui = ui;
			fh.getfiles_findout_subfolder(ui.c_str(), _T("*.xls"), &uitree.vtSubUI);
			vtTree.push_back(uitree);
		}

		m_map_tree.insert(std::pair<std::string, std::vector<UITree>>(_get_dir(it), vtTree));
	}

	// 去除目录级;
	for (std::map<std::string, std::vector<UITree>>::iterator it = m_map_tree.begin(); it != m_map_tree.end(); it++ )
	{
		for (std::vector<UITree>::iterator ui = it->second.begin(); ui != it->second.end(); ui++ )
		{
			ui->ui = _get_dir(ui->ui);
			for (std::vector<std::string>::iterator xls = ui->vtSubUI.begin(); xls != ui->vtSubUI.end();)
			{
				*xls = _get_name(xls->c_str());
				if ( xls->size() == 0 )
				{
					xls = ui->vtSubUI.erase(xls);
				}
				else
				{
					xls++;
				}
			}
		}
	}
}

void CDeviceView::ParseJson()
{
	if ( m_rscfg.chroma1.size() == 0 )
	{
		cJSON* pJson = cJSON_Parse(m_rscfg.chroma22293.c_str());
		if (pJson)
		{
			cJSON* pArray = cJSON_GetObjectItem(pJson, "devices");
			if (pArray)
			{
				int nCount = cJSON_GetArraySize(pArray);
				for ( int i = 0; i < nCount; i++ )
				{
					cJSON *pItem = cJSON_GetArrayItem(pArray, i);
					if ( pItem )
					{
						if (i == 0)
							m_rscfg.chroma1 = cJSON_GetObjectItem(pItem, "port") ? cJSON_GetObjectItem(pItem, "port")->valuestring : _T("");
						if (i == 1)
							m_rscfg.chroma2 = cJSON_GetObjectItem(pItem, "port") ? cJSON_GetObjectItem(pItem, "port")->valuestring : _T("");
					}
				}
			}
		}

		// 保存;
		WritePrivateProfileString(_T("SATHelper"), _T("chroma1"), m_rscfg.chroma1.c_str(), m_szRsCfgPath);
		WritePrivateProfileString(_T("SATHelper"), _T("chroma2"), m_rscfg.chroma2.c_str(), m_szRsCfgPath);
	}	
}

void CDeviceView::RunDevice()
{
	if ( m_rscfg.chroma1.size() )
	{
		CDevice* pdev = new CDevice();
		pdev->m_iBaudrate = 115200;
		pdev->m_iDatabit = 8;
		pdev->m_iPort = atoi(m_rscfg.chroma1.substr(3).c_str());
		pdev->m_iParitybit = 0;
		pdev->m_iStopbit = 1;
		pdev->m_iID = 1;
		pdev->m_strDeviceName = "Chroma22293";
		pdev->m_iDeviceType = 1;
		if (pdev->Open())
		{
			g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
			m_ck_chroma1.SetCheck(TRUE);
		}
		else
		{
			delete pdev;
			m_ck_chroma1.SetCheck(FALSE);
		}
	}
	
	if (m_rscfg.chroma2.size())
	{
		CDevice* pdev = new CDevice();
		pdev->m_iBaudrate = 115200;
		pdev->m_iDatabit = 8;
		pdev->m_iPort = atoi(m_rscfg.chroma2.substr(3).c_str());
		pdev->m_iParitybit = 0;
		pdev->m_iStopbit = 1;
		pdev->m_iID = 2;
		pdev->m_strDeviceName = "Chroma22293";
		pdev->m_iDeviceType = 1;
		if (pdev->Open())
		{
			g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
			m_ck_chroma2.SetCheck(TRUE);
		}
		else
		{
			delete pdev;
			m_ck_chroma2.SetCheck(FALSE);
		}
	}

	UpdateData(FALSE);
}

void CDeviceView::InitCombobox_port()
{
	CComboBox* pCommbox1 = ((CComboBox*)GetDlgItem(IDC_COMBO1));
	CComboBox* pCommbox2 = ((CComboBox*)GetDlgItem(IDC_COMBO2));
	for (std::vector<std::string>::iterator it = m_vtCOM.begin(); it != m_vtCOM.end(); it++)
	{
		if (!IsPortInserted(pCommbox1, it->c_str()))
			pCommbox1->AddString(it->c_str());

		if (!IsPortInserted(pCommbox2, it->c_str()))
			pCommbox2->AddString(it->c_str());
	}

	if ( m_rscfg.chroma1.size())
		pCommbox1->SelectString(0, m_rscfg.chroma1.c_str());
	if (m_rscfg.chroma2.size())
		pCommbox2->SelectString(0, m_rscfg.chroma2.c_str());
}

BOOL CDeviceView::IsPortInserted(CComboBox* pCommbox, LPCTSTR lpPort)
{
	BOOL bInserted = FALSE;
	CString strCommPort = _T("");
	int nCount = pCommbox->GetCount();
	for (int i = 0; i < nCount; i++)
	{
		pCommbox->GetLBText(i, strCommPort);
		if (strCommPort.CompareNoCase(lpPort) == 0)
		{
			bInserted = TRUE;
			break;
		}
	}

	return bInserted;
}

//实现串口热插拔  
BOOL CDeviceView::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
{
	switch (nEventType)
	{
		// 串口拨掉;
	case DBT_DEVICEREMOVECOMPLETE:
		break;
		// 串口插入;
	case DBT_DEVICEARRIVAL:
	{
		Global::GetSysSerialPort(m_vtCOM);
		InitCombobox_port();
	}
		break;
	default:
		break;
	}
	return TRUE;
}

void CDeviceView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO:  在此添加您专用的创建代码
	ParseJson();
	// Ribbon Combobox添加默认项;
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pOcrCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBOX_OCR));
	if (pOcrCombo)
	{
		for (auto it : m_bdcfg.list_count)
		{
			pOcrCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pOcrCombo->SetEditText(m_bdcfg.cur_count.c_str());
	}

	// 剩余次数;
	CMFCRibbonEdit* pOcrCount = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_COUNT));
	if (pOcrCount)
	{
		pOcrCount->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 声音等级;
	CMFCRibbonEdit* pSoundLevel = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_SOUND_LEVEL));
	if (pSoundLevel)
	{
		pSoundLevel->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 全步骤截图;
// 	CMFCRibbonCheckBox* pCheckFullCut = DYNAMIC_DOWNCAST(CMFCRibbonCheckBox, pRibbon->FindByID(ID_CHECK_FULL_CUT));
// 	if (pCheckFullCut)
// 	{
// 		pCheckFullCut->SetCheck();
// 	}

	// MenuTree;
	CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
	if (pChipCombo)
	{
		for (auto it : m_map_tree)
		{
			pChipCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pChipCombo->SetEditText(m_rscfg.menutree_chip.c_str());

		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
		for (auto it : m_map_tree.find(curText.GetString())->second)
		{
			pStyleCombo->AddItem(it.ui.c_str());
		}
		pStyleCombo->SetEditText(m_rscfg.menutree_style.c_str());
	}

	// 启动设备;
	Global::GetSysSerialPort(m_vtCOM);
	InitCombobox_port();
	RunDevice();
}


void CDeviceView::OnDraw(CDC* /*pDC*/)
{
	// TODO: 在此添加专用代码和/或调用基类
}


void CDeviceView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CFormView::OnPaint()

	CRect rc;
	GetWindowRect(&rc);
	static CImage mImage;
	static BOOL bLoad = FALSE;
	if (!bLoad)
	{
		bLoad = mImage.Load(_T("bk.jpg")) == S_OK;
	}
	
	if (bLoad)
	{
		// 缩放到最小后再拉大，区域为Null;
		if (rc.IsRectEmpty() || rc.IsRectNull())
			return;
		//...BUG,当出现滚动条时，绘制的内容出错;
		//这里让图片绘制区域和窗口大小一致
		mImage.Draw(GetDC()->GetSafeHdc(), CRect(0, 0, rc.Width(), rc.Height()));
	}
}


HBRUSH CDeviceView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor == CTLCOLOR_STATIC)
	{
		pDC->SetBkMode(TRANSPARENT);//<设置背景透明
		return (HBRUSH)::GetStockObject(NULL_BRUSH);
	}
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CDeviceView::OnCheckFullCut()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonButton* pFullCutCheck = DYNAMIC_DOWNCAST(CMFCRibbonButton, pRibbon->FindByID(ID_CHECK_FULL_CUT));
	if (pFullCutCheck)
	{
		WritePrivateProfileString(_T("COMM"), _T("issendkeytakepicture_tester"), !pFullCutCheck->IsChecked() ? _T("True") : _T("False"), m_szRsCfgPath);
		m_rscfg.full_cut = !pFullCutCheck->IsChecked() ? _T("True") : _T("False");
	}
}


void CDeviceView::OnUpdateCheckFullCut(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	pCmdUI->SetCheck(!_tcsicmp(m_rscfg.full_cut.c_str(), _T("True")));
}


void CDeviceView::OnHadSound()
{
	// TODO: 在此添加命令处理程序代码
}


void CDeviceView::OnUpdateHadSound(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CDeviceView::OnDefSound()
{
	// TODO: 在此添加命令处理程序代码
}


void CDeviceView::OnUpdateDefSound(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CDeviceView::OnComboxOcr()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pOcrCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBOX_OCR));
	if (pOcrCombo)
	{
		// 获取当前文本项;
		CString strOcr = pOcrCombo->GetEditText();
		WritePrivateProfileString(_T("CurStatus"), _T("Cur_Count"), strOcr, m_szBaiduCfgPath);
	}
}



void CDeviceView::OnUpdateComboxOcr(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CDeviceView::OnComboProduct()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
	if (pChipCombo)
	{
		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
		pStyleCombo->RemoveAllItems();
		for (auto it : m_map_tree.find(curText.GetString())->second)
		{
			pStyleCombo->AddItem(it.ui.c_str());
		}
		pStyleCombo->SelectItem(0);

		// SUBUI下拉框;
		CString strUIName = pStyleCombo->GetEditText();
		CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
		pSubUICombo->RemoveAllItems();
		std::vector<UITree> &ui = m_map_tree.find(curText.GetString())->second;
		for (std::vector<UITree>::iterator it = ui.begin(); it != ui.end(); it++)
		{
			if ( _tcsicmp(strUIName.GetString(), it->ui.c_str()) == 0 )
			{
				if ( it->vtSubUI.size() == 0 )
					break;

				for ( auto SubUI : it->vtSubUI )
				{
					pSubUICombo->AddItem(SubUI.c_str());
				}

				pSubUICombo->SelectItem(0);
			}
		}

		// 保存结果;
		CString strCurChip = pChipCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchip"), strCurChip, m_szRsCfgPath);

		CString strCurStyle = pStyleCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), strCurStyle, m_szRsCfgPath);

		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);
	}
}


void CDeviceView::OnUpdateComboProduct(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}

void CDeviceView::OnComboUi()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
	if (pStyleCombo)
	{
		CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		// SUBUI下拉框;
		CString strUIName = pStyleCombo->GetEditText();
		CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
		pSubUICombo->RemoveAllItems();
		std::vector<UITree>& ui = m_map_tree.find(curText.GetString())->second;
		for (std::vector<UITree>::iterator it = ui.begin(); it != ui.end(); it++)
		{
			if (_tcsicmp(strUIName.GetString(), it->ui.c_str()) == 0 )
			{
				if (it->vtSubUI.size() == 0)
					break;

				for (auto SubUI : it->vtSubUI)
				{
					pSubUICombo->AddItem(SubUI.c_str());
				}

				pSubUICombo->SelectItem(0);
			}
		}

		// UI下拉框;
		CString strCurStyle = pStyleCombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedstyle"), strCurStyle, m_szRsCfgPath);

		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);
	}
}

void CDeviceView::OnUpdateComboUi(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}

void CDeviceView::OnComboSubUi()
{
	// TODO: 在此添加命令处理程序代码
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pSubUICombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_SUBUI));
	if (pSubUICombo)
	{
		// UI下拉框;
		CString strSubStyle = pSubUICombo->GetEditText();
		WritePrivateProfileString(_T("MenuTree"), _T("menutreeselectedchannel"), strSubStyle, m_szRsCfgPath);
	}
}

void CDeviceView::OnUpdateComboSubUi(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}


void CDeviceView::OnCombo1_22293()
{
	// TODO: 在此添加命令处理程序代码
}

void CDeviceView::OnUpdateCombo1_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}



void CDeviceView::OnCombo2_22293()
{
	// TODO: 在此添加命令处理程序代码
}

void CDeviceView::OnUpdateCombo2_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CDeviceView::OnCheck1_22293()
{
	// TODO: 在此添加命令处理程序代码
}

void CDeviceView::OnUpdateCheck1_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

void CDeviceView::OnCheck2_22293()
{
	// TODO: 在此添加命令处理程序代码
}

void CDeviceView::OnUpdateCheck2_22293(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


//////////////////////////////////////////////////////////////////////////
// 说明：使用CFormView时，任务初始化动作都应该放在OnCreate中;
// 而非放在OnInitialUpdate中执行，因为OnInitialUpdate可能被调用多次;
int CDeviceView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

#if 0
	// TODO:  在此添加您专用的创建代码
	ParseJson();
	// Ribbon Combobox添加默认项;
	CMFCRibbonBar* pRibbon = g_pMainFrame->GetRibbonBar();
	ASSERT_VALID(pRibbon);
	CMFCRibbonComboBox* pOcrCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBOX_OCR));
	if (pOcrCombo)
	{
		for (auto it : m_bdcfg.list_count)
		{
			pOcrCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pOcrCombo->SetEditText(m_bdcfg.cur_count.c_str());
	}

	// 剩余次数;
	CMFCRibbonEdit* pOcrCount = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_COUNT));
	if (pOcrCount)
	{
		pOcrCount->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 声音等级;
	CMFCRibbonEdit* pSoundLevel = DYNAMIC_DOWNCAST(CMFCRibbonEdit, pRibbon->FindByID(ID_EDIT_SOUND_LEVEL));
	if (pSoundLevel)
	{
		pSoundLevel->SetEditText(m_bdcfg.reset_time.c_str());
	}

	// 全步骤截图;
// 	CMFCRibbonCheckBox* pCheckFullCut = DYNAMIC_DOWNCAST(CMFCRibbonCheckBox, pRibbon->FindByID(ID_CHECK_FULL_CUT));
// 	if (pCheckFullCut)
// 	{
// 		pCheckFullCut->SetCheck();
// 	}

	// MenuTree;
	CMFCRibbonComboBox* pChipCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_PRODUCT));
	if (pChipCombo)
	{
		for (auto it : m_map_tree)
		{
			pChipCombo->AddItem(it.first.c_str());
		}

		// 选择默认项;
		pChipCombo->SetEditText(m_rscfg.menutree_chip.c_str());

		// UI下拉框;
		CString curText = pChipCombo->GetEditText();
		CMFCRibbonComboBox* pStyleCombo = DYNAMIC_DOWNCAST(CMFCRibbonComboBox, pRibbon->FindByID(ID_COMBO_UI));
		for (auto it : m_map_tree.find(curText.GetString())->second)
		{
			pStyleCombo->AddItem(it.c_str());
		}
		pStyleCombo->SetEditText(m_rscfg.menutree_style.c_str());
	}

	// 启动设备;
	Global::GetSysSerialPort(m_vtCOM);
	InitCombobox_port();
	RunDevice();
#endif

	return 0;
}

void CDeviceView::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CDeviceView::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CDeviceView::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
	CDevice* pDev = g_dmap.find(1)->second;
	if (m_ck_chroma1.GetCheck())
		pDev->Open();
	else
		pDev->Close();
}


void CDeviceView::OnBnClickedCheck2()
{
	// TODO: 在此添加控件通知处理程序代码
	CDevice* pDev = g_dmap.find(2)->second;
	if (m_ck_chroma2.GetCheck())
		pDev->Open();
	else
		pDev->Close();
}


void CDeviceView::OnCbnSelchangeCombo1()
{
	// 保存到配置文件中;
	int nCurSel = m_cb_chroma1.GetCurSel();
	if ( nCurSel != CB_ERR )
	{
		CString strChroma1;
		m_cb_chroma1.GetLBText(nCurSel, strChroma1);
		WritePrivateProfileString(_T("SATHelper"), _T("chroma1"), strChroma1, m_szRsCfgPath);

		CDevice *pDev = g_dmap.find(1)->second;
		int port = atoi(strChroma1.Right(1).GetString());
		if (pDev->m_iPort != port)
		{
			pDev->m_iPort = port;
			pDev->Close();
			m_ck_chroma1.SetCheck(pDev->Open());
		}
	}	
}


void CDeviceView::OnCbnSelchangeCombo2()
{
	int nCurSel = m_cb_chroma2.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strChroma2;
		m_cb_chroma2.GetLBText(nCurSel, strChroma2);
		WritePrivateProfileString(_T("SATHelper"), _T("chroma2"), strChroma2, m_szRsCfgPath);

		CDevice* pDev = g_dmap.find(2)->second;
		int port = atoi(strChroma2.Right(1).GetString());
		if (pDev->m_iPort != port)
		{
			pDev->m_iPort = port;
			pDev->Close();
			m_ck_chroma2.SetCheck(pDev->Open());
		}
	}
}
