#include "beatProc.h"

void localBeatRecv(BYTE oidLen, DWORD *oidArr, BYTE *pdata,WORD dataLen,snmp_addr *addr);
int initSysConf();
void update_state(_sys_conf *psysconf,int sysNo,int subSysNo,BYTE *pdata,int dataLen,snmp_addr *addr);
void update_ems_process_state(DWORD* oidArr,BYTE *pdata);
void update_msc_trap_msg(BYTE oidLen, DWORD *oidArr, BYTE *pdata,WORD dataLen,snmp_addr *addr);
void update_alarm(_sys_conf *psysconf,int sysno,int subsysno,int compcode,int alarmcode);
BYTE parseoid(char *str, DWORD * array, BYTE max);
char *oidtostr(DWORD* oid,int oidlen,char *output);

static char beatproc_sqlstr[8192];
static char state_str[MAX_MSG_LEN];
static _sys_conf sys_conf[MAX_SYS_CONF];
static int sys_conf_count=0;
static BYTE beatProcSequenceNum[4]={0,0,0,0};
static int dwordlen;
unsigned long receive_localbeat_time=0;
unsigned long beatProc_time=0;
unsigned long cons=0;
int beatProcInit(unsigned char *sequenceNum)
{
	int i;
	
	ems_debug(BEATPROC_DEBUG_LOGFILE,"beatProc is starting ...");
	
	for(i=0;i<4;i++)
		beatProcSequenceNum[i]=sequenceNum[i];
	ems_debug(BEATPROC_DEBUG_LOGFILE,"beatProc is starting ...:%ld,%ld",(long)beatProcSequenceNum,(long *)sequenceNum);
	initSysConf();
	inquire_trapmsg(localBeatRecv);
	return(1);
}


int beatProc(void)
{
	static int check_conf_index=0;
	static int check_sys_index=0;
	static int check_subsys_index=0;
	_sys_state_mng *psys;
	
	if(!sys_conf[check_conf_index].isalive)
	{
		check_conf_index++;
		return 1;
	}
	
	if(check_conf_index >= sys_conf_count)
	{
		check_conf_index=0;
		check_sys_index=0;
		check_subsys_index=0;
		return 1;
	}
	
	if(check_sys_index >= sys_conf[check_conf_index].maxsysnum)
	{
		check_conf_index++;
		check_sys_index=0;
		check_subsys_index=0;
		return 1;
	}
	
	if(check_subsys_index >= sys_conf[check_conf_index].maxsubsysnum)
	{
		check_sys_index++;
		check_subsys_index=0;
		return 1;
	}
	//beatProc_time=time(NULL);
	//cons=beatProc_time-receive_localbeat_time;
	//ems_debug(BEATPROC_DEBUG_LOGFILE,"cons time:%d",cons);
	psys=&sys_conf[check_conf_index].systems[check_sys_index][check_subsys_index];
	if(psys->isalive)
	{
		if(time(NULL) - psys->lastupdatetime > MAX_HEARTBEAT_TIMEOUT)
		{
			psys->isoffline=1;
			update_alarm(&sys_conf[check_conf_index],check_sys_index,check_subsys_index,0,254);
		}
	}
	
	check_subsys_index++;
	
	return(1);
}


