// rdbapiwap.cpp For vc6,vc7,vc8,vc9,vc10 X86/X64
#include "stdafx.h"
#include "rdbapiwap.h"

//////////////////////////////////////////////////////////////////////////
// CRdbApi
CRdbApi::CRdbApi()
{
	m_hinstLib = NULL;	//驱动库
	m_handel = NULL;	//句柄
#ifdef _WIN64
	strcpy(m_szDllFile,"rdb4api_x64.dll");//DLL程序文件名(全路径,可不带路径);
#else
	strcpy(m_szDllFile, "rdb4api.dll");//DLL程序文件名(全路径,可不带路径);
#endif
	

	fiTB_Create			= NULL;
	fiTB_Destory		= NULL;
	fiTB_Connect		= NULL;
	fiTB_DisConnnect	= NULL;
	fiTB_IsConnnect		= NULL;

	fiTB_OptAddAct		= NULL;
	fiTB_OptAddOpt		= NULL;
	fiTB_OptDelAct		= NULL;
	fiTB_OptDelOpt		= NULL;
	fiTB_OptModPass		= NULL;
	fiTB_OptGetActors	= NULL;
	fiTB_OptGetOperators= NULL;

	fiTB_TagInport		= NULL;
	fiTB_TagGetAttr		= NULL;
	fiTB_TagQuery		= NULL;
	fiTB_TagDel			= NULL;
	
	fiTB_WriteReal		= NULL;
	fiTB_WriteHis		= NULL;
	fiTB_WriteRealVar	= NULL;
	fiTB_WriteHisVar	= NULL;
	fiTB_ReadReal		= NULL;
	fiTB_ReadRealVar	= NULL;
	fiTB_ReadRealVars	= NULL;
	fiTB_ReadHis		= NULL;
	fiTB_ReadHis_EX		= NULL;
	fiTB_ReadHisVar		= NULL;
	fiTB_GetHisSum		= NULL;
	fiTB_GetHisSumExp	= NULL;
	fiTB_GetHisSum_EX	= NULL;

	fiTB_QueryHisExp	= NULL;
	fiTB_CountHisExp	= NULL;
	fiTB_CountKstAct	= NULL;

	fiTB_VolLoad		= NULL;
	fiTB_VolDown		= NULL;
	fiTB_VolGetTab		= NULL;

	fiTB_ctrl			= NULL;
	fiTB_ReadOrigHis	= NULL;

	fiSOE_Write			= NULL;
	fiSOE_OpenRST		= NULL;
	fiSOE_GetNext		= NULL;
	fiSOE_MoveFirst		= NULL;
	fiSOE_CloseRST		= NULL;
	fiSOE_ClearAllRST	= NULL;

	m_szuser[0] = '\0';
	InitErrMsg();
}

CRdbApi::~CRdbApi()
{
	if(m_hinstLib)
	{
		TB_Destory();
		::FreeLibrary(m_hinstLib);
	}
}

