//#include "stdafx.h"					// QT删掉stdafx头文件引用
#include "SecsBase.h"
#include <TlHelp32.h>				// QT删掉TlHelp32头文件引用
#pragma warning(disable:4996)


void RcResult::_parse(const char *rcResultString)
{
	std::vector<std::string> sv;
	listSplit(rcResultString, sv);
	if(sv.size() == 2)
	{
		rc = atoi(sv[0].c_str());
		result = sv[1];
	} else 
	{
		// 其他错误
		rc = -1;
		result = rcResultString;
	}
}

std::string RcResult::ToString(void)
{
	char buff[200];
	sprintf(buff, "%d", rc);
	std::string s(buff);
	listAppend(s, result.c_str());
	return s;
}


// 将text链表分解成元素字符串，返回数值为0代表成功
RcResult listSplit(const char *srcList, std::vector<std::string> &sv) 
{
	int64 argc;
	char **argv;
	sv.clear();
	int rc = (int)imc_listSplit(srcList, &argc, &argv);
	if(rc!=0) return GetErrorByCode(rc);
	sv.reserve((__int32)argc);
	for(int i=0; i<argc; i++) { sv.push_back(argv[i]);}
	imc_free((void *)argv);
	return GetErrorByCode(rc);
}

// 将text链表分解成元素字符串，返回数值为0代表成功
RcResult listSplit(const char *srcList, std::list<std::string> &sv) 
{
	int64 argc;
	char **argv;
	sv.clear();
	int rc = (int)imc_listSplit(srcList, &argc, &argv);
	if(rc!=0) return GetErrorByCode(rc);
	for(int i=0; i<argc; i++) { sv.push_back(argv[i]);}
	imc_free((void *)argv);
	return GetErrorByCode(rc);
}

// 将text链表分解成元素字符串，返回数值为0代表成功
RcResult listSplit(const std::string &srcList, std::vector<std::string> &sv) 
{
	return listSplit(srcList.c_str(), sv); 
}

// 将text链表分解成元素字符串，返回数值为0代表成功
RcResult listSplit(const std::string &srcList, std::list<std::string> &sv) 
{
	return listSplit(srcList.c_str(), sv); 
}

// 加入字符串元素到链表
std::string listJoin(std::vector<std::string> &sv) 
{
	char const** pArgv;
	pArgv = (const char**)imc_malloc(sizeof(const char**) * sv.size());		
	if (pArgv == NULL)
	{	
		return "";
	}
	for(std::vector<std::string>::size_type i=0; i<sv.size(); i++)
	{ 
		pArgv[i] = sv[i].c_str();
	}
	char *cptr = imc_listJoin((int)sv.size(), pArgv);
	imc_free(pArgv);
	if(cptr == NULL)
	{
		return ""; // should never happen
	}
	std::string s(cptr);
	imc_free(cptr);
	return s;
}


// 加入字符串元素到链表
std::string listJoin(std::list<std::string> &sl) 
{
	int i =0;
	char const** pArgv;
	pArgv = (const char**)imc_malloc(sizeof(const char**) * sl.size());	
	if (pArgv == NULL)
	{
		return "";
	}
	for(std::list<std::string>::iterator ite = sl.begin(); ite != sl.end(); ite++, i++)
	{ 
		pArgv[i] = ite->c_str();
	}
	char *cptr = imc_listJoin((int)sl.size(), pArgv);
	imc_free(pArgv);
	if(cptr == NULL) 
	{
		return ""; // 出现的情况下，电脑已经出故障了
	}
	std::string s(cptr);
	imc_free(cptr);
	return s;
}


// 加入字符串元素到链表
std::string listJoin(int argc, const char *argv[]) {
	char *cptr = imc_listJoin(argc, argv);
	if(cptr == NULL)
	{
		return ""; 
	}
	std::string s(cptr);
	imc_free(cptr);
	return s;
}

std::string listJoin(int count, std::string s1, std::string s2, std::string s3, 
	std::string s4, std::string s5, std::string s6)
{
	std::vector<std::string> sv;
	sv.push_back(s1);

	if (count > 1)	{sv.push_back(s2);	}
	if (count > 2)	{sv.push_back(s3);	}
	if (count > 3)	{sv.push_back(s4);	}
	if (count > 4)	{sv.push_back(s5);	}
	if (count > 5)	{sv.push_back(s6);	}
	return listJoin(sv);	
}

