/*=================================================================
* Function ID :  SMT_EveryDayGetAllInitAssistantThread
* Input       :  void
* Output      :  void
* Author      :  
* Date        :  2009  8
* Return	  :  void
* Description :  主线程(定时获取黑名单)12小时
* Notice	  :  
*
*=================================================================*/
UINT SMT_EveryDayGetAllInitAssistantThread( LPVOID pParam )
{
	DWORD		nDStartTime=GetTickCount();
	int         FunId;
	int         updata1=0;
	int         updata2=0;
	time_t      rawtime;
	struct tm * timeinfo;
	try
	{        
		while( g_StartFlag )
		{   
			           
            time ( &rawtime );    
            timeinfo = localtime ( &rawtime ); 

      //执行设置黑名单更新时间1
		   if  ((timeinfo->tm_hour  == g_IniFilePara.DownBlacktime1) && (updata1!=timeinfo->tm_mday))
		   {
			   FunId=SMT_SMARTFRONTDOWNALLBLACK_EVERYDAY;
                 	g_CInterface.SMT_GetAllInitBlackList(FunId);					
					updata1=timeinfo->tm_mday;
		   }
        //执行设置黑名单更新时间2
		    if  ((timeinfo->tm_hour  == g_IniFilePara.DownBlacktime2) && (updata2!=timeinfo->tm_mday))
		    	{
					FunId=SMT_SMARTFRONTDOWNALLBLACK_EVERYDAY;
                 	g_CInterface.SMT_GetAllInitBlackList(FunId);					
					updata2=timeinfo->tm_mday;
				}
		}
	
	}
	catch(...)
	{
		((CSmartCommunicationDlg*)pParam)->WriteLog("调用金仕达接口时出现异常,前置自动停止运行!!");
		MessageBox(NULL,"调用金仕达接口时出现异常,前置自动停止运行!!","严重错误",MB_OK);
		((CSmartCommunicationDlg*)pParam)->OnButtonStop();
	}
	return 0;
}
void CSmartCommunicationDlg::OnButtonStart() 
{		
	char		buf[128],Module[30],msg[256];	
	CData		mdata;	
	int			nErrCode=0;	
	int         FunId;
	CSystemInfo	SystemInfo;
	list<CSocketSmart>::iterator SocketSmart;

	memset(buf,   0x00,sizeof buf);
	memset(msg,	  0x00,sizeof msg);
	memset(Module,0x00,sizeof Module);

	g_CInterface.SetParentpDlg(this);
	//清除显示框中的信息
	OnButtonClear();
	Init();
	m_BtCancel.EnableWindow(FALSE);
	m_BtStart.EnableWindow(FALSE);
	m_BtClear.EnableWindow(TRUE);

	strcpy(msg,"前置系统开始启动并监控所有终端设备,请稍后......");
	AddString(msg);
	WriteLog(msg);
	//初始化第三方接口		
	strcpy(msg,"前置系统正在初始化第三方接口,请稍后......");
	AddString(msg);
	WriteLog(msg);
	if( !g_bInitConnectFlag )
	{	
		if( !g_CInterface.SMT_InterfaceInit(m_ServerIP,m_ServerPort) )
		{
			AddString("前置系统正在初始化第三方接口失败,请退出前置重新启动");
			strcpy(m_ErrMsg,g_CInterface.m_szText);
			goto To_SystemEnd;
		}
		g_bInitConnectFlag=true;
	}
	//前置签到	
	strcpy(msg,"前置系统正在向金仕达签到,请稍后......");
	AddString(msg);
	WriteLog(msg);
	if( !g_CInterface.SMT_SmartFrontLogin(5000) )
	{
		sprintf(m_ErrMsg,"前置向金仕达签到失败[金仕达返回:%s]",g_CInterface.m_szText);
		goto To_SystemEnd;
	}

	//获取黑名单
	strcpy(msg,"前置系统正在装载黑名单,请稍后......");
	AddString(msg);
	WriteLog(msg);	
	FunId=SMT_SMARTFRONTDOWNALLBLACK;
	if( !g_CInterface.SMT_GetAllInitBlackList(FunId) )
	{
		strcpy(m_ErrMsg,g_CInterface.m_szText);
		goto To_SystemEnd;
	}
	

	//获取补助名单
	strcpy(msg,"前置系统正在装载补助名单,请稍后......");
	AddString(msg);
	WriteLog(msg);	
	if( !g_CInterface.SMT_GetAllInitAssistantList(false,0) )
	{
		strcpy(m_ErrMsg,g_CInterface.m_szText);
		goto To_SystemEnd;
	}

	//获取设备的主参数	
	strcpy(msg,"前置系统正在获取设备的主参数,请稍后......");
	AddString(msg);
	WriteLog(msg);
	if( !GetDevicePara() ) 
	{
		strcpy(m_ErrMsg,g_CInterface.m_szText);
		goto To_SystemEnd;
	}
	//显示启动的服务总数
	sprintf(m_ErrMsg,"前置系统总共需要启动%ld个链路服务.",g_LTcpsmart.size());
	AddString(m_ErrMsg);	
	WriteLog(m_ErrMsg);	
	//启动所有通过TCP连接的服务	
	for( SocketSmart=g_LTcpsmart.begin();SocketSmart!=g_LTcpsmart.end();SocketSmart++)
	{
		SocketSmart->SetParent(this);
		SocketSmart->Start();
		sprintf(m_ErrMsg,"开始准备启动 %s 的线程......",SocketSmart->m_IpPar.cIPAddress);
		AddString(m_ErrMsg);	
		WriteLog(m_ErrMsg);
	}
	g_StartFlag = 1;
	//取任务线程
	UINT SMT_StartQueryTaskThread( LPVOID pParam );
	if(AfxBeginThread(SMT_StartQueryTaskThread, (LPVOID)this)==NULL)
	{			
		strcpy(m_ErrMsg,"创建从数据库中取任务的线程失败");	
		goto To_SystemEnd;
	}
	//上传交易记录线程
	UINT SMT_StartUpRecordThread( LPVOID pParam );
	if(AfxBeginThread(SMT_StartUpRecordThread, (LPVOID)this)==NULL)
	{			
		strcpy(m_ErrMsg,"创建从数据库中取任务的线程失败");	
		goto To_SystemEnd;
	}
 	//定时取黑名单线程,每12小时取一次数据
	UINT SMT_EveryDayGetAllInitAssistantThread( LPVOID pParam );
	if(AfxBeginThread(SMT_EveryDayGetAllInitAssistantThread, (LPVOID)this)==NULL)
	{			
		strcpy(m_ErrMsg,"创建从数据库中定时取黑名单的线程失败");	
		goto To_SystemEnd;
	}	
	m_BtRate.EnableWindow(TRUE);
	m_BtCancel.EnableWindow(FALSE);
	m_BtStart.EnableWindow(FALSE);
	return ;	
To_SystemEnd:
	AddString(m_ErrMsg);
	WriteLog(m_ErrMsg);
	OnButtonStop();
	m_BtCancel.EnableWindow(TRUE);
	m_BtStart.EnableWindow(TRUE);
	m_BtClear.EnableWindow(TRUE);		
	m_BtRate.EnableWindow(FALSE);
}