void	CRdbApi::InitErrMsg() //初始化错误信息
{
	int i;
	for(i=0; i < MAX_ERR_MSG;i++)
		m_serrmsg[i][0] = '\0';
	strcpy(m_serrmsg[SE_ERR],"错误!");
	strcpy(m_serrmsg[SE_EXP],"表达式错误!");
	strcpy(m_serrmsg[SE_EXPVAR],"表达式引用变量错误");
	strcpy(m_serrmsg[SE_EXPVAROVER],"表达式引用变量太多,最多允许4个不同引用标签");

	strcpy(m_serrmsg[SE_NOTCONNECT],"没有连接RDB4实时数据库!");
	strcpy(m_serrmsg[OPT_NOACTOR],"无此角色!");
	strcpy(m_serrmsg[OPT_NOUSER],"无此账号!");
	strcpy(m_serrmsg[OPT_PASSERR],"密码错误!");
	strcpy(m_serrmsg[OPT_NOPOWER],"无此权限!");
	strcpy(m_serrmsg[OPT_USEREXIST],"账号重复!");
	strcpy(m_serrmsg[OPT_ACTOREXIST],"角色重复!");
	strcpy(m_serrmsg[OPT_ACTORUSE],"角色正在使用!");
	strcpy(m_serrmsg[OPT_NOLOGIN],"没有登录!");
	strcpy(m_serrmsg[OPT_NOTACTIVE],"账号冻结!");
	strcpy(m_serrmsg[OPT_OPTFULL],"账号满!");
	strcpy(m_serrmsg[OPT_ACTFULL],"角色满!");
	strcpy(m_serrmsg[SE_TG_NOTAG],"无此标签!");
	
	strcpy(m_serrmsg[SE_DA_TIME],"时标错误!");
	strcpy(m_serrmsg[SE_DA_TYPE],"类型错误!");
	strcpy(m_serrmsg[SE_DA_QA],"质量错误!");
	strcpy(m_serrmsg[SE_DA_NODATA],"无数据!");

	strcpy(m_serrmsg[SRV_FRMNOOD],"无此命令!");
	strcpy(m_serrmsg[SRV_FRMERR],"报文错误!");

	strcpy(m_serrmsg[VOL_ERR],"失败!");
	strcpy(m_serrmsg[VOL_VOLFLAG],"不是本库的卷,卷和库不对应!");
	strcpy(m_serrmsg[VOL_VOLERR],"卷错误,不是卷文件!");
	strcpy(m_serrmsg[VOL_FILE],"卷文件操作失败!");

	strcpy(m_serrmsg[CTRL_ERRARG],"控制接口参数错误!");
	strcpy(m_serrmsg[CTRL_NODEC],"无此控制设备!");
	strcpy(m_serrmsg[CTRL_NOTAG],"控制设备无此标签!");
	strcpy(m_serrmsg[CTRL_ERRWRITE],"控制标签不能写!");

	strcpy(m_serrmsg[CTRL_TIMEOUT],"控制超时!");
	strcpy(m_serrmsg[CTRL_COMERR],"控制通讯错误!");
	strcpy(m_serrmsg[CTRL_DRVCTRLERR],"控制驱动错误!");

	strcpy(m_serrmsg[SOE_ERR_RSET],"错误的记录集!");
	strcpy(m_serrmsg[SOE_ERR_RSETFULL],"记录集满!");
	strcpy(m_serrmsg[SOE_ERR_RSETARG],"记录集参数错误!");
	strcpy(m_serrmsg[SRV_DEMOOVER],"试用过期!");
}

BOOL	CRdbApi::GetErrMsg(T_U32 uerr,char sbuf[MAX_PATH])
{
	sbuf[0] = '\0';
	if(uerr >= MAX_ERR_MSG)
		return FALSE;
	strcpy(sbuf,m_serrmsg[uerr]);
	return TRUE;
}

void CRdbApi::GetPowStr(T_U32 dwpow,char sbuf[MAX_PATH])//取权限字符串
{
	sbuf[0] = '\0';
	if(dwpow & OPT_POW_READ)
		strcat(sbuf,"读,");
	if(dwpow & OPT_POW_WRITE)
		strcat(sbuf,"写,");
	if(dwpow & OPT_POW_WTAG)
		strcat(sbuf,"标签管理,");
		
	if(dwpow & OPT_POW_MAN)
		strcat(sbuf,"账号管理,");
	if(dwpow & OPT_POW_CTRL)
		strcat(sbuf,"设备控制,");
}

void	CRdbApi::FreeDrv()
{
	if(m_hinstLib)
	{
		TB_Destory();
		::FreeLibrary(m_hinstLib);
		m_hinstLib = NULL;
	}
}