// 加入字符串元素到链表
std::string listJoin(const char *e1, const char *e2, const char *e3,
	const char *e4, const char *e5, const char *e6)
{
	std::vector<std::string> sv;
	sv.push_back(e1);	
	if(e2 != NULL) { sv.push_back(e2);	}
	if(e3 != NULL) { sv.push_back(e3);	}
	if(e4 != NULL) { sv.push_back(e4);	}
	if(e5 != NULL) { sv.push_back(e5);	}
	if(e6 != NULL) { sv.push_back(e6);	}

	return listJoin(sv);
}


// 从出现NULL的哪一个起，后面的都忽略，不添加到链表当中
//
void listAppend(std::string& desList, const char *e1, const char *e2, const char *e3,
	const char *e4, const char *e5, const char *e6)
{
	const char* srcList[6];
	srcList[0] = e1;
	srcList[1] = e2;
	srcList[2] = e3;
	srcList[3] = e4;
	srcList[4] = e5;
	srcList[5] = e6;
	char *cptr = imc_listAppend(desList.c_str(), srcList, 6);
	if (cptr != NULL)
	{
		desList = std::string(cptr);
	}	
	imc_free(cptr);
}

// append one or more elements to the end of a list
void listAppend(std::string &desList, int count, std::string s1, std::string s2, std::string s3,
	std::string s4, std::string s5, std::string s6)
{
	int i = 0;
	const char* pStr[6] = {0};
	pStr[i++] = s1.c_str();
	if (count > 1)	{pStr[i++] = s2.c_str(); }
	if (count > 2)	{pStr[i++] = s3.c_str(); }
	if (count > 3)	{pStr[i++] = s4.c_str(); }
	if (count > 4)	{pStr[i++] = s5.c_str(); }
	if (count > 5)	{pStr[i++] = s6.c_str(); }

	i = 0;
	listAppend(desList, pStr[i++], pStr[i++], pStr[i++], pStr[i++], pStr[i++], pStr[i++]);
}

// 提取srcList中第index1 的 index2 的 index3元素
std::string listElement(std::string &srcList, int index1, int index2, int index3)
{
	std::string data;
	char* cptr = imc_listElement(srcList.c_str(), index1, index2, index3);
	if(cptr != NULL)
	{
		data = cptr;
	}
	imc_free(cptr);
	return data;
}

// 获取错误代码，通过code
RcResult GetErrorByCode(long nCode)
{
	if (nCode == 0)
	{
		return RcResult();
	}
	RcResult rc;
	char* pC = imc_GetErrorByCode(nCode);
	rc.rc = nCode;
	if (pC != NULL)
	{
		rc.result = pC;
	}
	imc_free(pC);
	return rc;
}


// 整形转字符串
std::string IntToString(__int64 nValue)
{
	char pBuff[400] = {0};
	sprintf(pBuff,  "%lld", nValue);
	return pBuff;
}


__int64 StringToInt(std::string pValue)
{
	__int64 nValue = ::atoi(pValue.c_str());
	return nValue;
}


// 16进制转10
unsigned char HexToDec(char cHightHex, char cLowHex)
{
	return imc_hexToDec(cHightHex, cLowHex);
}

// 10进制转16进制
void DecToHex(const unsigned char nDec, char& cHightHex, char& cLowHex)
{
	imc_decToHex(nDec, cHightHex, cLowHex);
}

// 获取许可授权级别
RcResult JngGetLicenseLevel(__int32& nLevel)
{
	long nCode = (long)imc_getLicenseLevel(nLevel);               
	return GetErrorByCode(nCode); 
}

// 获取允许运行个数
RcResult JngGetMaxRunCount(__int32& nCount)
{
	long nCode = (long)imc_getMaxRunCount(nCount);               
	return GetErrorByCode(nCode); 
}


// 获取允许运行数据点(变量、事件、警报分别计算)
RcResult JngGetMaxRunDataPoint(__int32& nCount)
{
	long nCode = (long)imc_getMaxRunDataPoint(nCount);               
	return GetErrorByCode(nCode); 
}



CSecsBase::CSecsBase()
{
	m_ih = imc_new();            
		
	// 回调
	imc_setClientData(m_ih, this);                
	imc_setConnectedProc(m_ih, OnConnectedCallback);
	imc_setDisconnectedProc(m_ih, OnDisconnectedCallback);
	imc_setTraceProc(m_ih, OnTraceLog);

}

CSecsBase::~CSecsBase()
{	
	imc_destroy(m_ih);
}