int initSysConf()
{
	MYSQL *pubConn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	int systypeno;
	int agentforsys;
	int index;
	int i,j;
	
	memset(sys_conf,0,sizeof(sys_conf));
	dwordlen=sizeof(DWORD);
	pubConn=mysql_conn("localhost","OMC_PUB");
	if(pubConn == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"Connect to OMC_PUB fail");
		return 0;
	}
	
	sprintf(beatproc_sqlstr,"delete from OMC_PUB.sysInfo;");
	if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
	}
	
	sprintf(beatproc_sqlstr,"delete from OMC_PUB.sysStat;");
	if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
	}
	
	sprintf(beatproc_sqlstr,"select sysTypeNo,sysTypeName,maxSysNum,maxSubSysNum,snmpHead from OMC_PUB.sysConf where needHeartBeat>0;");
	res=mysql_getres(pubConn,beatproc_sqlstr);
	if(res == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		return 0;
	}
	while((row=mysql_fetch_row(res)) != NULL)
	{
		sys_conf[sys_conf_count].systypeno=atoi(row[0]);
		sprintf(sys_conf[sys_conf_count].systypename,"%s",row[1]);
		sys_conf[sys_conf_count].maxsysnum=atoi(row[2]);
		sys_conf[sys_conf_count].maxsubsysnum=atoi(row[3]);
		sys_conf[sys_conf_count].prefixoidlen=parseoid(row[4],sys_conf[sys_conf_count].prefixoid,24);
		
		for(i=0;i<sys_conf[sys_conf_count].maxsysnum;i++)
		{
			for(j=0;j<sys_conf[sys_conf_count].maxsubsysnum;j++)
			{
				sys_conf[sys_conf_count].systems[i][j].isoffline=1;
				sys_conf[sys_conf_count].systems[i][j].lastcompcode=0;
				sys_conf[sys_conf_count].systems[i][j].lastalarmcode=254;
			}
		}
		
		sys_conf_count++;
		if(sys_conf_count >= MAX_SYS_CONF)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"The count of configured modules is greater than %d",MAX_SYS_CONF);
			break;
		}
	}
	mysql_free_result(res);
	
	sprintf(beatproc_sqlstr,"select sysTypeNo,otherModuleSendHeart from OMC_PUB.sysConf;");
	res=mysql_getres(pubConn,beatproc_sqlstr);
	if(res == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		return 0;
	}
	while((row=mysql_fetch_row(res)) != NULL)
	{
		systypeno=atoi(row[0]);
		agentforsys=atoi(row[1]);
		for(index=0;index<sys_conf_count;index++)
		{
			if(agentforsys == sys_conf[index].systypeno)
			{
				ems_debug(BEATPROC_DEBUG_LOGFILE,"systypeno %d's heartbeat is agent by systypeno %d",systypeno,agentforsys);
				sys_conf[index].agentforsystypeno[sys_conf[index].agentnum]=systypeno;
				sys_conf[index].agentnum++;
				break;
			}
		}
	}
	mysql_free_result(res);
	
	mysql_close(pubConn);
	return 1;
}

/* by simon at 23/9/26 */
extern int corefile_flag;

void localBeatRecv(BYTE oidLen, DWORD *oidArr, BYTE *pdata,WORD dataLen,snmp_addr *addr)
{
	int index;
	int i;
	int sysno;
	int subsysno;
	_sys_conf *psysconf;
	WORD j;

	//check oidlen
	if(oidLen < 7)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"The length of oid is smaller than 7");
		return;
	}

	//find related system
	for(index=0;index<sys_conf_count;index++)
	{
		if(memcmp(sys_conf[index].prefixoid,oidArr,sys_conf[index].prefixoidlen*dwordlen) == 0)
		{
			psysconf=&sys_conf[index];
			break;
		}
	}
	if(index == sys_conf_count)
	{
		//char oidstr[256];
		//struct in_addr newInaddr;
		//newInaddr.s_addr = addr->remote_ip;
		//ems_debug(BEATPROC_DEBUG_LOGFILE,"Unknown oid %s from %s",oidtostr(oidArr,oidLen,oidstr),inet_ntoa(newInaddr));
//printf("Receive hb from %s, OID:%s\n",inet_ntoa(newInaddr),oidtostr(oidArr,oidLen,oidstr));
		return;
	}
	//check oid
	/*char tmpstr[64];
	char oidstring[256];
	char pdatastring[256];
	char Sequencstring[256];
	struct in_addr newInaddr;
	oidstring[0]=0;
	pdatastring[0]=0;
	Sequencstring[0]=0;
	
	receive_localbeat_time=time(NULL);
	for(i=0;i<oidLen;i++)
	{
		sprintf(tmpstr,"%ld.",oidArr[i]);
		strcat(oidstring,tmpstr);
	}
	newInaddr.s_addr = addr->remote_ip;
	if(psysconf->systypeno == 0 && (strcmp(inet_ntoa(newInaddr),"172.25.196.80")==0 || strcmp(inet_ntoa(newInaddr),"172.25.196.200")==0))
	{
		if(!strcmp(oidstring,"1.3.6.1.4.1.1373.2.4.5.1."))
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"receive_localbeat_time:%ld",receive_localbeat_time);
			ems_debug(BEATPROC_DEBUG_LOGFILE,"oidstring:%s",oidstring);
			//ems_debug(BEATPROC_DEBUG_LOGFILE,"pdata:");
			for(j=0;j<dataLen;j++)
			{
				sprintf(tmpstr,"%02X,",pdata[j]);
				strcat(pdatastring,tmpstr);		
			}	
				ems_debug(BEATPROC_DEBUG_LOGFILE,"%s,",pdatastring);
				
		
			ems_debug(BEATPROC_DEBUG_LOGFILE,"remotIPAddess:%s",inet_ntoa(newInaddr));
		for(i=0;i<4;i++)
		{
			sprintf(tmpstr,"%02X,",beatProcSequenceNum[i]);
			strcat(Sequencstring,tmpstr);
		}
		}
	}*/
	//another omc
	if(psysconf->systypeno == 0)
	{
		if(oidArr[7] == 2 && oidArr[8] == 4 && oidArr[9] == 10)
		{
			update_ems_process_state(oidArr,pdata);
			return;
		}
		
		for(i=0;i<4;i++)
		{
			//ems_debug(BEATPROC_DEBUG_LOGFILE,"pdata[39+%d]:%d",i,(int *)pdata[39+i]);
			//ems_debug(BEATPROC_DEBUG_LOGFILE,"beatProcSequenceNum[%d]:%d",i,(int)beatProcSequenceNum[i]);

			if(pdata[39+i] != beatProcSequenceNum[i])
			{
				//ems_debug(BEATPROC_DEBUG_LOGFILE,"beatProcSequenceNum:%s",Sequencstring);
				return;
			}
		}
		//ems_debug(BEATPROC_DEBUG_LOGFILE,"EqHBProSequenceNum:%s",Sequencstring);
		
	}
	//msc trap message for parameter
	else if(psysconf->systypeno == 320)
	{
		if(oidLen <= psysconf->prefixoidlen)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"MSC trap message oid is too short %d <= %d",oidLen,psysconf->prefixoidlen);
			return;
		}
		//parameter
		if(oidArr[psysconf->prefixoidlen] == 2)
		{
			update_msc_trap_msg(oidLen,oidArr,pdata,dataLen,addr);
			return;
		}
	}
	

	
	//check sysno and subsysno
	sysno=pdata[0];
	subsysno=pdata[1];
	if(sysno >= psysconf->maxsysnum || subsysno >= psysconf->maxsubsysnum)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"System %s sysno or subsysno is too big,[%d,%d] > [%d,%d]",psysconf->systypename,sysno,subsysno,psysconf->maxsysnum,psysconf->maxsubsysnum);
		return;
	}
	
	if(psysconf->systems[sysno][subsysno].isalive == 0)
	{
		psysconf->systems[sysno][subsysno].isalive=1;
		//printf("%s-%d-%d-%d is alive\n",psysconf->systypename,index,sysno,subsysno);
	}
	
	if(psysconf->isalive == 0)
	{
		psysconf->isalive=1;
	}
	
	psysconf->systems[sysno][subsysno].lastupdatetime=time(NULL);
	
	update_state(psysconf,sysno,subsysno,pdata,dataLen,addr);
}