BOOL	CRdbApi::LoadDll(const char* lpszFile)//读入驱动
{
	if(m_hinstLib != NULL || !lpszFile)
		return TRUE;
	strncpy(m_szDllFile,lpszFile,MAX_PATH-1);
	m_szDllFile[MAX_PATH-1] = '\0';

	m_hinstLib = LoadLibrary(m_szDllFile); 
	if(m_hinstLib == NULL)
		return FALSE;//没找到驱动程序
		
	fiTB_Create =			(iTB_Create)		::GetProcAddress(m_hinstLib,"TB_Create");
	fiTB_Destory =			(iTB_Destory)		::GetProcAddress(m_hinstLib,"TB_Destory");
	fiTB_Connect =			(iTB_Connect)		::GetProcAddress(m_hinstLib,"TB_Connect");
	fiTB_DisConnnect =		(iTB_DisConnnect)	::GetProcAddress(m_hinstLib,"TB_DisConnnect");
	fiTB_IsConnnect =		(iTB_IsConnnect)	::GetProcAddress(m_hinstLib,"TB_IsConnnect");

	fiTB_OptAddAct =		(iTB_OptAddAct)		::GetProcAddress(m_hinstLib,"TB_OptAddAct");
	fiTB_OptAddOpt =		(iTB_OptAddOpt)		::GetProcAddress(m_hinstLib,"TB_OptAddOpt");
	fiTB_OptDelAct =		(iTB_OptDelAct)		::GetProcAddress(m_hinstLib,"TB_OptDelAct");
	fiTB_OptDelOpt =		(iTB_OptDelOpt)		::GetProcAddress(m_hinstLib,"TB_OptDelOpt");
	fiTB_OptModPass =		(iTB_OptModPass)	::GetProcAddress(m_hinstLib,"TB_OptModPass");
	fiTB_OptGetActors =		(iTB_OptGetActors)	::GetProcAddress(m_hinstLib,"TB_OptGetActors");
	fiTB_OptGetOperators=	(iTB_OptGetOperators)	::GetProcAddress(m_hinstLib,"TB_OptGetOperators");

	fiTB_TagInport =		(iTB_TagInport)		::GetProcAddress(m_hinstLib,"TB_TagInport");
	fiTB_TagGetAttr =		(iTB_TagGetAttr)	::GetProcAddress(m_hinstLib,"TB_TagGetAttr");
	fiTB_TagQuery =			(iTB_TagQuery)		::GetProcAddress(m_hinstLib,"TB_TagQuery");
	fiTB_TagDel =			(iTB_TagDel)		::GetProcAddress(m_hinstLib,"TB_TagDel");

	fiTB_WriteReal =		(iTB_WriteReal)		::GetProcAddress(m_hinstLib,"TB_WriteReal");
	fiTB_WriteHis  =		(iTB_WriteHis)		::GetProcAddress(m_hinstLib,"TB_WriteHis");
	fiTB_WriteRealVar =		(iTB_WriteRealVar)	::GetProcAddress(m_hinstLib,"TB_WriteRealVar");
	fiTB_WriteHisVar =		(iTB_WriteHisVar)	::GetProcAddress(m_hinstLib,"TB_WriteHisVar");
	fiTB_ReadReal =			(iTB_ReadReal)		::GetProcAddress(m_hinstLib,"TB_ReadReal");
	fiTB_ReadRealVar =		(iTB_ReadRealVar)	::GetProcAddress(m_hinstLib,"TB_ReadRealVar");
	fiTB_ReadRealVars =		(iTB_ReadRealVars)	::GetProcAddress(m_hinstLib,"TB_ReadRealVars");
	fiTB_ReadHis =			(iTB_ReadHis)		::GetProcAddress(m_hinstLib,"TB_ReadHis");
	fiTB_ReadHis_EX	=		(iTB_ReadHis_EX)	::GetProcAddress(m_hinstLib,"TB_ReadHis_EX");
	fiTB_ReadHisVar =		(iTB_ReadHisVar)	::GetProcAddress(m_hinstLib,"TB_ReadHisVar");
	fiTB_GetHisSum =		(iTB_GetHisSum)		::GetProcAddress(m_hinstLib,"TB_GetHisSum");
	fiTB_GetHisSumExp =		(iTB_GetHisSumExp)	::GetProcAddress(m_hinstLib,"TB_GetHisSumExp");
	fiTB_GetHisSum_EX=		(iTB_GetHisSum_EX)	::GetProcAddress(m_hinstLib,"TB_GetHisSum_EX");
	fiTB_ReadOrigHis =		(iTB_ReadOrigHis)	::GetProcAddress(m_hinstLib,"TB_ReadOrigHis");
	fiTB_QueryHisExp =		(iTB_QueryHisExp)	::GetProcAddress(m_hinstLib,"TB_QueryHisExp");
	fiTB_CountHisExp =		(iTB_CountHisExp)	::GetProcAddress(m_hinstLib,"TB_CountHisExp");
	fiTB_CountKstAct =		(iTB_CountKstAct)	::GetProcAddress(m_hinstLib,"TB_CountKstAct");
	
	fiTB_VolLoad	=		(iTB_VolLoad)		::GetProcAddress(m_hinstLib,"TB_VolLoad");
	fiTB_VolDown	=		(iTB_VolDown)		::GetProcAddress(m_hinstLib,"TB_VolDown");
	fiTB_VolGetTab	=		(iTB_VolGetTab)		::GetProcAddress(m_hinstLib,"TB_VolGetTab");

	fiTB_ctrl		 =		(iTB_ctrl)			::GetProcAddress(m_hinstLib,"TB_ctrl");

	fiSOE_Write		=		(iSOE_Write)		::GetProcAddress(m_hinstLib,"SOE_Write");
	fiSOE_OpenRST	=		(iSOE_OpenRST)		::GetProcAddress(m_hinstLib,"SOE_OpenRST");
	fiSOE_GetNext	=		(iSOE_GetNext)		::GetProcAddress(m_hinstLib,"SOE_GetNext");
	fiSOE_MoveFirst	=		(iSOE_MoveFirst)	::GetProcAddress(m_hinstLib,"SOE_MoveFirst");
	fiSOE_CloseRST	=		(iSOE_CloseRST)		::GetProcAddress(m_hinstLib,"SOE_CloseRST");
	fiSOE_ClearAllRST	=	(iSOE_ClearAllRST)	::GetProcAddress(m_hinstLib,"SOE_ClearAllRST");

	return TRUE;
}