// 开始连接      
RcResult CSecsBase::Start(std::string pServerAddr, int nPort)
{	
//	CloseProcess("JNG_Server.exe");
	long nCode = (long)imc_start(m_ih, pServerAddr.c_str(), nPort);
	return GetErrorByCode(nCode);  
}

// 启用/禁用 SECS功能
void CSecsBase::SecsEnable(bool bEnable)
{
	imc_secsEnable(m_ih, bEnable);   
}

// 终止连接
void CSecsBase::Abort()
{	
	imc_stop(m_ih);          
}


// 获取句柄
IMCHandle CSecsBase::GetIMCHandle()
{
	return m_ih;
}



// 获取许可授权级别
RcResult CSecsBase::GetLicenseLevel(__int32& nLevel)
{
	long nCode = (long)imc_getLicenseLevel(nLevel);               
	return GetErrorByCode(nCode); 
}

// 获取允许运行个数
RcResult CSecsBase::GetMaxRunCount(__int32& nCount)
{
	long nCode = (long)imc_getMaxRunCount(nCount);               
	return GetErrorByCode(nCode); 
}


// 获取允许运行数据点(变量、事件、警报分别计算)
RcResult CSecsBase::GetMaxRunDataPoint(__int32& nCount)
{
	long nCode = (long)imc_getMaxRunDataPoint(nCount);               
	return GetErrorByCode(nCode); 
}



// 启用通信      
RcResult CSecsBase::CommEnable()
{
	long nCode = (long)imc_communicationEnable(m_ih);               
	return GetErrorByCode(nCode);  
}

// 关闭通信
RcResult CSecsBase::CommDisable()
{
	long nCode = (long)imc_communicationDisable(m_ih);
	return GetErrorByCode(nCode);
}

// 通讯是否开启
RcResult CSecsBase::CommIsEnable(bool& bEnable)		
{
	long nCode = (long)imc_communicationIsEnable(m_ih, bEnable);
	return GetErrorByCode(nCode); 
}

// 获取通信状态
RcResult CSecsBase::GetCommState(int& nState)
{
	int64 tmp = 0;
	long nCode = (long)imc_getCommunicationState(m_ih, tmp);
	nState = (int)tmp;
	return GetErrorByCode(nCode);
}

// 设置控制状态
RcResult CSecsBase::SetControlMode(int state)
{
	long nCode = (long)imc_setControlMode(m_ih, state);
	return GetErrorByCode(nCode);
}

// 设置控制状态
RcResult CSecsBase::SetControlModeForce(int state)
{
	long nCode = (long)imc_setControlModeForce(m_ih, state);
	return GetErrorByCode(nCode);
}

// 获取控制状态
RcResult CSecsBase::GetControlState(int& state)
{
	int64 nTmpState = 0;
	long nCode = (long)imc_getControlState(m_ih, nTmpState);
	state = (int)nTmpState;
	return GetErrorByCode(nCode);
}

// 主动请求在线，在线状态通过GetControlState设置
RcResult CSecsBase::ActiveRequestOnline()
{
	long nCode = (long)imc_activeRequestOnline(m_ih);	
	return GetErrorByCode(nCode);
}

// 这是静态回调函数
void CSecsBase::OnConnectedCallback(IMCHandle ic)
{
	CSecsBase* pObj;
	pObj = (CSecsBase*)imc_getClientData(ic);
	if(pObj != NULL)
	{
		pObj->OnConnected();
	}
}

// 这是静态回调函数
void CSecsBase::OnDisconnectedCallback(IMCHandle ic)
{
	CSecsBase* pObj;
	pObj = (CSecsBase*)imc_getClientData(ic);
	if(pObj != NULL)
	{
		pObj->OnDisconnected();
	}
}



// 设置超时
RcResult CSecsBase::SetTn(std::string Tn, int nTimeout)
{
	long nCode;
	nCode = (long)imc_setTn(m_ih, Tn.c_str(), nTimeout);
	return GetErrorByCode(nCode);                   
}


// 设置参数，单个
RcResult CSecsBase::SetParament(std::string type, std::string param)
{
	long nCode;	
	const char* pType[1]; 
	const char* pParam[1]; 
	pType[0] = type.c_str();
	pParam[0] = param.c_str();
	nCode = (long)imc_setParameter(m_ih, pType, pParam, 1);
	return GetErrorByCode(nCode);
}

