// xmlapp.cpp: implementation of the xmlapp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "xmlapp.h"
#include "Markup.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXMLApp::CXMLApp()
{

}

CXMLApp::~CXMLApp()
{

}

bool CXMLApp::init()
{
	CString Temp;
	int index = 0;

	for (index = 0; index < EQUIP_MAX_NUM; index++)
	{
		equip[index].init(_T(""), 0, 0, 0, 0);
	}
	index = 0;
	equip[index++].init(_T("DAM0200"), 4, 0, 0, 1);
	equip[index++].init(_T("DAM0400"), 4, 0, 0, 1);
	equip[index++].init(_T("DAM0404"), 4, 4, 0, 1);
	equip[index++].init(_T("DAM0800"), 8, 0, 0, 1);
	equip[index++].init(_T("DAM1600"), 16, 0, 0, 1);
	equip[index++].init(_T("DAM0606"), 6, 6, 0, 1);
	equip[index++].init(_T("USB0100"), 1, 0, 0, 1);
	equip[index++].init(_T("DAM2010"), 0, 20, 10, 1);
	equip[index++].init(_T("DAM1012A"), 10, 0, 12, 1);
	equip[index++].init(_T("DAM1012D"), 10, 12, 0, 1);
	equip[index++].init(_T("DAM10102"), 10, 10, 2, 1);
	equip[index++].init(_T("DAM14142"), 2, 14, 14, 1);	
	equip[index++].init(_T("DO0200"), 2, 0, 0, 1);
	equip[index++].init(_T("PT0030"), 0, 0, 3, 1);
	equip[index++].init(_T("WT0040"), 0, 0, 4, 1);
	equip[index++].init(_T("DAM4100"), 0, 0, 4, 1);
	equip[index++].init(_T("DAM6640"), 6, 4, 6, 1);
	equip[index++].init(_T("DAM6120"), 9, 0, 12, 1);
	//DO0400;DO0800;DO1600;DAM6600;USB0100;DAM2010;DAM1010;DAMDD02;DO0200;DO1012;
	

	for (int index = 0; index < EQUIP_DI_NUM; index++)
	{
		Temp.Format(_T("%d#"), index + 1);
		cdi[index].init(Temp, 0, 1);
	}

	for (int index = 0; index < EQUIP_DO_NUM; index++)
	{
		Temp.Format(_T("继电器%d"), index + 1);
		cdo[index].init(Temp,0,1);
	}

	for (int index = 0; index < EQUIP_AI_NUM; index++)
	{
		Temp.Format(_T("AI%d"), index + 1);
		cai[index].init(Temp, _T(""),1,0,1);
	}

	cset.comname = _T("COM1");
	cset.baud = _T("9600");
	cset.EquipName = _T("DO0400");
	cset.addr = _T("254");
	return true;
}
bool CXMLApp::CreateXmlFile(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
	
	
	CMarkup xml;
	xml.SetDoc(__T("<?xml version=\"1.0\"?>\r\n"));
	xml.AddElem(__T("Server"));
	xml.IntoElem();

	xml.AddElem(_T("公司信息"));
	xml.AddAttrib(_T("公司名"), CommpanyName);
	//-------------串口配置信息
	for (int index = 0; index < EQUIP_MAX_NUM; index++)
	{
		Temp.Format(_T("产品%d"),index+1);
		xml.AddElem(Temp);
		xml.AddAttrib(_T("名称"), equip[index].name);
		xml.AddAttrib(_T("DI"), equip[index].dinum);
		xml.AddAttrib(_T("DO"), equip[index].donum);
		xml.AddAttrib(_T("AI"), equip[index].ainum);
		xml.AddAttrib(_T("有效"), equip[index].IsValid);
	}
	xml.OutOfElem();//Point		
	
	xml.Save(methord::GetAppPath()+"\\"+szFileName);
    return true;
}

bool CXMLApp::ReadXmlFile(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
	
	
	CMarkup xml; 
	if(xml.Load(methord::GetAppPath()+"\\"+szFileName)==false)return false;
	
	xml.ResetMainPos();
	if ( !(xml.FindElem(__T("Server"))) )
	{		
		return false;
	}
    xml.IntoElem();

	if (xml.FindElem(_T("公司信息")))
	{
		CommpanyName = xml.GetAttrib(_T("公司名"));
	}
	
	for (int index = 0; index < EQUIP_MAX_NUM; index++)
	{
		Temp.Format(_T("产品%d"), index + 1);
		if (xml.FindElem(Temp))
		{			
			equip[index].name = xml.GetAttrib(_T("名称"));
			equip[index].dinum = _ttoi(xml.GetAttrib(_T("DI")));
			equip[index].donum = _ttoi(xml.GetAttrib(_T("DO")));
			equip[index].ainum = _ttoi(xml.GetAttrib(_T("AI")));
			equip[index].IsValid = _ttoi(xml.GetAttrib(_T("有效")));
		}
	}
	xml.OutOfElem();//
	
    return true;
}