BOOL	CRdbApi::IsConnect()//是否连接
{
	int nc = 0;
	if(SE_OK == TB_IsConnnect(&nc))
	{
		if(nc)
			return TRUE;
	}
	return FALSE;
}

TBHANDLE CRdbApi::TB_Create()//创建对象,返回对象句柄
{
	if((m_hinstLib == NULL) || (fiTB_Create == NULL))
		return NULL;
	if(m_handel)
		return m_handel;
	m_handel = fiTB_Create();
	return m_handel;
}

T_U32	CRdbApi::TB_Destory() //注销对象，注销后h不能再用
{
	if(m_handel == NULL)
		return SE_OK;
	if((m_hinstLib == NULL) || (fiTB_Destory == NULL))
		return SE_ERR;
	fiTB_Destory(m_handel);
	m_handel = NULL;
	return SE_OK;
}

T_U32	CRdbApi::TB_Connect(
		const char *sServerIP,		//in 实时库 IP地址
		T_U16 wPort,				//in 服务端口，默认15088
		const char* lpszUser,		//in 用户账号
		const char* lpszPass		//in 密码
	)//连接
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_Connect == NULL))
		return SE_ERR;
	T_U32 uret = fiTB_Connect(m_handel,sServerIP,wPort,lpszUser,lpszPass);
	if(uret == SE_OK)
		strcpy(m_szuser, lpszUser);
	else
		m_szuser[0] = '\0';
	return uret;
}

T_U32	CRdbApi::TB_DisConnnect()//断开连接
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_DisConnnect == NULL))
		return SE_ERR;
	m_szuser[0] = '\0';
	return fiTB_DisConnnect(m_handel);
}

T_U32	CRdbApi::TB_IsConnnect(T_I32 *pret)//*pret回填 1表示已连接，0表示未连接
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_IsConnnect == NULL))
		return SE_ERR;
	return fiTB_IsConnnect(m_handel,pret);
}

T_U32	CRdbApi::TB_OptAddAct(LPOPTACTOR pa) //角色,添加和修改一体,按名字索引
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptAddAct == NULL))
		return SE_ERR;
	return fiTB_OptAddAct(m_handel,pa);
}