// 设置参数，多个
RcResult CSecsBase::SetParament(const char* type[], const char* param[], int nCount)
{
	int i = 0;
	long nCode;	
	nCode = (long)imc_setParameter(m_ih, type, param, nCount);            
	return GetErrorByCode(nCode);                
}

// 报警列表获取
RcResult CSecsBase::AlarmGetList(std::string& pData)
{	
	char* pList = imc_alarmGetList(m_ih);
	if(pList != NULL)
	{
		pData = pList;
		imc_free(pList);
	}	
	return GetErrorByCode(0); 
}

// 添加报警
RcResult CSecsBase::AlarmAdd(int nALID, std::string pALTX, int nAlarmSetCEID, int nAlarmClearCEID, bool bEnable, int nCategory, bool bAddEvent)
{
	long nCode;
	nCode = (long)imc_alarmAdd(m_ih, nALID, pALTX.c_str(), nAlarmSetCEID, nAlarmClearCEID, bEnable, nCategory, bAddEvent);
	return GetErrorByCode(nCode);
}


// 添加报警
RcResult CSecsBase::AlarmAdd(int nALID, std::string pALTX)
{
	return AlarmAdd(nALID, pALTX.c_str(), nALID, nALID, true, eCategoryIrrecoverableError, true);
}

// 启用报警
RcResult CSecsBase::AlarmEnable(int nALID, bool bEnable)
{
	long nCode;
	nCode = (long)imc_alarmEnable(m_ih, nALID, bEnable);
	return GetErrorByCode(nCode);
}

// 设置报警
RcResult CSecsBase::AlarmSet(int nALID, bool bSet)
{
	long nCode;
	nCode = (long)imc_alarmSet(m_ih, nALID, bSet);
	return GetErrorByCode(nCode);
}

// 设置触发所有的报警
RcResult CSecsBase::AlarmSetAll(bool bSet)
{
	long nCode;
	nCode = (long)imc_alarmSetAll(m_ih, bSet);
	return GetErrorByCode(nCode);
}


// 警报发生后，事件通知方式
RcResult CSecsBase::AlarmNotifyEventMode(int nMode)
{
	long nCode;
	nCode = (long)imc_alarmNotifyEventMode(m_ih, nMode);
	return GetErrorByCode(nCode);
}

// 事件列表获取
RcResult CSecsBase::EventGetList(std::string& pData)
{	
	char* pList = imc_eventGetList(m_ih);
	if(pList != NULL)
	{
		pData = pList;
		imc_free(pList);
	}	
	return GetErrorByCode(0); 
}


// 事件发送: CEID 
// bWaitSendFinish：等待发送完毕
RcResult CSecsBase::EventPos(int nECID, bool bWaitReply)			
{
	long nCode = (long)imc_eventPos(m_ih, nECID, bWaitReply);
	return GetErrorByCode(nCode);	
}

// 事件发送: 事件名称 
// bWaitSendFinish：等待发送完毕
RcResult CSecsBase::EventPos(std::string EventName, bool bWaitReply)				
{
	long nCode = (long)imc_eventPosByName(m_ih, EventName.c_str(), bWaitReply);
	return GetErrorByCode(nCode);
}

// 增加事件列表	
RcResult CSecsBase::EventAdd(int nCEID, std::string pDescription, std::string pEventName, bool bEnable)	
{
	long nCode;
	nCode = (long)imc_eventAdd(m_ih, nCEID, pDescription.c_str(), pEventName.c_str(), bEnable);
	return GetErrorByCode(nCode);
}

// 事件启用
RcResult CSecsBase::EventEnable(int nCEID, bool bEnable)
{
	long nCode;
	nCode = (long)imc_eventEnable(m_ih, nCEID, bEnable);
	return GetErrorByCode(nCode);
}

// 事件是否启用
RcResult CSecsBase::EventIsEnable(int nCEID, bool& bEnable)
{
	long nCode;
	nCode = (long)imc_eventIsEnable(m_ih, nCEID, bEnable);
	return GetErrorByCode(nCode);
}

// 变量增加  
RcResult CSecsBase::VariableAdd(int varID, std::string varName, std::string description, 
	std::string valueType, std::string initValue, std::string units, std::string varClass)
{
	long nCode;
	nCode = (long)imc_variableAdd(m_ih, varID, varName.c_str(), description.c_str(), 
		valueType.c_str(), initValue.c_str(), units.c_str(), varClass.c_str());
	return GetErrorByCode(nCode);
}