bool CXMLApp::CreateXmlFile2(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false


	CMarkup xml;
	xml.SetDoc(__T("<?xml version=\"1.0\"?>\r\n"));
	xml.AddElem(__T("Server"));
	xml.IntoElem();

	//-------------串口配置信息
	for (int index = 0; index < EQUIP_DI_NUM; index++)
	{
		Temp.Format(_T("DI%d"), index + 1);
		xml.AddElem(Temp);
		xml.AddAttrib(_T("名称"), cdi[index].name);
		xml.AddAttrib(_T("反相"), cdi[index].Isdir);
		xml.AddAttrib(_T("有效"), cdi[index].IsValid);
	}

	for (int index = 0; index < EQUIP_DO_NUM; index++)
	{
		Temp.Format(_T("DO%d"), index + 1);
		xml.AddElem(Temp);
		xml.AddAttrib(_T("名称"), cdo[index].name);
		xml.AddAttrib(_T("反相"), cdo[index].Isdir);
		xml.AddAttrib(_T("有效"), cdo[index].IsValid);
	}

	for (int index = 0; index < EQUIP_AI_NUM; index++)
	{
		Temp.Format(_T("AI%d"), index + 1);
		xml.AddElem(Temp);
		xml.AddAttrib(_T("名称"), cai[index].name);
		xml.AddAttrib(_T("单位"), cai[index].unit);

		Temp.Format(_T("%f"), cai[index].a);
		xml.AddAttrib(_T("倍率a"), Temp);
		Temp.Format(_T("%f"), cai[index].b);
		xml.AddAttrib(_T("基数b"), Temp);
		xml.AddAttrib(_T("有效"), cai[index].IsValid);
	}
	xml.OutOfElem();//Point		

	xml.Save(methord::GetAppPath() + "\\" + szFileName);
	return true;
}

bool CXMLApp::ReadXmlFile2(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false


	CMarkup xml;
	if (xml.Load(methord::GetAppPath() + "\\" + szFileName) == false)return false;

	xml.ResetMainPos();
	if (!(xml.FindElem(__T("Server"))))
	{
		return false;
	}
	xml.IntoElem();

	for (int index = 0; index < EQUIP_DI_NUM; index++)
	{
		Temp.Format(_T("DI%d"), index + 1);
		if (xml.FindElem(Temp))
		{
			cdi[index].name = xml.GetAttrib(_T("名称"));
			cdi[index].Isdir = _ttoi(xml.GetAttrib(_T("反相")));
			cdi[index].IsValid = _ttoi(xml.GetAttrib(_T("有效")));
		}
	}

	for (int index = 0; index < EQUIP_DO_NUM; index++)
	{
		Temp.Format(_T("DO%d"), index + 1);
		if (xml.FindElem(Temp))
		{
			cdo[index].name = xml.GetAttrib(_T("名称"));
			cdo[index].Isdir = _ttoi(xml.GetAttrib(_T("反相")));
			cdo[index].IsValid = _ttoi(xml.GetAttrib(_T("有效")));
		}
	}

	for (int index = 0; index < EQUIP_AI_NUM; index++)
	{
		Temp.Format(_T("AI%d"), index + 1);
		if (xml.FindElem(Temp))
		{
			cai[index].name = xml.GetAttrib(_T("名称"));
			cai[index].unit = xml.GetAttrib(_T("单位"));
			cai[index].a = _ttof(xml.GetAttrib(_T("倍率a")));
			cai[index].b = _ttof(xml.GetAttrib(_T("基数b")));
			cai[index].IsValid = _ttoi(xml.GetAttrib(_T("有效")));
		}
	}
	xml.OutOfElem();//

	return true;
}



bool CXMLApp::CreateXmlFile3(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false


	CMarkup xml;
	xml.SetDoc(__T("<?xml version=\"1.0\"?>\r\n"));
	xml.AddElem(__T("Server"));
	xml.IntoElem();

	//-------------串口配置信息
	xml.AddElem(__T("串口号"), cset.comname);
	xml.AddElem(__T("波特率"), cset.baud);
	xml.AddElem(__T("型号"), cset.EquipName);
	xml.AddElem(__T("地址"), cset.addr);
	xml.OutOfElem();//Point		

	xml.Save(methord::GetAppPath() + "\\" + szFileName);
	return true;
}

bool CXMLApp::ReadXmlFile3(CString szFileName)
{
	CString Temp;
	//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false


	CMarkup xml;
	if (xml.Load(methord::GetAppPath() + "\\" + szFileName) == false)return false;

	xml.ResetMainPos();
	if (!(xml.FindElem(__T("Server"))))
	{
		return false;
	}
	xml.IntoElem();
	if (xml.FindElem(__T("串口号")))	cset.comname = (xml.GetData());//
	if (xml.FindElem(__T("波特率")))cset.baud = (xml.GetData());//
	if (xml.FindElem(__T("型号")))	cset.EquipName = (xml.GetData());//
	if (xml.FindElem(__T("地址")))	cset.addr = (xml.GetData());//
	xml.OutOfElem();//

	return true;
}