T_U32	CRdbApi::TB_OptAddOpt(LPOPERATOR po) //账号,添加和修改一体,按名字索引
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptAddOpt == NULL))
		return SE_ERR;
	return fiTB_OptAddOpt(m_handel,po);
}

T_U32	CRdbApi::TB_OptDelAct(const char *sname)//删除角色
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptDelAct == NULL))
		return SE_ERR;
	return fiTB_OptDelAct(m_handel,sname);
}

T_U32	CRdbApi::TB_OptDelOpt(const char *sname)//删除账号
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptDelOpt == NULL))
		return SE_ERR;
	return fiTB_OptDelOpt(m_handel,sname);
}

T_U32	CRdbApi::TB_OptModPass(const char *sname,const char*soldpass,const char *snewpass) //修改密码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptModPass == NULL))
		return SE_ERR;
	return fiTB_OptModPass(m_handel,sname,soldpass,snewpass);
}

T_U32	CRdbApi::TB_OptGetActors(T_OPTACTOR actors[],T_I32 nrecbufsize,T_I32 *pnacts)
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptGetActors == NULL))
		return SE_ERR;
	return fiTB_OptGetActors(m_handel,actors,nrecbufsize,pnacts);
}	

T_U32	CRdbApi::TB_OptGetOperators(T_OPERATOR opts[],T_I32 nrecbufsize,T_I32 *pnopts)
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_OptGetOperators == NULL))
		return SE_ERR;
	return fiTB_OptGetOperators(m_handel,opts,nrecbufsize,pnopts);
}

T_U32	CRdbApi::TB_TagInport(
		T_U32 dwflag,			//in 策略
		LPTOMTAG ptag,			//in 标签属性,已名字为索引，uid不填写
		T_I32 *pnResult			//out 回填TAGIN_XXXXXX 
	)//导入一个标签
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_TagInport == NULL))
		return SE_ERR;
	return fiTB_TagInport(m_handel,dwflag,ptag,pnResult);
}

T_U32	CRdbApi::TB_TagGetAttr(
	LPTOMTAG ptag			//in/out 输入时只填写标签名
	)//读取标签属性
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_TagGetAttr == NULL))
		return SE_ERR;
	return fiTB_TagGetAttr(m_handel,ptag);
}

T_U32	CRdbApi::TB_TagDel(const char *sname)//删除标签
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_TagDel == NULL))
		return SE_ERR;
	return fiTB_TagDel(m_handel,sname);
}

T_U32	CRdbApi::TB_TagQuery(
		const char* sname,		//名称过滤串
		const char* sdes,		//描述过滤串
		T_I32 ndatatype,		//数据类型，-1表示全部
		T_I32 ntagtype,			//标签类型,-1表示全部
		Enum_TagAttr pfun,		//回调函数
		T_PARAM lEnumparam		//回调函数的参数
	)//查询标签
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_TagQuery == NULL))
		return SE_ERR;
	return fiTB_TagQuery(m_handel,sname,sdes,ndatatype,ntagtype,pfun,lEnumparam);
}


T_U32	CRdbApi::TB_WriteReal(
	T_NAMEBASEVAL vals[],	//in/out 标签值结构数组
	T_I32 nvals				//个数
	)//写基本类型实时值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_WriteReal == NULL))
		return SE_ERR;
	return fiTB_WriteReal(m_handel,vals,nvals);
}

T_U32	CRdbApi::TB_WriteHis(
	   T_NAMEBASEVAL vals[],	//in/out 标签值结构数组
	   T_I32 nvals				//个数
	   )//写基本类型实时值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_WriteHis == NULL))
		return SE_ERR;
	return fiTB_WriteHis(m_handel,vals,nvals);
}