// 事件DVVAL绑定
RcResult CSecsBase::EventDvvals(std::string eventName, std::string &dvvalNameList)
{
	long nCode;
	nCode = (long)imc_eventDvvalByName(m_ih, eventName.c_str(), dvvalNameList.c_str());
	return GetErrorByCode(nCode);
}

// 事件DVVAL绑定
RcResult CSecsBase::EventDvvals(int nECID, std::string& dvvalList)
{
	long nCode;
	nCode = (long)imc_eventDvval(m_ih, nECID, dvvalList.c_str());
	return GetErrorByCode(nCode);
}

// 事件S6,F3发送: CEID
RcResult CSecsBase::EventDiscreteVarPos(int nCEID, bool bWaitSendFinish)
{
	long nCode = (long)imc_eventDiscreteVarPos(m_ih, nCEID, bWaitSendFinish);
	return GetErrorByCode(nCode);	
}

// 事件-报表关联
RcResult CSecsBase::EventReportLink(int nECID, std::string& report)
{
	long nCode;
	nCode = (long)imc_eventReportLink(m_ih, nECID, report.c_str());
	return GetErrorByCode(nCode);
}

// 事件-报表取消关联
RcResult CSecsBase::EventReportUnlink(int nECID)
{
	long nCode;
	nCode = (long)imc_eventReportUnlink(m_ih, nECID);
	return GetErrorByCode(nCode);
}

// 报表定义
RcResult CSecsBase::ReportDefine(int rptID, std::string&  varID)
{
	long nCode;
	nCode = (long)imc_reportDefine(m_ih, rptID, varID.c_str());
	return GetErrorByCode(nCode);
}

// 获取报表定义
RcResult CSecsBase::ReportDefineGet(int rptID, std::string& varID)
{
	long nCode;
	char* pList = NULL;
	nCode = (long)imc_reportDefineGet(m_ih, rptID, &pList);
	
	if(pList != NULL)
	{
		if (nCode == 0)
		{
			varID = pList;
		}	
		imc_free(pList);
	}	
	return GetErrorByCode(nCode); 
}

// 删除报表
RcResult CSecsBase::ReportDelete(int rptID)
{
	long nCode;
	nCode = (long)imc_reportDelete(m_ih, rptID);
	return GetErrorByCode(nCode);
}

// 清除报表所有与之相关的事件
RcResult CSecsBase::ReportUnlink(int rptID)
{
	long nCode;
	nCode = (long)imc_reportUnlink(m_ih, rptID);
	return GetErrorByCode(nCode);
}

// 清除所有报告定义，并且清除所有事件与报告之间的并联
RcResult CSecsBase::ReportClear()
{
	long nCode;
	nCode = (long)imc_reportClear(m_ih);
	return GetErrorByCode(nCode);
}

// 报告严格按照标准规范处理：E5标准的处理及返回值
RcResult CSecsBase::ReportStrictlyFollowStandard(bool bFollow)
{
	long nCode;
	nCode = (long)imc_reportStrictlyFollowStandard(m_ih, bFollow);
	return GetErrorByCode(nCode);
}

// 报告保存服务器配置到本地
RcResult CSecsBase::ReportAutoSaveHostLinkConfig(bool bEnable)
{
	long nCode;
	nCode = (long)imc_reportAutoSaveHostLinkConfig(m_ih, bEnable);
	return GetErrorByCode(nCode);
}

// 设置变量新值  
RcResult CSecsBase::VariableSet(int varID, std::string newValue)
{
	int nCode;
	nCode = (long)imc_variableSet(m_ih, varID, newValue.c_str());
	return GetErrorByCode(nCode);
}

// 获取变量的值  
RcResult CSecsBase::VariableGet(int varID, std::string& pValue)
{
	pValue = "";	
	const char* point = imc_variableGet(m_ih, varID);
	if (point != NULL)
	{
		pValue = point;
		imc_free((void*)point);
	}
	return RcResult();
}

// 变量操作
RcResult CSecsBase::VariableGetList(std::string& pData)
{		
	char* pList = imc_variableGetList(m_ih);
	if(pList != NULL)
	{
		pData = pList;
		imc_free(pList);
	}	
	return GetErrorByCode(0); 
}


// 设置处理方法
RcResult CSecsBase::VariableSetMethod(int varID, bool enable)
{
	long nCode;
	nCode = (long)imc_variableSetMethod(m_ih, varID, enable);
	return GetErrorByCode(nCode);
}


/////////////////////////////////////////////////////////////////////
// 常量操作