void update_state(_sys_conf *psysconf,int sysNo,int subSysNo,BYTE *pdata,int dataLen,snmp_addr *addr)
{
	int compcode;
	int alarmcode;
	int state;
	MYSQL *pubConn;
	struct in_addr newInaddr;
	int i;
	char *strip;
	
	if(psysconf->systypeno == 111|| psysconf->systypeno == 112)
	{
		compcode=pdata[14];
		alarmcode=pdata[15];
	}
	else
	{
		compcode=pdata[14]*256+pdata[15];
		alarmcode=pdata[16];
	}
	
	if(alarmcode != 0)
		state=0;
	else
		state=1;
	
	if(psysconf->systypeno==200)
		state=1;

	pubConn=mysql_conn("localhost","OMC_PUB");
	if(pubConn == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"Connect to OMC_PUB fail");
		return;
	}
	
	//update sysStat
	state_str[0]=0;
	omc_RbcdToAscii(state_str,pdata,dataLen*2);
	sprintf(beatproc_sqlstr,"replace OMC_PUB.sysStat(sysTypeNo,sysNo,subSysNo,detailStat,updateTime)values('%d','%d','%d','%s',CURRENT_TIMESTAMP);",
		psysconf->systypeno,sysNo,subSysNo,state_str);
	if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR].%s",beatproc_sqlstr);
	}
	//if(psysconf->systypeno==0)
		//ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL msg].%s",beatproc_sqlstr);

	//update sysInfo
	newInaddr.s_addr = addr->remote_ip;
	strip=inet_ntoa(newInaddr);
	sprintf(beatproc_sqlstr,"replace OMC_PUB.sysInfo(sysTypeNo,sysNo,subSysNo,isRemote,netId,DPC,ip,baseStat,updateTime)values('%d','%d','%d','0','0','0','%s','%d',CURRENT_TIMESTAMP);",
		psysconf->systypeno,sysNo,subSysNo,strip,state);
	if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR].%s",beatproc_sqlstr);
	}
	//if(psysconf->systypeno==0)
		//ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL msg].%s",beatproc_sqlstr);
	
	//update the sysInfo for the agent heartbeat
	for(i=0;i<psysconf->agentnum;i++)
	{
		sprintf(beatproc_sqlstr,"replace OMC_PUB.sysInfo(sysTypeNo,sysNo,subSysNo,isRemote,netId,DPC,ip,baseStat,updateTime)values('%d','%d','%d','0','0','0','%s','%d',CURRENT_TIMESTAMP);",
			psysconf->agentforsystypeno[i],sysNo,subSysNo,strip,state);
		if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR].%s",beatproc_sqlstr);
		}
		//if(psysconf->systypeno==0)
			//ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL msg].%s",beatproc_sqlstr);
		
	}
	//if(psysconf->systypeno == 0)
		//ems_debug(BEATPROC_DEBUG_LOGFILE,"%d %d %d %s %s",psysconf->systypeno,sysNo,subSysNo,inet_ntoa(newInaddr),state_str);
	
	//clear the offline alarm
	if(psysconf->systems[sysNo][subSysNo].isoffline)
	{
		psysconf->systems[sysNo][subSysNo].isoffline=0;
		sprintf(beatproc_sqlstr,"update OMC_PUB.sysAlarmLog set clearTime=CURRENT_TIMESTAMP,clearMode=1,clearBy='System' where sysTypeNo=%d and sysNo=%d and subSysNo=%d and compCode=0 and alarmCode=254;",
			psysconf->systypeno,sysNo,subSysNo);
		if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
		else
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"%s",beatproc_sqlstr);
		}
	}
	
	mysql_close(pubConn);
	
	//update alarm
	update_alarm(psysconf,sysNo,subSysNo,compcode,alarmcode);
}