T_U32	CRdbApi::TB_WriteRealVar(
		const char* sname,		//标签名
		char cdatatype,			//类型DT_STRING,DT_BLOB
		T_U32 uobjtype,			//对象类型,客户端自定义
		T_TIME ltime,			//时标
		char cqa,				//质量
		T_U8 *pdata,			//数据
		T_I32 nlen				//数据长度
		)
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_WriteRealVar == NULL))
		return SE_ERR;
	return fiTB_WriteRealVar(m_handel,sname,cdatatype,uobjtype,ltime,cqa,pdata,nlen);
}
T_U32	CRdbApi::TB_WriteHisVar(
		const char* sname,		//标签名
		char cdatatype,			//类型DT_STRING,DT_BLOB
		T_U32 uobjtype,			//对象类型,客户端自定义
		T_TIME ltime,			//时标
		char cqa,				//质量
		T_U8 *pdata,			//数据
		T_I32 nlen				//数据长度
						   )
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_WriteHisVar == NULL))
		return SE_ERR;
	return fiTB_WriteHisVar(m_handel,sname,cdatatype,uobjtype,ltime,cqa,pdata,nlen);
}

T_U32	CRdbApi::TB_ReadReal(
	T_NAMEBASEVAL vals[],	//in/out 标签值结构数组,只填写标签名
	T_I32 nvals				//个数
	)//读基本类型实时值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadReal == NULL))
		return SE_ERR;
	return fiTB_ReadReal(m_handel,vals,nvals);
}

T_U32	CRdbApi::TB_ReadRealVar(
		  const char* sname,	//in 标签名
		  PVARVAL pval			//out 
		  )//读可变类型实时
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadRealVar == NULL))
		return SE_ERR;
	return fiTB_ReadRealVar(m_handel,sname,pval);
}

T_U32	CRdbApi::TB_ReadRealVars(
		T_NAMEVARVAL* pvars,	// in/out，调用前填写每个数组中sname成员
		T_I32 nvals				// in 空间个数
		)//读可变类型实时值多个
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadRealVars == NULL))
		return SE_ERR;
	return fiTB_ReadRealVars(m_handel,pvars,nvals);
}
T_U32		CRdbApi::TB_ReadHis(
		const char *sname,		// in 标签名
		T_TIME lts,				// in 开始时标
		T_TIME lte,				// in 结束时标
		T_TIME lds,				// in 间隔时间,秒0表示样本值
		T_I32 lflag,			// in 标志
		T_BASEVAL vals[],		// out 接收数据空间
		T_I32 nsize,			// in 空间大小
		T_I32 *pnrecs,			// out 实际记录数
		T_I32 *pnend			// out 是否结束，0表示还有数据，非0表示结束
		)
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadHis == NULL))
		return SE_ERR;
	return fiTB_ReadHis(m_handel,sname,lts,lte,lds,lflag,vals,nsize,pnrecs,pnend);
}

T_U32	CRdbApi::TB_ReadHis_EX(
		T_NAMEBASEVAL vals[],	//in/out 标签值结构数组,只填写标签名
		T_TIME ltime,			//in 时标
		T_I32 nsize				//个数
		)//读多点同时刻历史数据
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadHis_EX == NULL))
		return SE_ERR;
	int i;
	for(i=0;i< nsize;i++)
	{
		vals[i].val.cerr =  SE_DA_NODATA;
		vals[i].val.u64 = 0;
	}
	return fiTB_ReadHis_EX(m_handel,vals,ltime,nsize);
}

T_U32  CRdbApi::TB_ReadHisVar(
		const char* sname,		//in 标签名
		T_TIME ltime,			//时标
		T_U32 dwflag,			//标志 VAR_CUR,VAR_AFTER,VAR_DEFAULT之一
		PVARVAL pval			//out 
		)//取可变类型历史
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadHisVar == NULL))
		return SE_ERR;
	return fiTB_ReadHisVar(m_handel,sname,ltime,dwflag,pval);
}

T_U32  CRdbApi::TB_GetHisSum(
		const char* sname,		//in 标签名
		T_TIME lts,				//in 开始时间
		T_TIME lte,				//in 结束时间
		PBASEVAL pMin,			//out 最小值和发生时间
		PBASEVAL pMax,			//out 最大值和发生时间
		double *pavg,			//out 平均值
		double *psum,			//out 累加值
		T_I32 *plrecs			//out 样本值记录个数
		)//读统计值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_GetHisSum == NULL))
		return SE_ERR;
	return fiTB_GetHisSum(m_handel,sname,lts,lte,pMin,pMax,pavg,psum,plrecs);
}