// 添加常量  
RcResult CSecsBase::ConstantsAdd(int nECID, std::string name, std::string description, std::string Type,
	std::string Units, std::string MinValue, std::string MaxValue, std::string DefValue)
{
	long nCode;
	nCode = (long)imc_constantsAdd(m_ih, nECID, name.c_str(), description.c_str(), Type.c_str(), 
		Units.c_str(), MinValue.c_str(), MaxValue.c_str(), DefValue.c_str());
	return GetErrorByCode(nCode);
}

// 常量获取      
RcResult CSecsBase::ConstantsGet(int nECID, std::string& pValue)
{
	char* point = NULL;
	long nCode = (long)imc_constantsGet(m_ih, nECID, &point);
	if (point != NULL)
	{
		pValue = point;
		imc_free(point);
	}
	return GetErrorByCode(nCode); 
}

// 设置变量新值  
RcResult CSecsBase::ConstantsSet(int nECID, std::string newValue)
{
	long nCode;
	nCode = (long)imc_constantsSet(m_ih, nECID, newValue.c_str());
	return GetErrorByCode(nCode);
}

// 常量信息获取
RcResult CSecsBase::ConstantsInfoGet(std::string& pInfoList)
{
	long nCode;
	char* pBackValue = NULL;
	nCode = (long)imc_constantsInfoGet(m_ih, &pBackValue);
	if(pBackValue != NULL)
	{
		if (nCode == 0)
		{
			pInfoList = pBackValue;
		}		
		imc_free(pBackValue);
	}
	return GetErrorByCode(nCode);
}

// 设置处理方法
RcResult CSecsBase::ConstantsSetMethod(int nECID, bool enable)
{
	long nCode;
	nCode = (long)imc_constantsSetMethod(m_ih, nECID, enable);
	return GetErrorByCode(nCode);
}


// 启用/禁用常量改变发送事件         
RcResult CSecsBase::ConstantChangeSendEventEnable(bool bEnable)
{
	long nCode;
	nCode = (long)imc_constantChangeSendEventEnable(m_ih, bEnable);
	return GetErrorByCode(nCode);
}


// 主动请求主机时间进行同步 
RcResult CSecsBase::ActiveTimeSynchronizeRequest()
{
	long nCode;
	nCode = (long)imc_hostTimeSynchronizeRequest(m_ih);
	return GetErrorByCode(nCode);
}

// 添加处理
RcResult CSecsBase::WheneverAdd(const char* receiveMailbox, WhenProc* whenmsgProc, 
	void* clientData)
{
	long nCode;
	nCode = (long)imc_wheneverAdd(m_ih, receiveMailbox, whenmsgProc, clientData);
	return GetErrorByCode(nCode); 
}

// 移除处理
void CSecsBase::WheneverRemove(const char* receiveMailbox)
{
	imc_wheneverRemove(m_ih, receiveMailbox);
}

// 答复同步消息   
// when需要同步的消息
void CSecsBase::Reply(std::string receiveMailbox, std::string data)
{
	imc_relpy(m_ih, receiveMailbox.c_str(), data.c_str());                       
}

// 发送单一终端
RcResult CSecsBase::SendSingleTerminal(std::string pContent)
{
	long nCode;
	nCode = (long)imc_sendSingleTerminal(m_ih, pContent.c_str());
	return GetErrorByCode(nCode); 
}

// 发送多条终端
RcResult CSecsBase::SendMultipleTerminal(std::vector<std::string> svContent)
{
	const char** pList;
	pList = (const char**)imc_malloc(sizeof(const char**) * svContent.size());	
	if (pList == NULL)
	{
		return RcResult(10, "memory malloc fail");
	}
	for (std::vector<std::string>::size_type i = 0; i < svContent.size(); i++)
	{
		pList[i] = svContent[i].c_str();
	}
	long nCode = 0;// = (long)imc_sendMultipleTerminal(m_ih, pList, svContent.size());
	RcResult rc = GetErrorByCode(nCode); 
	imc_free(pList);
	return rc;
}



// 设置程式目录
void CSecsBase::SetRecipeDirectory(std::string dir)
{
	imc_setRecipeDirectory(m_ih, dir.c_str());           
}

// 设置程式目录
RcResult CSecsBase::ProcessProgramUpload(std::string ppid)
{
	int nCode;
	nCode = (long)imc_processProgramUpload(m_ih, ppid.c_str());
	return GetErrorByCode(nCode);
}

