Beispiel #1
0
//*=================================================================================
//*原型: void TSmartTaskObj::ReportFinished()
//*功能: 写任务结束日志
//*参数: 略
//*返回: 无
//*说明: 任务管理类
//*=================================================================================
void TSmartTaskObj::ReportFinished()
{
	char   strText[28192];
	char   strString[1024];
	char   szTime[24];
	long   nOK = 0, nFaild = 0 ;
	char   pszResult[256];

	GetCurDateTime(szTime);

	sprintf(strText,"******任务计划(%d):%s 开始时间:%s****结束时间:%d***设备数:%d****成功数:%d****\n", 
		nTaskPlanID, szTaskName, m_szDateTime, szTime, nTask, nTask);

	for(int i=0; i< nTask; i++)
	{
		GetResultMessage(pTask[i].nResult, pszResult);

		sprintf(strString, "设备(%d) ID号:%s 机型:%s 任务代码:%s 开始时间:%s  结束时间:%s  运行时长:%d  结果(%d):%s\n",
			pTask[i].nAuthID, "", "", pTask[i].szTaskCode, 
			m_szDateTime, pTask[i].szRunTime, 
			pTask[i].nRepeat2, pTask[i].nResult, pszResult);
		strcat(strText,strString);
	}

	strcat(strText,"************************************************************************************\n\n");
	WriteTaskLog(strText);
}
Beispiel #2
0
//签到时处理请求
long CSingInServer::SignInProcess(TSSmartDocEx *pDocEx, TSCmdBuffer *pBuffer, long nPort, TSSmartTask *pTask)
{
	long  nAuthID = 0 ;
	int   n = pDocEx->nPortNo ;

	TSDeviceInfo  DevInfo;
	ZeroMemory(&DevInfo, sizeof(DevInfo));


	if( pDocEx->nChildDoc > 1 )
	{
		for(int i=1; i< pDocEx->nChildDoc; i++)
		{

			if( pDocEx->pChildDoc[i]->m_nParentID > 0 &&
				pDocEx->pChildDoc[i]->m_nSMTPort == nPort &&
				(pDocEx->pChildDoc[i]->m_nStatus == 0 || pDocEx->pChildDoc[i]->m_nStatus == 1) )
			{
				n = i ;
        		memcpy(&DevInfo, &pDocEx->pChildDoc[i]->DeviceInfo, sizeof(DevInfo));
				break;

			}
		}
	}
	else
	{
		memcpy(&DevInfo, &pDocEx->pChildDoc[n]->DeviceInfo, sizeof(DevInfo));
		DevInfo.nLevel = 0 ;
		DevInfo.sLevelArray[1] = (unsigned char)nPort;
	}

	CCheckBlackVersion  Check;
	int nTemp = pDocEx->nPortNo ;
	pDocEx->nPortNo = n;
	if( Check.SignInDown(pDocEx->pChildDoc[n], pBuffer) != RET_OK )
	{
		pDocEx->nPortNo  = nTemp ;
		pDocEx->pChildDoc[n]->m_nSignIn = RET_BLACKLIST_ERROR ;
		return RET_BLACKLIST_ERROR;
	}
	
	if( ProcessData(pDocEx, pTask, pBuffer) != RET_OK )
	{
		//printf("签到不成功!\n");
		pDocEx->nPortNo  = nTemp ;
		pDocEx->pChildDoc[n]->m_nSignIn = RET_BLACKLIST_ERROR ;
		return RET_BLACKLIST_ERROR;
	}

	if( SignIn && pDocEx->pChildDoc[n]->m_nSignIn != 1 )
	{
		SignIn(pDocEx->pChildDoc[n]);
	}

	GetCurDateTime(pDocEx->pChildDoc[n]->m_szSignInTime);
	pDocEx->pChildDoc[n]->m_nSignIn = 1 ;
	pDocEx->nPortNo  = nTemp ;
	return RET_OK;
}
Beispiel #3
0
void
WriteLog (char *p_logfile, char *p_format, ...)
{
  va_list str;
  char old_logfile[256];
  char cur_time[25];
  FILE *logfile_fd = NULL;
  struct _stat stat_buf;

#define _MAX_LOGFILE_SIZE_	102400

  if (p_logfile != NULL)
    {
      if ((_stat (p_logfile, &stat_buf) == 0) &&
	  (stat_buf.st_size >= _MAX_LOGFILE_SIZE_))
	{
	  strcpy_s (old_logfile, p_logfile);
	  strcat_s (old_logfile, ".bak");

	  remove (old_logfile);

	  if (rename (p_logfile, old_logfile) != 0)
	    {
	      fprintf (stderr, "WriteLog:rename error\n");
	      return;
	    }
	}

      fopen_s (&logfile_fd, p_logfile, "a+");

      if (logfile_fd == NULL)
	{
	  fprintf (stderr, "WriteLog:Can't open logfile [%s][%d]\n",
		   p_logfile, errno);
	  return;
	}
    }
  else
    {
      logfile_fd = stderr;
    }

#ifndef __DEBUG
  GetCurDateTime (cur_time, "%Y%m%d %H:%M:%S");
  fprintf (logfile_fd, "[%s] ", cur_time);
#endif

  va_start (str, p_format);
  vfprintf (logfile_fd, p_format, str);
  va_end (str);

  if (p_logfile != NULL)
    {
      fclose (logfile_fd);
    }
}
//*=============================================================================================
//*原型: void ReportLog(TSSmartDocEx *pDocEx, const char *format, ...)
//*功能: 日志信息保存
//*参数: 
//*      pDoc  -- 设备信息
//*      pTask -- 设备任务
//*      
//*返回: 
//*      无
//*=============================================================================================
void ReportLog(TSSmartDoc *pDoc, TSSmartTask *pTask, long nRet, char *pszTask, char *pszString)
{
	TCHAR	szFileName[MAX_PATH],szTemp[MAX_PATH];
	SYSTEMTIME  SysTime;
	TCHAR   szLog[4096];
	TCHAR   szText[1024];

	GetLocalTime(&SysTime);

	if( nRet == RET_OK )
	{
		wsprintf(szLog, "%02d:%02d:%02d [授权号<%d> 服务器<%s> 端口号<%s> 任务计划<%d> 任务<%s>] [执行结果(%d):成功!] %s\n", 
			SysTime.wHour, SysTime.wMinute, SysTime.wSecond,
			pDoc==NULL?0:pDoc->m_nAuthID, pDoc==NULL?"":pDoc->m_szAddr, pDoc==NULL?"":pDoc->m_szPort, 
			pTask==NULL?0:pTask->nTaskPlanID, 
			pszTask, nRet, pszString);
	}
	else
	{
		TCHAR  szTemp[256];

		memset(szTemp, 0, sizeof(szTemp));
		GetResultMessage(nRet, szTemp);

		wsprintf(szLog, "%02d:%02d:%02d [授权号<%d> 服务器<%s> 端口号<%s> 任务计划<%d> 任务<%s>] [执行结果(%d):失败! 出错信息:%s] %s\n", 
			SysTime.wHour, SysTime.wMinute, SysTime.wSecond,
			pDoc==NULL?0:pDoc->m_nAuthID, pDoc==NULL?"":pDoc->m_szAddr, 
			pDoc==NULL?"":pDoc->m_szPort, pTask==NULL?0:pTask->nTaskPlanID, 
			pszTask, nRet, szTemp, pszString);

		if( pDoc && pTask )
		{
			char szTmp[32];
			GetCurDateTime(szTmp);

			wsprintf(szText, "%d&%s&%s&%d&%s", pDoc->m_nAuthID, pTask->szTaskCode,
				szTmp, nRet, pszString);
			//BroadcastPro(PACKET_CMD_1003, pDoc, szText, lstrlen(szText));
		}
	}
	wsprintf(szTemp,"%s\\%04d%02d%2d",szErrorLogPath,SysTime.wYear,SysTime.wMonth,
		SysTime.wDay);
	CreateDirectory(szTemp,NULL);

	wsprintf(szFileName, "%s\\%s.log", 
		szTemp, pDoc==NULL?"all":pDoc->m_szDeviceID);

	FILE *fp = NULL ;
	if( (fp=fopen(szFileName, "a+")) != NULL )
	{
		fwrite(szLog, lstrlen(szLog), sizeof(TCHAR), fp);
		fclose(fp);
	}
}
long __stdcall ReReadSmartTaskPlan(TSSmartTaskPlan *pTaskPlan)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
	pTaskPlan->nTaskPlanID = GetTickCount()-5;	
	sprintf(pTaskPlan->szTaskName,"功能号:%d", 100); //任务计划名称
	pTaskPlan->nTaskCycle=0;                  //任务计划执行周期
	GetCurDateTime(pTaskPlan->szBeginTime);   //开始日期时间
	pTaskPlan->nRepeatTime = 1 ;              //持续多长时间(分钟)
	pTaskPlan->nRepeatTimes = 1;             //重复次数
	GetCurDateTime(pTaskPlan->szTaskTime);    //任务计划的时间
	pTaskPlan->nTask = 0 ;

	int p=0;
	for(int i=0; i< gnDocCount; i++)
	{
		if( !gpSmartDoc[i].m_nParentID )
		{
			pTaskPlan->pTask[p].nTaskID = GetTickCount();
			pTaskPlan->pTask[p].nTaskPlanID=pTaskPlan->nTaskPlanID; 
			pTaskPlan->pTask[p].nAuthID = gpSmartDoc[i].m_nAuthID ;
			strcpy(pTaskPlan->pTask[p].szDeviceID, gpSmartDoc[i].m_szDeviceID);
			strcpy(pTaskPlan->pTask[p].szTaskCode,"70");
			pTaskPlan->pTask[p].nPriority = 9;
			pTaskPlan->pTask[p].nFunc = 930005;
				
		    //if( pPacket->pack.lvol4 ) 
			sprintf(pTaskPlan->pTask[p].szMemo, "%d", 1);
			//	else
			//		sprintf(pTaskPlan->pTask[p].szMemo, "%d", pPacket->pack.lvol0);
				p++;
				pTaskPlan->nTask++;
			//	WriteLog("!!!广播黑名单....设备:%s.....卡号:%d....\n", gpSmartDoc[i].m_szDeviceID, pPacket->pack.lvol0);
		}
	//wenjian comment below line
	//	Sleep(3);
	}

	//if( pTaskPlan->nTask > 0 )
	//	iCount = 1 ;
	return RET_OK;
}
Beispiel #6
0
//*=================================================================================
//*原型: void TSmartTaskObj::SetFinished(long nIndex)
//*功能: 设置子任务结束
//*参数: 略
//*返回: 无
//*说明: 任务管理类
//*=================================================================================
void TSmartTaskObj::SetFinished(long nIndex)
{
	if( nIndex < 0 || nIndex >= nTask )
		return ;

	GetCurDateTime(pTask[nIndex].szRunTime);
	strcpy(pTask[nIndex].szSystemRCode, "");

	pTask[nIndex].nResult = 0 ;
	pTask[nIndex].nRepeat1++;
	pTask[nIndex].nRepeat2 = (GetTickCount()-pTask[nIndex].nTick)/(1000*60);
}
//*========================================================================================
//*函数: void CSmartTask::SetTask(char *sName, char *sBeginTime, long nTime, long nTimes)
//*功能: 设置新任务属性
//*参数: 略
//*返回: 无
//*说明: 任务表操作类
//*========================================================================================
void CSmartTask::SetTask(char *sName, char *sBeginTime, long nTime, long nTimes)
{
	m_SmartTask.nRepeatTime = nTime;				//持继时间
	m_SmartTask.nRepeatTimes = nTimes;				//持继次数
	m_SmartTask.nTask = 0 ;							//复原设备数
	m_SmartTask.nTaskCycle = TASK_CYCLE_ONCE;		//固定为 '一次性任务'
	strcpy(m_SmartTask.szTaskName, sName);			//任务名称

	if( !strcmp(sBeginTime, "") )
		GetCurDateTime(m_SmartTask.szBeginTime);
	else
		strcpy(m_SmartTask.szBeginTime, sBeginTime);	//任务起始时间
}
Beispiel #8
0
//*=================================================================================
//*原型: void TSmartTaskObj::SetTaskResult(long nIndex, long nRet)
//*功能: 设置单个任务的返回结果
//*参数: 略
//*返回: 无
//*说明: 任务管理类
//*=================================================================================
void TSmartTaskObj::SetTaskResult(long nIndex, long nRet, long nTerminate)
{
	if( nIndex < 0 || nIndex >= nTask )
		return ;

	strcpy(pTask[nIndex].szSystemRCode, "");
	pTask[nIndex].nResult = nRet ;

	GetCurDateTime(pTask[nIndex].szRunTime);

	strcpy(pTask[nIndex].szSystemRCode, "");
	pTask[nIndex].nRepeat1++;
	pTask[nIndex].nRepeat2 = (GetTickCount()-pTask[nIndex].nTick)/(1000*60);
	pTask[nIndex].nTerminate = nTerminate ;
}
Beispiel #9
0
void BeginUploadFile()
{
	int	i = 0;
	int	iLen = 0;
	
	int iCurDateTime[6];

	BYTE	szTempBuf[100] = {0};
	int  iTempLen = 0;

	// 若当前正在监控,则返回
	if(	g_objWorkStart.VUpload[0] || g_objWorkStart.VUpload[1] || g_objWorkStart.VUpload[2] || g_objWorkStart.VUpload[3] )
		return;

	// 更新上传文件的状态
	if( FALSE == UpdateUploadSta(FALSE) ) 
		return;

	// 组帧(C1帧)
	szTempBuf[iTempLen++] = 0xC1;

	// 手机号
	tag1PComuCfg objComuCfg;
	GetImpCfg( &objComuCfg, sizeof(objComuCfg), offsetof(tagImportantCfg, m_uni1PComuCfg.m_obj1PComuCfg), sizeof(objComuCfg) );					
	memcpy(szTempBuf+iTempLen, objComuCfg.m_szTel, 15);
	iTempLen += 15;
	
	//文件类型
	szTempBuf[iTempLen++] = g_objUploadFileSta.m_uszFileType;		
	
	//此处黑匣子文件名固定长为34个字节,后面补空格,凑齐40个字节
	iLen = strlen(g_objUploadFileSta.m_szTempFileName[0]);
	memcpy( szTempBuf+iTempLen, g_objUploadFileSta.m_szTempFileName[0], iLen);	
	iTempLen += iLen;
	szTempBuf[iTempLen++] = ' ';szTempBuf[iTempLen++] = ' ';szTempBuf[iTempLen++] = ' ';	
	szTempBuf[iTempLen++] = ' ';szTempBuf[iTempLen++] = ' ';szTempBuf[iTempLen++] = ' ';
	
	// 时间序号
	GetCurDateTime( iCurDateTime );
	
	szTempBuf[iTempLen++] = (char)(iCurDateTime[0]-2000);
	szTempBuf[iTempLen++] = (char)iCurDateTime[1];
	szTempBuf[iTempLen++] = (char)iCurDateTime[2];
	szTempBuf[iTempLen++] = (char)iCurDateTime[3];
	szTempBuf[iTempLen++] = (char)iCurDateTime[4];
	szTempBuf[iTempLen++] = (char)iCurDateTime[5];
	
	// 总包数
	memcpy(szTempBuf+iTempLen, (void*)&g_objUploadFileSta.m_ulPackTotal, 8);
	iTempLen += 8;
		
	// 推到SockServExe发送
	DataPush(szTempBuf, (DWORD)iTempLen, DEV_DVR, DEV_SOCK, LV2);

	PRTMSG(MSG_DBG, "Upload C1 frame\n");
	//PrintString((char*)szTempBuf, iTempLen);

	// 保存到缓冲,以备重传
	memcpy(g_szReUploadBuf, szTempBuf, iTempLen);
	g_iReUploadLen = iTempLen;

 	// 启动超时重传定时器
	_SetTimer(&g_objTimerMng, C1C3_REUPLOAD_TIMER, 5000, Deal_ReUpload);
}
Beispiel #10
0
void TSmartTaskObj::SetBeginTime()
{
	GetCurDateTime(m_szDateTime);
}
Beispiel #11
0
long CSingInServer::ProcessData(TSSmartDocEx *pDocEx, TSSmartTask *pTask, TSCmdBuffer *pBuffer)
{
	bool          bResult = false;
	CTime		  tm;
	int			  len=0,i=0;
	int			  E_number=0;
	unsigned char Clock[16];
	unsigned char Plain[256],ID[16];
	char		  ErrMsg[256];

	
	memset(ID,    0x00,sizeof ID);
	memset(Clock, 0x00,sizeof Clock);
	memset(Plain, 0x00,sizeof Plain);	
	memset(ErrMsg,0x00,sizeof ErrMsg);
	
	sprintf(ErrMsg,"处理终端%.8X签到任务\n",pDocEx->pChildDoc[pDocEx->nPortNo]->m_nAuthID);
	printf(ErrMsg);
	WriteLog(ErrMsg);
	m_nIndex = pDocEx->nPortNo;

	memset(ErrMsg,0x00,sizeof ErrMsg);
	//由于需要金仕达确认是否可以签到,故将签到任务提前到前面
	int ret = 0;
	E_number = 1;	
	for(i=0; i<3; i++)
	{
		tm=CTime::GetCurrentTime();
		Clock[0] = tm.GetYear()-2000;
		Clock[1] = tm.GetMonth();
		Clock[2] = tm.GetDay();
		Clock[3] = tm.GetHour();
		Clock[4] = tm.GetMinute();
		Clock[5] = tm.GetSecond();
		Clock[6] = tm.GetDayOfWeek();

		MakeBuf_SetClock(&pDocEx->pChildDoc[pDocEx->nPortNo]->DeviceInfo, pBuffer,Clock);
		pBuffer->nTimeOut1 = 1000;
		pBuffer->nTimeOut2 = 1000;
		//Modified by hhd at 2006-01-20,added return code
		ret=SendRequest(pDocEx->pChildDoc[m_nIndex], pBuffer);
		if(ret==RET_OK)
		{
			ret=UnMakeDeviceBuffer(pBuffer);
			if(ret==RET_OK)
			{
				sprintf(ErrMsg,"下传终端%.8X时钟成功!\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID);
				printf(ErrMsg);
				WriteLog(ErrMsg);
				bResult = true;
				break;
			}
			else
			{
				sprintf(ErrMsg,"-----!!!!校验%.8X返回包失败-时钟!错误码=[%d]\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID,ret);
				printf(ErrMsg);	
				Sleep(1000);
			}
		}
		else
		{
			sprintf(ErrMsg,"-----!!!!下传终端%.8X时钟失败!错误码=[%d]\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID,ret);
			printf(ErrMsg);			
			Sleep(1000);
		}
	}

	if( !bResult )
	{
		printf("-----!!!!下传终端%.8X时钟失败!\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID);
		ReportLog(pDocEx->pChildDoc[m_nIndex], pTask, RET_CLOCK_ERROR, "终端机签到", "终端机签到 ---校对时钟失败!");
		pDocEx->pChildDoc[pDocEx->nPortNo]->m_nSignIn = RET_CLOCK_ERROR ;
		return RET_CLOCK_ERROR;
	}
	
	char szKeyBuf[256];
	memset(Plain,    0x00, sizeof(Plain));
	memset(szKeyBuf, 0x00, sizeof(szKeyBuf));
	GetAuthKey(szKeyBuf);
	if( !strlen(szKeyBuf) ) //向金仕达取授权密钥失败 add by dengjun 20060616
	{
		strcpy(ErrMsg,"向金仕达取授权密钥失败");
		printf("%s\n",ErrMsg);
		WriteLog(ErrMsg);
	}	
	len = strlen(szKeyBuf)/2;
	SVC_DSP_2_HEX((BYTE*)szKeyBuf, Plain, len);
	//设置授权信息
	bResult = false;
	Sleep(5000); //Modified by dengjun 20060616 update Sleep(250) to Sleep(5000)
	memset(ErrMsg,0x00,sizeof ErrMsg);
	//Modified by hhd at 2006-01-20,added return code
	for(i=0; i<3; i++)
	{
		SVC_DSP_2_HEX((unsigned char *)pDocEx->pChildDoc[pDocEx->nPortNo]->m_szDeviceID, ID, 4);
		//modified by lina 20050308 SmartEncrypt(E_number,ID,Plain);
		MakeBuf_SetWorkingKey(&pDocEx->pChildDoc[pDocEx->nPortNo]->DeviceInfo,pBuffer,Plain,E_number);
		pBuffer->nTimeOut1 = 1000 ;
		pBuffer->nTimeOut2 = 1000;
		ret=SendRequest(pDocEx->pChildDoc[pDocEx->nPortNo], pBuffer);
		if(ret==RET_OK)
		{
			ret=UnMakeDeviceBuffer(pBuffer);
			if(ret==RET_OK)
			{
				sprintf(ErrMsg,"-----设置终端%.8X授权信息成功!\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID);
				printf(ErrMsg);
				WriteLog(ErrMsg);
				bResult = true;
				break;
			}
			else
			{
				sprintf(ErrMsg,"-----!!!!校验%.8X返回包失败-授权!错误码=[%d]\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID,ret);
				printf(ErrMsg);					
				Sleep(2500);
			}
		}
		Sleep(1000); 
	}
	
	if( !bResult )
	{
		printf("-----!!!!设置终端%.8X授权信息失败!\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID);
		ReportLog(pDocEx->pChildDoc[m_nIndex], pTask, RET_NOT_AUTH, "终端机签到", "终端机签到 ---设置授权信息失败!");
		pDocEx->pChildDoc[pDocEx->nPortNo]->m_nSignIn = RET_NOT_AUTH ;
		return RET_NOT_AUTH;
	}	
	if((ret=SignIn(pDocEx->pChildDoc[pDocEx->nPortNo])) != RET_OK)
	{
		sprintf(ErrMsg,"后台不允许签到[ErrCode=%ld]!!!!!!!!!!!!!!!\n",ret);
		printf(ErrMsg);
		WriteLog(ErrMsg);		
		return  RET_ERROR_AUTH;//25:后台不允许签到;		
	}
	
	Sleep(3000);
	//设置消费开关
	bResult = false;
	for(i=0; i<3; i++)
	{
		MakeBuf_SetComsumeFlag(&pDocEx->pChildDoc[pDocEx->nPortNo]->DeviceInfo, pBuffer, 1);
		pBuffer->nTimeOut1 = 1000 ;
		pBuffer->nTimeOut2 = 1000;
		ret=SendRequest(pDocEx->pChildDoc[pDocEx->nPortNo], pBuffer);
		if(ret==RET_OK)
		{
			ret=UnMakeDeviceBuffer(pBuffer);
			if(ret==RET_OK)
			{
				bResult = true;
				break;
			}
			else
			{
				sprintf(ErrMsg,"-----!!!!校验%.8X返回包失败-消费开关!错误码=[%d]\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID,ret);
				printf(ErrMsg);
				WriteLog(ErrMsg);
				Sleep(1500);
			}
		}
		else
		{
			Sleep(200);
		}
	}

	if( !bResult )
	{
		printf(ErrMsg,"----!!!!设备%.8X签到失败!\n",pDocEx->pChildDoc[m_nIndex]->m_nAuthID);
		ReportLog(pDocEx->pChildDoc[m_nIndex], pTask, RET_REQU_SIGNIN, "终端机签到", "终端机签到--设置消费开关失败!");
		pDocEx->pChildDoc[pDocEx->nPortNo]->m_nSignIn = RET_REQU_SIGNIN ;
		WriteLog(ErrMsg);
		return RET_REQU_SIGNIN;
	}
	pDocEx->pChildDoc[pDocEx->nPortNo]->m_nSignIn = 1 ;
	GetCurDateTime(pDocEx->pChildDoc[pDocEx->nPortNo]->m_szSignInTime);
	printf(ErrMsg,"-----%.8X签到成功!\n",pDocEx->pChildDoc[pDocEx->nPortNo]->m_nAuthID);
	ReportLog(pDocEx->pChildDoc[m_nIndex], pTask, RET_OK, "终端机签到", "终端机签到成功!");
	WriteLog(ErrMsg);	
	return RET_OK;
}
Beispiel #12
0
long CSingInServer::ProcessSingIn(TSDeviceInfo *pDevInfo, TSSmartDoc *pDoc, TSCmdBuffer *pBuffer)
{
	bool          bResult = false;
	CTime		  tm;
	int			  E_number;
	unsigned char Clock[7];
	unsigned char Plain[256],ID[4];

	E_number = 1;

	//下传时钟
	for(int i=0; i<3; i++)
	{
		tm=CTime::GetCurrentTime();

		Clock[0] = tm.GetYear()-2000;	Clock[1] = tm.GetMonth();	Clock[2] = tm.GetDay();
		Clock[3] = tm.GetHour();	Clock[4] = tm.GetMinute();	Clock[5] = tm.GetSecond();
		Clock[6] = tm.GetDayOfWeek();

		MakeBuf_SetClock(pDevInfo, pBuffer, Clock);
		pBuffer->nTimeOut1 = 300;//1000 ;
		pBuffer->nTimeOut2 = 1000;
		if(SendRequest(pDoc, pBuffer) == RET_OK )
		{
			if(UnMakeDeviceBuffer(pBuffer) == RET_OK && !pBuffer->pBuffer[2])
			{
				bResult = true;
				break;
			}
		}
		Sleep(200);
	}

	if( !bResult )
	{
		printf("终端机签到--校对时钟失败!.\n");
		ReportLog(pDoc, m_pTask, RET_CLOCK_ERROR, "终端机签到", "终端机签到--校对时钟失败!");
		return RET_CLOCK_ERROR;
	}

	char szKeyBuf[256];

	memset(Plain, 0, sizeof(Plain));
	memset(szKeyBuf, 0, sizeof(szKeyBuf));

	GetAuthKey(szKeyBuf);

	SVC_DSP_2_HEX((unsigned char *)szKeyBuf, Plain, strlen(szKeyBuf)/2);
	Sleep(200);
	//设置授权信息
	bResult = false;
	for(i=0; i<3; i++)
	{
		SVC_DSP_2_HEX((unsigned char *)pDoc->m_szDeviceID, ID, 4);
		//modified by lina 20050308 SmartEncrypt(E_number,ID,Plain);
		MakeBuf_SetWorkingKey(pDevInfo, pBuffer, Plain, E_number);

		pBuffer->nTimeOut1 = 300 ;
		pBuffer->nTimeOut2 = 1000;
		//bResult = true;//debug 20050311
		if(SendRequest(pDoc, pBuffer) == RET_OK )
		{
			if(UnMakeDeviceBuffer(pBuffer) == RET_OK && !pBuffer->pBuffer[2] )
			{
				bResult = true;
				break;
			}
		}
		Sleep(200);
	}

	if( !bResult )
	{
		printf("终端机签到--设置授权信息失败!.\n");
		ReportLog(pDoc, m_pTask, RET_NOT_AUTH, "终端机签到", "终端机签到--设置授权信息失败!");
		return RET_NOT_AUTH;
	}

	//设置消费开关
	bResult = false;
	Sleep(200);
	for(i=0; i<3; i++)
	{
		MakeBuf_SetComsumeFlag(pDevInfo, pBuffer, 1);
		pBuffer->nTimeOut1 = 300;//1000 ;
		pBuffer->nTimeOut2 = 1000;
		if( SendRequest(pDoc, pBuffer) == RET_OK )
		{
			//debug 20050311
		//	ReportLog(pDoc, m_pTask, RET_REQU_SIGNIN, "终端机签到", "RETOK!");

			if( UnMakeDeviceBuffer(pBuffer) == RET_OK&& !pBuffer->pBuffer[2] )
			{
				bResult = true;
				break;
			}
		}
		Sleep(200);
	}

	if( !bResult )
	{
		printf("终端机签到失败!\n");
		ReportLog(pDoc, m_pTask, RET_REQU_SIGNIN, "终端机签到", "终端机签到--设置消费开关失败!");
		return RET_REQU_SIGNIN;
	}

	if( SignIn && pDoc->m_nSignIn != 1 )
	{
		SignIn(pDoc);
	}

	pDoc->m_nSignIn = 1 ;
	GetCurDateTime(pDoc->m_szSignInTime);

	ReportLog(pDoc, m_pTask, RET_OK, "终端机签到", "");
	return RET_OK;
}
//*=================================================================================
//*原型: void TSmartServer::Run()
//*功能: 服务器执行服务
//*参数: 无
//*返回: 无
//*说明: 前置采集服务器类
//*=================================================================================
void TSmartServer::Run()
{
	bool bReloadFlag = false;
	try
	{
		m_OutThread.Start();
		m_SmartMonitor.Start();

		//如果没有获取到系统的设备档案数据,则始终向金仕达发获取档案的任务.
		while(1 )
		{
			if( GetDocCount() <= 0 )
			{
				ReportLog("没有设备档案信息! 服务器自动重新装入设备档案表!");
				InitSmartDocList();
				Sleep(4000);				
			}
			else
			{
				break;
			}
		}

		bool  bRet=false;
		long  nNoTaskTick=0,nTick=0,nCollDataTick=0;
		
		m_CollDataTaskObj.nTaskPlanID  = 100 ;
		strcpy(m_CollDataTaskObj.szTaskName, "收数");
		m_CollDataTaskObj.nTaskCycle   = 1 ;
		GetCurDateTime(m_CollDataTaskObj.szBeginTime);
		m_CollDataTaskObj.nRepeatTime  = 0 ;                 //持续多长时间(分钟)
		m_CollDataTaskObj.nRepeatTimes = 0;                //重复次数

		int  k = 0;
		long i = 0 ;
		for(i=0; i< m_DocList.GetCount(); i++)
		{
			TSmartDocObj *pObj = (TSmartDocObj*)m_DocList[i];
			if(!pObj->m_nParentID)
			{
				m_CollDataTaskObj.pTask[k].nTaskID     = i + 100 ;
				m_CollDataTaskObj.pTask[k].nTaskPlanID = 01;
				m_CollDataTaskObj.pTask[k].nAuthID     = pObj->m_nAuthID ;
				strcpy(m_CollDataTaskObj.pTask[k].szTaskCode, "01");
				strcpy(m_CollDataTaskObj.pTask[k].szName, "实时收数");
				strcpy(m_CollDataTaskObj.pTask[k].szType, "01");
				m_CollDataTaskObj.pTask[k].nPriority = 0 ;
				m_CollDataTaskObj.nTask++;  //设备总数量
				k++;
			}
		}
		m_CollDataTaskObj.SetBeginTime();
		ExecuteTask(&m_CollDataTaskObj);
		//执行任务
		static TSmartTaskObj curObj;
		BOOL bNoTask=FALSE;
		int ret=-1;
		nCollDataTick = GetTickCount();
		while( !IsShutdown() )
		{	
				bRet = false;
				//如果后台没有任务要处理,则开始采集数据任务
				while(bNoTask)
				{
					ExecuteCollDataTask();
					nTick = GetTickCount();
					nCollDataTick = nTick;
					//如果采集数据时间超过了2秒钟,则跳出,开始向后台请求新的任务
					if(nTick-nNoTaskTick>=m_nNoTask_Tick)//2秒
					{
						break;
					}
					Sleep(1000);
				}
				nTick=GetTickCount();
				//如果距离上次采集数据的时间已经超过了5秒钟,则再发起一次采集数据的任务
				if(nTick-nCollDataTick>=m_nCollData_Tick)
				{
					nCollDataTick = nTick;
					ExecuteCollDataTask();
				}
				//发送心跳请求包
				ret=m_Channels.SendTick();
				if(ret!=RET_OK)
				{
					continue;
				}
				ZeroMemory(&curObj, sizeof(curObj));
				curObj.Clear();
				ret=m_Channels.GetSmartTaskPlan(&curObj);
				switch(ret)
				{	
					case RET_OK:
						ReportLog("选中任务: %s(%d), 执行次数:%d, 执行时长:%d, 设备数:%d.\n",curObj.szTaskName, curObj.nTaskPlanID,curObj.nRepeatTimes, curObj.nRepeatTime, curObj.nTask); 
						curObj.SetBeginTime();
						bRet = ExecuteTask(&curObj);
						if(!bRet)
						{
							for(int j=0; j< curObj.nTask; j++)
							{
								TSResultData  data;
								ZeroMemory(&data, sizeof(data));
								strcpy(data.sMsg, "任务执行失败[可能终端正忙]!");
								m_Channels.ReportTaskResult(&curObj.pTask[j], RET_TERME_NOANSWER, &data);
							}
						}
						bNoTask=FALSE;
						break;
					case RET_NOTASK:
						ReportLog("金仕达没有任务下达"); 
						nNoTaskTick = GetTickCount();
						bNoTask=TRUE;
						break;
					default:
						ReportLog("向金仕达请求任务失败"); 
						bNoTask=FALSE;
						break;
				}
			/*
			if(!GetMemoryInfo())
			{
				ReportError("获取系统内存信息失败,请关闭前置程序,重新启动!");				
			}
			printf("TotalMemory %dK,UseMemory %dK,FreeMemory %dK\n",nTotalMemory,nUseMemory,nFreeMemory);	
			if(nFreeMemory/1024<10)
			{
				ReportError("可用内存小于10M,请关闭一些其它无用的程序");				
			}
			*/
		}
		m_OutThread.Shutdown();
	}
	catch(TException& e)
	{
		ReportError(e.GetText());
		printf("------ e error ----\n");
		Shutdown();
	}
	catch(...)
	{
		ReportError("未知的错误导致服务器终止!");
		Shutdown();
	}

	PostQuitMessage(0);
}
//*========================================================================================
//*函数: bool CSmartJZSRCTable::Convert(TSSmartDoc *pDoc, unsigned char *ucRawData)
//*功能: 转换结构
//*参数: 略
//*返回: 是否成功
//*说明: 虚基类程序
//*========================================================================================
bool CSmartJZSRCTable::Convert(int nAuthNo, TSSmartDoc *pDoc, unsigned char *ucRawData, char *pszAdjustCode)
{
	CString  strValue = "" ;
	CString  strText = "";
	CString  strData = "";
	char     szDateTime[7];

	CString strDealCode = "20";

	//收费机 0232 上机上网机
	if( !strcmp(pDoc->m_szMacCode, "0226") || 
		!strcmp(pDoc->m_szMacCode, "0232") )
	{
		strDealCode = "91";
	}
	//增值机
	else if( !strcmp(pDoc->m_szMacCode, "0201") )
	{
		strDealCode = "90";
	}

	CTime  t = CTime::GetCurrentTime();
	strText.Format("%04d-%02d-%02d %02d:%02d:%02d  ", 
		t.GetYear(), t.GetMonth(), t.GetDay(),
		t.GetHour(), t.GetMinute(), t.GetSecond());

	sprintf(szDateTime, "%04d%02d", t.GetYear(), t.GetMonth()); 

	m_strTableName.Format("Smart_JZSource%04d%02d", t.GetYear(), t.GetMonth());

	strValue.Format("注册号:%.2X%.2X%.2X%.2X ",ucRawData[0],ucRawData[1],ucRawData[2],ucRawData[3]); strText += strValue ;
	GetValue(strValue, m_SRC.sMachineID);
	strValue.Format("%s", m_SRC.sMachineID); strData+= strValue;

	strValue.Format("扎帐流水:%d ",  ucRawData[6]*256+ucRawData[7]); strText += strValue ;
	GetValue(strValue, m_SRC.nSettleInvoice);
	strValue.Format("%d", m_SRC.nSettleInvoice); strData+= strValue;

	if( !IsValidDateTime(&ucRawData[8])  )
	{
		char szDateTime[24];
		GetCurDateTime(szDateTime);
		strValue = szDateTime;  strText += strValue ;
		GetValue(strValue, m_SRC.sSettleTime);
	}
	else
	{
		strValue.Format("扎帐时间:%04d-%02d-%02d %02d:%02d:%02d ",ucRawData[8]+2000,ucRawData[9],ucRawData[10],ucRawData[11],ucRawData[12],ucRawData[13]);  strText += strValue ;
		GetValue(strValue, m_SRC.sSettleTime);
	}

	strValue.Format("%s", m_SRC.sSettleTime); strData+= strValue;

	strValue.Format("起始流水号:%d ",ucRawData[14]*256+ucRawData[15]); strText += strValue ;
	GetValue(strValue, m_SRC.nBeginInvoice);
	strValue.Format("%d", m_SRC.nBeginInvoice); strData+= strValue;

	strValue.Format("结束流水号:%d ",ucRawData[16]*256+ucRawData[17]); strText += strValue ;
	GetValue(strValue, m_SRC.nEndInvoice);
	strValue.Format("%d", m_SRC.nEndInvoice); strData+= strValue;

	strValue.Format("正常消费总笔数:%d ",ucRawData[18]*256+ucRawData[19]); strText += strValue ;
	GetValue(strValue, m_SRC.nDealCount);
	strValue.Format("%d", m_SRC.nDealCount); strData+= strValue;

	strValue.Format("正常消费总金额:%d ",ucRawData[20]+ucRawData[21]*256+ucRawData[22]*65536); strText += strValue ;
	GetValue(strValue, m_SRC.nDealAmount);
	strValue.Format("%d", m_SRC.nDealAmount); strData+= strValue;

	strValue.Format("冲正消费总笔数:%d ",ucRawData[23]*256+ucRawData[24]);  strText += strValue ;
	GetValue(strValue, m_SRC.nCancelCount);
	strValue.Format("%d", m_SRC.nCancelCount); strData+= strValue;

	strValue.Format("冲正消费总金额:%d ",ucRawData[25]+ucRawData[26]*256+ucRawData[27]*65536); strText += strValue ;
	GetValue(strValue, m_SRC.nCancelAmount);
	strValue.Format("%d", m_SRC.nCancelAmount); strData+= strValue;

	strValue.Format("异常消费总笔数%d\n",ucRawData[28]*256+ucRawData[29]); strText += strValue ;
	GetValue(strValue, m_SRC.nExcepCount);
	strValue.Format("%d", m_SRC.nExcepCount); strData+= strValue;

	strValue.Format("异常消费总金额%d\n",ucRawData[30]+ucRawData[31]*256+ucRawData[32]*65536); strText += strValue ;
	GetValue(strValue, m_SRC.nExcepACount);
	strValue.Format("%d", m_SRC.nExcepACount); strData+= strValue;

	strValue.Format("其他交易总笔数:%d ",ucRawData[33]*256+ucRawData[34]); strText += strValue ;
	GetValue(strValue, m_SRC.nOtherCount);
	strValue.Format("%d", m_SRC.nOtherCount); strData+= strValue;

	strValue.Format("扎帐标记:%.2X ",ucRawData[35]); strText += strValue ;
	GetValue(strValue, m_SRC.nOuterkeeper);
	strValue.Format("%d", m_SRC.nOuterkeeper); strData+= strValue;

	int nSettType = 0;

	if(ucRawData[35] == 0x00)
		strValue = "初次运行扎帐 ";
	else if(ucRawData[35] == 0x01)
		strValue = "上位机扎帐 ";
	else if(ucRawData[35] == 0x02)
		strValue = "复核扎帐 ";
	else if(ucRawData[35] == 0x03)
	{
		nSettType = 0;
		strValue = "手工扎帐 ";
	}
	else if(ucRawData[35] == 0x04)
	{
		nSettType = 1;
		strValue = "定时扎帐 ";
	}
	else if(ucRawData[35] == 0x05)
	{
		nSettType = 1;
		strValue = "开机扎帐 ";
	}
	else
		strValue = "未知类型 ";
	strValue.Format("CRC:%.2X%.2X\n", ucRawData[36],ucRawData[37]);  strText += strValue ;

	CString strAdjust=GetAdjustCode(pszAdjustCode, m_SRC.nSettleInvoice, 
		m_SRC.sSettleTime, "Smart_Settlement");

	if( strAdjust == "" && 	!IsValidDateTime(&ucRawData[8])  )
	{
		strAdjust = "0204";
	}

	m_pDoc = pDoc ;

	CString strTemp = "" ;

	strTemp = "INSERT INTO " + m_strTableName ;
	strTemp+= "(SMT_RowID, SMT_AuthNo, SMT_Data, SMT_CRC, ";
	strTemp+= "SMT_DateTime, SMT_DealWith, SMT_DealWithDateTime, SMT_AdjustCode) ";
	strTemp+= " VALUES(SMART_JZSOURCE%s_rowid.nextval, %ld, '%s', %d, SYSDATE, 0, NULL, '%s')";

	if( strAdjust == "0102" )
	{
		strSQL1.Format(strTemp, szDateTime, nAuthNo, 
			strData.GetBuffer(0), 0, strAdjust.GetBuffer(0));
	}
	else
	{
		strSQL1.Format(strTemp, szDateTime, nAuthNo, 
			strData.GetBuffer(0), 1, strAdjust.GetBuffer(0));
	}

	strTemp = "INSERT INTO Smart_Settlement(" ;
	strTemp+= " SMT_RowID, SMT_Authno, SMT_MachineID, SMT_Org_id, " ;
	strTemp+= " SMT_SettleInvoice, SMT_SettleTime, SMT_BeginInvoice, " ;
	strTemp+= " SMT_EndInvoice, SMT_DealCount, SMT_DealAmount, " ;
	strTemp+= " SMT_CancelCount, SMT_CancelAmount, SMT_OtherCount, " ;
	strTemp+= " SMT_OperatorID, SMT_KeepingDate, SMT_BatchNo, " ;
	strTemp+= " SMT_SettleType, SMT_DealCode, SMT_EXCEPTCOUNT, SMT_EXCEPTAMOUNT, SMT_ADJUSTCODE) VALUES( " ;
	strTemp+= " SMART_SETTLEMENT_ROWID.nextval, %d, '%s', '%s', " ;
	strTemp+= " %d, to_date('%s','yyyy-mm-dd hh24:mi:ss'), %d, " ;
	strTemp+= " %d, %d, %d, " ;
	strTemp+= " %d, %d, %d, " ;
	strTemp+= " '%d', SYSDATE, %d, " ;
	strTemp+= " %d, '%s', %d, %d,'%s')" ;

	char  szOrg[12];
	if( !strcmp(pDoc->m_szOrgid, "") )
		strcpy(szOrg, " ");
	else
		strcpy(szOrg, pDoc->m_szOrgid);

	strSQL2.Format(strTemp.GetBuffer(0), nAuthNo, m_SRC.sMachineID, szOrg, 
		m_SRC.nSettleInvoice, m_SRC.sSettleTime, m_SRC.nBeginInvoice, 
		m_SRC.nEndInvoice, m_SRC.nDealCount, m_SRC.nDealAmount, 
		m_SRC.nCancelCount, m_SRC.nCancelAmount, m_SRC.nOtherCount, 
		999, 0, 
		nSettType, strDealCode.GetBuffer(0), m_SRC.nExcepCount, m_SRC.nExcepACount, strAdjust.GetBuffer(0));

	strUpdate.Format("Update Smart_Commrec Set  SMT_LASTDEALDATETIME=to_date('%s','yyyy-mm-dd hh24:mi:ss'), SMT_SETTLEINVOICE=%ld, SMT_SETTLETIME=to_date('%s','yyyy-mm-dd hh24:mi:ss'), SMT_BEGININVOICE=%d, SMT_ENDINVOICE=%d Where Smt_AuthNo=%ld ", 
		m_SRC.sSettleTime, m_SRC.nSettleInvoice, m_SRC.sSettleTime, m_SRC.nBeginInvoice, 
		m_SRC.nEndInvoice, nAuthNo);
/*
	CString strString;

	pDoc->m_nFlow = m_SRC.nSettleInvoice;
	pDoc->m_nCardID = 0;
	pDoc->m_nInMoney = m_SRC.nDealAmount;
	pDoc->m_nOutMoney = m_SRC.nCancelAmount;
	pDoc->m_nDealCount++;

	strString.Format("%d&%d&%d&%s&%d&%d&%d",
		pDoc->m_nAuthID, m_SRC.nSettleInvoice, pDoc->m_nDealCount, 
		m_SRC.sSettleTime, 0, m_SRC.nDealAmount, m_SRC.nCancelAmount);

	BroadcastPro(PACKET_CMD_1002, pDoc, strString.GetBuffer(0), strString.GetLength());
*/
	return true;
}