T_U32  CRdbApi::TB_GetHisSumExp(
		const char* sname,		//in 标签名
		const char *sexp,		//in 表达式
		T_TIME lts,				//in 开始时间
		T_TIME lte,				//in 结束时间
		PBASEVAL pMin,			//out 最小值和发生时间
		PBASEVAL pMax,			//out 最大值和发生时间
		double *pavg,			//out 平均值
		double *psum,			//out 累加值
		T_I32 *plrecs			//out 样本值记录个数
		)//读统计值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_GetHisSum == NULL))
		return SE_ERR;
	return fiTB_GetHisSumExp(m_handel,sname,sexp,lts,lte,pMin,pMax,pavg,psum,plrecs);
}

T_U32  CRdbApi::TB_GetHisSum_EX(
		const char* sname,		//in 标签名
		T_TIME lts,				//in 开始时间
		T_TIME lte,				//in 结束时间
		T_TIME lds,				//in 分组时间(秒)，比如1小时就填写3600
		TSUMVAL groupvals[],	//out 分组统计值空间,调用前不用填写其内容
		T_I32 nbufsize,			//in 分组统计值空间大小，组数个数
		T_I32 *psize			//out 实际统计的组数
		)//读分组统计值,每次调用最大分组数512

{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_GetHisSum_EX == NULL))
		return SE_ERR;
	return fiTB_GetHisSum_EX(m_handel,sname,lts,lte,lds,groupvals,nbufsize,psize);
}


T_U32	CRdbApi::TB_QueryHisExp(
		const char *sname,		// in 标签名
		const char *sexp,		// in 表达式
		T_TIME lts,				// in 开始时标
		T_TIME lte,				// in 结束时标
		T_TIME lds,				// in 间隔时间,秒0表示样本值
		T_I32 lflag,			// in 标志
		T_BASEVAL vals[],		// out 接收数据空间
		T_I32 nsize,			// in 空间大小
		T_I32 *pnrecs,			// out 实际记录数
		T_I32 *pnend			// out 是否结束，0表示还有数据，非0表示结束
		)
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_QueryHisExp == NULL))
		return SE_ERR;
	return fiTB_QueryHisExp(m_handel,sname,sexp,lts,lte,lds,lflag,vals,nsize,pnrecs,pnend);
}

T_U32	CRdbApi::TB_CountHisExp(
		const char* sname,		// in 标签名
		const char *sexp,		// in 表达式
		T_TIME lts,				// in 开始时间
		T_TIME lte,				// in 结束时间
		T_TIME *pltime,			// out 时间值
		T_I32 *plrecs			// out 样本值记录个数
		)//读统计值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_CountHisExp == NULL))
		return SE_ERR;
	return fiTB_CountHisExp(m_handel,sname,sexp,lts,lte,pltime,plrecs);
}
T_U32	CRdbApi::TB_CountKstAct(
		const char *sname,		//in 标签名
		T_TIME lts,				//in 开始时间
		T_TIME lte,				//in 结束时间
		T_I32 nlval,			//in 分状态值，一般为0
		T_I32 nhval,			//in 合状态值，一般为1
		T_I32 *pnl2h,			//out 分到合的跳变次数
		T_I32 *pnh2l			//out合到分的跳变次数
		)//标签名开关动作统计
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_CountKstAct == NULL))
		return SE_ERR;
	return fiTB_CountKstAct(m_handel,sname,lts,lte,nlval,nhval,pnl2h,pnh2l);
}