// 设置程式目录
RcResult CSecsBase::ProcessProgramDownload(std::string ppid)
{
	int nCode;
	nCode = (long)imc_processProgramDownload(m_ih, ppid.c_str());
	return GetErrorByCode(nCode);           
}

// 允许主机下载
RcResult CSecsBase::PPAllowHostDowmload(bool bAllow)
{
	int nCode;
	nCode = (long)imc_ppAllowHostDowmload(m_ih, bAllow);
	return GetErrorByCode(nCode);           
}

// PP处理模式
RcResult CSecsBase::PPHandleMode(int nMode)
{
	int nCode;
	nCode = (long)imc_ppHandleMode(m_ih, nMode);
	return GetErrorByCode(nCode);  
}
				

// 通过加载Csv文件加载数据
// 内置功能
RcResult CSecsBase::LoadDataByCsvFile(std::string pDir)
{
	int nCode;
	nCode = (long)imc_loadDataByCsvFile(m_ih, pDir.c_str());
	return GetErrorByCode(nCode);    
}

// 加载SECS配置
RcResult CSecsBase::LoadSecsConfig(std::string pDir)
{
	int nCode;
	nCode = (long)imc_loadSecsConfig(m_ih, pDir.c_str());
	return GetErrorByCode(nCode);    
}

// 设置PP是文件夹目录	
RcResult CSecsBase::PPIsFolderDir(bool bFolder)
{
	int nCode;
	nCode = (long)imc_ppIsFolderDir(m_ih, bFolder);
	return GetErrorByCode(nCode); 
}

// 设置winrar目录
RcResult CSecsBase::SetWinrarPath(std::string path)
{
	int nCode;
	nCode = (long)imc_setWinrarPath(m_ih, path.c_str());
	return GetErrorByCode(nCode);  
}

// 设置扩展名
RcResult CSecsBase::PPSetFilenameExtension(std::string pFileExten)
{
	int nCode;
	nCode = (long)imc_ppSetFilenameExtension(m_ih, pFileExten.c_str());
	return GetErrorByCode(nCode);  
}

// PP序列化参数对象添加 
RcResult CSecsBase::PPFormatAdd(std::string pCmdCode, std::string pName, std::string pValueType)
{
	int nCode;
	nCode = (long)imc_ppFormatAdd(m_ih, pCmdCode.c_str(), pName.c_str(), pValueType.c_str());
	return GetErrorByCode(nCode);  
}

// PP序列化上传
RcResult CSecsBase::PPFormatSend(std::string ppid)
{
	int nCode;
	nCode = (long)imc_ppFormatSend(m_ih, ppid.c_str());
	return GetErrorByCode(nCode);  
}

// PP序列化下载
RcResult CSecsBase::PPFormatRequest(std::string ppid)
{
	int nCode;
	nCode = (long)imc_ppFormatRequest(m_ih, ppid.c_str());
	return GetErrorByCode(nCode);  
}

// PP序列化设置模式
RcResult CSecsBase::PPFormatSetCodeMode(int nMode)
{
	int nCode;
	nCode = (long)imc_ppFormatSetCodeMode(m_ih, nMode);
	return GetErrorByCode(nCode);  
}

// wafer map
// 晶圆图上传
RcResult CSecsBase::WaferMapUpload(std::string pWaferMap)
{
	int nCode;
	nCode = (long)imc_waferMapUpload(m_ih, pWaferMap.c_str());
	return GetErrorByCode(nCode);  
}

// 晶圆图下传请求
RcResult CSecsBase::WaferMapDownload(std::string pWaferInfo, std::string& pWaferMap)
{
	int nCode;
	char* pWaferMapTmp = NULL;
	nCode = (long)imc_waferMapDownload(m_ih, pWaferInfo.c_str(), &pWaferMapTmp);
	if (pWaferMapTmp != NULL)
	{
		pWaferMap = pWaferMapTmp;
		imc_free(pWaferMapTmp);
	}
	return GetErrorByCode(nCode); 
}

// 条带图下传请求
RcResult CSecsBase::StripMapDownload(std::string pStripInfo, std::string& pStripMap)
{
	int nCode;
	char* pStripMapTmp = NULL;
	nCode = (long)imc_stripMapDownload(m_ih, pStripInfo.c_str(), &pStripMapTmp);
	if (pStripMapTmp != NULL)
	{
		pStripMap = pStripMapTmp;
		imc_free(pStripMapTmp);
	}
	return GetErrorByCode(nCode); 
}