void update_ems_process_state(DWORD* oidArr,BYTE *pdata)
{
	MYSQL *pubConn;
	
	pubConn=mysql_conn("localhost","OMC_PUB");
	if(pubConn == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"Connect to OMC_PUB fail");
		return;
	}
	sprintf(beatproc_sqlstr,"replace OMC_PUB.procInfo (proc_id,proc_name,updateTime)VALUES ('%d','%s',CURRENT_TIMESTAMP)",(int)oidArr[10],(char*)pdata);
	if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
	}
	ems_debug(BEATPROC_DEBUG_LOGFILE,"%ld %s update",time(NULL),(char*)pdata);
	mysql_close(pubConn);
}

void update_msc_trap_msg(BYTE oidLen, DWORD *oidArr, BYTE *pdata,WORD dataLen,snmp_addr *addr)
{
	int oid_param=oidArr[11];
	int oid_x=oidArr[12];
	int oid_y=oidArr[13];
	int index;
	char selSql[256];
	char instanceNo[32];
	char tempstr[64];
	char *p;
	MYSQL_RES *selRes;
	char tmpBuf[MAX_MSG_LEN * 2]= "\0";
	int res_num=0;
	instanceNo[0]='\0';
	selSql[0]='\0';
	MYSQL *dbConn;
	
	dbConn=mysql_conn("localhost","OBJ_320");
	if(dbConn == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"Connect to OBJ_320 fail");
		return;
	}
	
	//判断该参数是不是一个表格参数
	sprintf(beatproc_sqlstr,"SELECT level_1,level_2 FROM OBJ_320.paramConf WHERE name_2 LIKE '%%Table' AND level_1=%d AND level_2=%d AND level_3=%d AND level_4=0 ",oid_param,oid_x,oid_y);
	selRes =(MYSQL_RES*) mysql_getres(dbConn,beatproc_sqlstr);
	
	//数据库出现异常
	if(selRes == NULL){
		ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		mysql_close(dbConn);
		return;
	}
	
	res_num=mysql_num_rows(selRes);
	mysql_free_result(selRes);	
	
	//initValue and readValue
	omc_RbcdToAscii(tmpBuf,pdata,dataLen*2);

	tempstr[0]='\0';
	//如果不是表格参数，则后续OID为一位，接下来的全为INSTANCE ID
	if(res_num==0)
	{
		//获取instanceNo
		for(index=15;index<oidLen;index++)
		{
			sprintf(tempstr,"%.4ld.",oidArr[index]);
			strcat(instanceNo,tempstr);
		}
		p=strrchr(instanceNo,'\056');
		if(p!=NULL)
			*p='\0';

		sprintf(beatproc_sqlstr,"REPLACE OBJ_320.param_99 (level_1,level_2,level_3,level_4,instanceNo,`initValue`,`readValue`)"\
				" VALUES ('%d','%d','%d','%ld','%s','%d','%d')",
				oid_param,oid_x,oid_y,oidArr[14],instanceNo,*pdata,*pdata);
		if(mysql_getnores(dbConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
	}
	//如果是个表格参数，则后续OID为两位，接下来的全为INSTANCE ID
	else
	{
		//获取instanceNo
		for(index=16;index<oidLen;index++)
		{
			sprintf(tempstr,"%.4ld.",oidArr[index]);
			strcat(instanceNo,tempstr);
		}
	
		p=strrchr(instanceNo,'\056');
		if(p!=NULL)
			*p='\0';
		
		sprintf(beatproc_sqlstr,"REPLACE OBJ_320.param_99 (level_1,level_2,level_3,level_4,level_5,instanceNo,`initValue`,`readValue`)"\
				" VALUES ('%d','%d','%d','%ld','%ld','%s','%d','%d')",
				oid_param,oid_x,oid_y,oidArr[14],oidArr[15],instanceNo,*pdata,*pdata);
		if(mysql_getnores(dbConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
	}
	mysql_close(dbConn);
}

void update_alarm(_sys_conf *psysconf,int sysno,int subsysno,int compcode,int alarmcode)
{
	_sys_state_mng *psys;
	MYSQL *pubConn;
	char condition[256];
	
	psys=&psysconf->systems[sysno][subsysno];
	
	if(psys->lastcompcode == compcode && psys->lastalarmcode == alarmcode)
		return;

	pubConn=mysql_conn("localhost","OMC_PUB");
	if(pubConn == NULL)
	{
		ems_debug(BEATPROC_DEBUG_LOGFILE,"Connect to OMC_PUB fail");
		return;
	}
	
	//clear alarm
	if(alarmcode == 0)
	{
		if(compcode == 0)
			sprintf(condition,"sysTypeNo=%d and sysNo=%d and subSysNo=%d and clearMode=0",psysconf->systypeno,sysno,subsysno);
		else
			sprintf(condition,"sysTypeNo=%d and sysNo=%d and subSysNo=%d and clearMode=0 and compCode=%d",psysconf->systypeno,sysno,subsysno,compcode);
		sprintf(beatproc_sqlstr,"update OMC_PUB.sysAlarmLog set clearTime=CURRENT_TIMESTAMP,clearMode=1,clearBy='System' where %s;",condition);
		if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
		else
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"%s",beatproc_sqlstr);
		}
	}
	//insert alarm
	else
	{
		sprintf(beatproc_sqlstr,"update OMC_PUB.sysAlarmLog set clearTime=CURRENT_TIMESTAMP,clearMode=1,clearBy='System' where sysTypeNo=%d and sysNo=%d and subSysNo=%d and compCode=%d and alarmCode=%d and clearMode=0;",
			psysconf->systypeno,sysno,subsysno,compcode,alarmcode);
		if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
		else
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"%s",beatproc_sqlstr);
		}
		sprintf(beatproc_sqlstr,"insert into OMC_PUB.sysAlarmLog(sysTypeNo,sysNo,subSysNo,compCode,alarmCode,alarmTime,clearTime,clearMode,clearInfo,clearBy)values(%d,%d,%d,%d,%d,CURRENT_TIMESTAMP,'0000-00-00 00:00:00',0,NULL,NULL);",
			psysconf->systypeno,sysno,subsysno,compcode,alarmcode);
		if(mysql_getnores(pubConn,beatproc_sqlstr) != 0)
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"[SQL ERR],%s",beatproc_sqlstr);
		}
		else
		{
			ems_debug(BEATPROC_DEBUG_LOGFILE,"%s",beatproc_sqlstr);
		}
	}
	
	psys->lastcompcode=compcode;
	psys->lastalarmcode=alarmcode;
	
	mysql_close(pubConn);
}

BYTE parseoid(char *str, DWORD * array, BYTE max)
{
	BYTE sub = 0;
	short len, i;
	char *pvar;

	len = strlen(str);
	pvar = str;
	for (i = 0; i < len && sub < max; i++) {
		if (str[i] == '.') {
			str[i] = '\0';
			if (strlen(pvar) == 0)
				continue;
			array[sub++] = atoi(pvar);
			pvar = str + i + 1;
		}
	}
	if (strlen(pvar) == 0)
		return sub;
	array[sub++] = atoi(pvar);
	return sub;
}

char *oidtostr(DWORD* oid,int oidlen,char *output)
{
	char tmpstr[8];
	int i;
	
	output[0]=0;
	for(i=0;i<oidlen;i++)
	{
		if(i == (oidlen-1))
			sprintf(tmpstr,"%ld",oid[i]);
		else
			sprintf(tmpstr,"%ld.",oid[i]);
		strcat(output,tmpstr);
	}
	return output;
}