T_U32	CRdbApi::TB_VolLoad(T_I32 nvol)//加载卷
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_VolLoad == NULL))
		return SE_ERR;
	return fiTB_VolLoad(m_handel,nvol);
}
T_U32	CRdbApi::TB_VolDown(T_I32 nvol)//卸载卷
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_VolDown == NULL))
		return SE_ERR;
	return fiTB_VolDown(m_handel,nvol);
}
T_U32	CRdbApi::TB_VolGetTab(T_VOLREC precs[],T_I32 nbufsize,T_I32 *pnsize)//读取全部卷表
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_VolGetTab == NULL))
		return SE_ERR;
	return fiTB_VolGetTab(m_handel,precs,nbufsize,pnsize);
}

T_U32	CRdbApi::TB_ctrl(
		const char* sip,		//in 目的地IP地址，指Tomdac的地址
		T_U16 wport,			//in 控制端口,指Tomdac的控制服务UDP端口
		const char* sdec,		//in 控制设备，指Tomdac中的设备名
		const char* stag,		//in 控制标签, 指Tomdac中设备的驱动标签名。
		T_I32 ndatatype,		//in 数据类型 DT_INT32等
		T_I32 ndatasize,		//in 控制数据的字节数,最大400字节
		unsigned char* pdata,	//in 控制数据
		T_U8 *pdrverrcode,		//out 回填驱动错误码，成功填写0，其他由驱动自己定义
		char sdrverrinfo[96],	//out 当pdrverrcode被回填有错误码时，sdrverrinfo回填驱动错误信息。
		T_I32 *pretdatasize,	//out 返回数据字节数
		T_U8 pretdata[400]		//out 返回数据
		) //控制,返回错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ctrl == NULL))
		return SE_ERR;
	return fiTB_ctrl(m_handel,sip,wport,sdec,stag,ndatatype,ndatasize,pdata,pdrverrcode,sdrverrinfo,pretdatasize,pretdata);
}

T_U32	CRdbApi::TB_ReadOrigHis(
			T_NAMEBASEVAL vals[],	//in/out 标签值结构数组,只填写标签名
			T_TIME ltime,			//in 时标
			T_I32 nsize,			//in 个数
			T_I32 nflag				//in ATTIME_AFTER，ATTIME_BEFORE
	)//读多点历史数据样本值
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiTB_ReadOrigHis == NULL))
		return SE_ERR;
	return fiTB_ReadOrigHis(m_handel,vals,ltime,nsize,nflag);
}

T_U32	CRdbApi::SOE_Write(
			T_SOE* psoe		//in SOE结构指针
			) // 写SOE,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_Write == NULL))
		return SE_ERR;
	return fiSOE_Write(m_handel,psoe);
}
T_U32	CRdbApi::SOE_OpenRST(
		T_TIME ts,			//in 开始时标
		T_TIME te,			//in 结束时标
		char *sexp,			//in 查询表达式
		T_I32 *pnrst		//out 回填记录集号
		) //打开SOE记录集,成功打开后自动处于第一条位置处,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_OpenRST == NULL))
		return SE_ERR;
	return fiSOE_OpenRST(m_handel,ts,te,sexp,pnrst);
}
T_U32	CRdbApi::SOE_GetNext(
		T_I32	nrst,		//in 记录集号
		T_SOE*	psoe		//out soe结构
		) //读取下一个SOE,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_GetNext == NULL))
		return SE_ERR;
	return fiSOE_GetNext(m_handel,nrst,psoe);
}
T_U32	CRdbApi::SOE_MoveFirst(
		T_I32 nrst			//in 记录集号
		) // 记录集游标移动到头部,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_MoveFirst == NULL))
		return SE_ERR;
	return fiSOE_MoveFirst(m_handel,nrst);
}
T_U32	CRdbApi::SOE_CloseRST(
		T_I32 nrst			//in 记录集号
		) // 关闭记录集,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_CloseRST == NULL))
		return SE_ERR;
	return fiSOE_CloseRST(m_handel,nrst);
}
T_U32	CRdbApi::SOE_ClearAllRST() // 关闭所有记录集,返回SOE错误码
{
	if((m_hinstLib == NULL) || (m_handel == NULL) || (fiSOE_ClearAllRST == NULL))
		return SE_ERR;
	return fiSOE_ClearAllRST(m_handel);
}