RcResult CSecsBase::SendSecsMsgAsync(int64 nStream, int64 nFunction, 
	bool bReply, std::string pData, int64& nMsgID)
{
	int nCode = 0;
	imc_sendSecsMsgAsync(m_ih, nStream, nFunction, bReply, nMsgID, pData.c_str());
	return GetErrorByCode(nCode);
}


RcResult CSecsBase::SendSecsMsgSync(int nStream, int nFunction, bool bReply, 
	std::string pData, std::string& pReplyData)
{
	int nCode = 0;
	char* pReplyDataPoint = NULL;
	nCode = (long)imc_sendSecsMsg(m_ih, nStream, nFunction, bReply, pData.c_str(),
		&pReplyDataPoint, true);

	if (nCode == 0 &&
		pReplyDataPoint != NULL)
	{		
		pReplyData = pReplyDataPoint;
		      
	}
	if (pReplyDataPoint != NULL)
	{
		imc_free(pReplyDataPoint);
	}
	return GetErrorByCode(nCode);  
}

RcResult CSecsBase::SendReply(int nStream, int nFunction, int nTransactionID, std::string pData)
{
	imc_sendReply(m_ih, nStream, nFunction, nTransactionID, pData.c_str());
	return RcResult();
}

// 添加处理对象
RcResult CSecsBase::MessageTypeAdd(int nStream, int nFunction, 
	SecsMessageReceiveProc pCallback, void* pClientData)
{
	// 添加处理对象操作
	int nCode = (long)imc_messageTypeAdd(m_ih, nStream, nFunction, pCallback, pClientData);
	return GetErrorByCode(nCode);
}      

// 删除处理对象
 RcResult CSecsBase::MessageTypeRemove(int nStream, int nFunction)
{	
	// 删除服务器的
	imc_messageTypeRemote(m_ih, nStream, nFunction);
	return GetErrorByCode(0);
}        

// 启用运行过程中的LogTrace,追查BUG等
void CSecsBase::EnableRuntimeTrace(bool bEnable)	
{
	imc_enableTrace(m_ih, bEnable);
}

void CSecsBase::CloseProcess(std::string pName)
{
	// QT删掉此函数内容
	// QT开始删除

	PROCESSENTRY32 pe = { 0 }; 
	pe.dwSize = sizeof(PROCESSENTRY32 );

	HANDLE hProcess = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//拍摄快照
	BOOL bRet = ::Process32First(hProcess, &pe);	//检索快照中第一个进程信息
	while(bRet)
	{
		//判断不是最后一个进程，历遍所有
		if(pName == pe.szExeFile)
		{
			//打开进程并杀死
			HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE,pe.th32ProcessID);
			if (pHandle != NULL)
			{
				TerminateProcess(pHandle, 0);
			}

		}
		bRet = Process32Next(hProcess,&pe);//下一个进程
	}
	// QT删除到此
}

// 追溯log
void CSecsBase::OnTraceLog(IMCHandle imc, int64 nCode, const char* pText)
{
	CSecsBase* pObj;
	pObj = (CSecsBase*)imc_getClientData(imc);
	if(pObj != NULL)
	{
		pObj->OnTraceLog(nCode, pText);
	}
}


void CSecsBase::OnTraceLog(int64 nCode, const char* pText)
{

}




// 转字符串
std::string ToString(__int64 nValue)
{
	return IntToString(nValue);
}

std::string ToString(__int32 nValue)
{
	return IntToString(nValue);
}

std::string ToString(__int16 nValue)
{
	return IntToString(nValue);
}

std::string ToString(__int8  nValue)
{
	return IntToString(nValue);
}
std::string ToString(unsigned __int64 nValue)
{
	char pBuff[400] = {0};
	sprintf(pBuff, "%lld", nValue);
	return pBuff;
}

std::string ToString(unsigned __int32 nValue)
{
	return IntToString((unsigned __int64)nValue);
}

std::string ToString(unsigned __int16 nValue)
{
	return IntToString((unsigned __int64)nValue);
}

std::string ToString(unsigned __int8  nValue)
{
	return IntToString((unsigned __int64)nValue);
}

std::string ToString(float nValue)
{
	char pBuff[400] = {0};
	sprintf(pBuff, "%f", nValue);
	return pBuff;
}

std::string ToString(double nValue)
{
	char pBuff[400] = {0};
	sprintf(pBuff, "%f", nValue);
	return pBuff;
}










