📄 deviceaccessunit.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include <math.h>
#include "DeviceAccessUnit.h"
#include <Classes.hpp>
#include <vcl.h>
//---------------------------------------------------------------------------
lpMXInitDevice MXInitDevice;
lpMXCloseDevice MXCloseDevice;
lpMXSetDeviceTime MXSetDeviceTime;
lpMXUpLog MXUpLog;
lpMXUpUserInfo MXUpUserInfo;
lpMXDownUserInfo MXDownUserInfo;
lpMXUpTemplate MXUpTemplate;
lpMXDownTemplate MXDownTemplate;
lpMXDeleteUser MXDeleteUser;
lpMXClearUser MXClearUser;
lpMXClearLog MXClearLog;
lpMXSetCaption MXSetCaption;
lpMXResumeCaption MXResumeCaption;
lpMXSetAutoLock MXSetAutoLock;
lpMXGetAutoLock MXGetAutoLock;
lpMXSetDoorMode MXSetDoorMode;
lpMXGetDoorMode MXGetDoorMode;
lpMXGetUserCount MXGetUserCount;
lpMXGetTemplateCount MXGetTemplateCount;
lpMXGetLogCount MXGetLogCount;
lpMXEmergencyOpen MXEmergencyOpen;
lpMXSetEmergencyOpenPwd MXSetEmergencyOpenPwd;
lpMXSetRoleData MXSetRoleData;
lpMXGetRoleData MXGetRoleData;
//---------------------------------------------------------------------------
TDeviceAccess::TDeviceAccess(char* iDeviceLibFile)
{
LibHandle = NULL;
}
//---------------------------------------------------------------------------
TDeviceAccess::TDeviceAccess()
{
aRV = OK;
LibHandle = NULL;
TempID = 1;
if(!InitDLL())
{
//Application->MessageBoxA("初始化动态库文件失败!","提示信息",MB_OK + MB_ICONERROR);
exit(0);
}
}
//---------------------------------------------------------------------------
TDeviceAccess::~TDeviceAccess()
{
FreeDLL();
}
//---------------------------------------------------------------------------
bool TDeviceAccess::InitDLL()
{
LibHandle = LoadLibrary("FTA600_DLLT.dll"); //装载FTA600_DLLT.dll, 得到该库句柄
if(LibHandle == NULL)
return false;
MXInitDevice = (lpMXInitDevice)GetProcAddress(LibHandle,"InitDevice");
MXCloseDevice = (lpMXCloseDevice)GetProcAddress(LibHandle,"CloseDevice");
MXSetDeviceTime = (lpMXSetDeviceTime)GetProcAddress(LibHandle,"SetDeviceTime");
MXUpLog = (lpMXUpLog)GetProcAddress(LibHandle,"UpLog");
MXUpUserInfo = (lpMXUpUserInfo)GetProcAddress(LibHandle,"UpUserInfo");
MXDownUserInfo = (lpMXDownUserInfo)GetProcAddress(LibHandle,"DownUserInfo");
MXUpTemplate = (lpMXUpTemplate)GetProcAddress(LibHandle,"UpTemplate");
MXDownTemplate = (lpMXDownTemplate)GetProcAddress(LibHandle,"DownTemplate");
MXDeleteUser = (lpMXDeleteUser)GetProcAddress(LibHandle,"DeleteUser");
MXClearUser = (lpMXClearUser)GetProcAddress(LibHandle,"ClearUser");
MXClearLog = (lpMXClearLog)GetProcAddress(LibHandle,"ClearLog");
MXSetCaption = (lpMXSetCaption)GetProcAddress(LibHandle,"SetCaption");
MXResumeCaption = (lpMXResumeCaption)GetProcAddress(LibHandle,"ResumeCaption");
MXSetAutoLock = (lpMXSetAutoLock)GetProcAddress(LibHandle,"SetAutoLock");
MXGetAutoLock = (lpMXGetAutoLock)GetProcAddress(LibHandle,"GetAutoLock");
MXSetDoorMode = (lpMXSetDoorMode)GetProcAddress(LibHandle,"SetDoorMode");
MXGetDoorMode = (lpMXGetDoorMode)GetProcAddress(LibHandle,"GetDoorMode");
MXGetUserCount = (lpMXGetUserCount)GetProcAddress(LibHandle,"GetUserCount");
MXGetTemplateCount = (lpMXGetTemplateCount)GetProcAddress(LibHandle,"GetTemplateCount");
MXGetLogCount = (lpMXGetLogCount)GetProcAddress(LibHandle,"GetLogCount");
MXEmergencyOpen = (lpMXEmergencyOpen)GetProcAddress(LibHandle,"EmergencyOpen");
MXSetEmergencyOpenPwd = (lpMXSetEmergencyOpenPwd)GetProcAddress(LibHandle,"SetEmergencyOpenPwd");
MXSetRoleData = (lpMXSetRoleData)GetProcAddress(LibHandle,"SetRoleData");
MXGetRoleData = (lpMXGetRoleData)GetProcAddress(LibHandle,"GetRoleData");
return true;
}
bool TDeviceAccess::FreeDLL()
{
if (LibHandle)
FreeLibrary(LibHandle);
return true;
}
//---------------------------------------------------------------------------
int TDeviceAccess::OpenDevice(int iDeviceID)
{
//打开USB设备
int lHandle = 0;
if(!MXInitDevice(iDeviceID,3,0,0,"",0,&lHandle))
return lHandle;
return 0;
}
//---------------------------------------------------------------------------
int TDeviceAccess::OpenDevice(int iDeviceID,int iCom,int iBaudRate)
{
//打开串口设备
int lHandle = 0;
if(!MXInitDevice(iDeviceID,1,iCom,iBaudRate,"",0,&lHandle))
return lHandle;
return 0;
}
//---------------------------------------------------------------------------
int TDeviceAccess::OpenDevice(int iDeviceID,char* iIP,unsigned int iPort)
{
//打开IP口设备
int lHandle = 0;
if(!MXInitDevice(iDeviceID,2,0,0,iIP,iPort,&lHandle))
return lHandle;
return 0;
}
//---------------------------------------------------------------------------
int TDeviceAccess::OpenDevice(int iDeviceID,DeviceType_T iType,DeviceParam_T Param)
{
//得到参数信息
int lDeviceHandle = 0;
switch(iType)
{
case dtCOM:
{
short int lCOMID = Param.UartParam.COMID;
short int lBaud = Param.UartParam.Baud;
lDeviceHandle = OpenDevice(iDeviceID,lCOMID,lBaud);
}break;
case dtIP:
{
AnsiString lIPStr = AnsiString(Param.IPParam.IPs[0])+"."+AnsiString(Param.IPParam.IPs[1])+"."+AnsiString(Param.IPParam.IPs[2])+"."+AnsiString(Param.IPParam.IPs[3]);
lDeviceHandle = OpenDevice(iDeviceID,lIPStr.c_str(),Param.IPParam.Port);
}break;
case dtUSB:
{
lDeviceHandle = OpenDevice(iDeviceID);
}break;
}
return lDeviceHandle;
}
//---------------------------------------------------------------------------
int TDeviceAccess::CloseDevice(int iHandle)
{
int lRet = MXCloseDevice(iHandle);
return lRet;
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetDeviceTime(int iHandle,SYSTEMTIME iTime)
{
AnsiString lTime,lYear,lMonth,lDay,lHour,lMinute,lSecond;
lYear = iTime.wYear;
lMonth = iTime.wMonth;
lDay = iTime.wDay;
lHour = iTime.wHour;
lMinute = iTime.wMinute;
lSecond = iTime.wSecond;
lTime = lYear.SubString(3,2);
if(lMonth.Length() == 1)
lTime = lTime + "0";
lTime = lTime + lMonth;
if(lDay.Length() == 1)
lTime = lTime + "0";
lTime = lTime + lDay;
if(lHour.Length() == 1)
lTime = lTime + "0";
lTime = lTime + lHour;
if(lMinute.Length() == 1)
lTime = lTime + "0";
lTime = lTime + lMinute;
if(lSecond.Length() == 1)
lTime = lTime + "0";
lTime = lTime + lSecond;
return MXSetDeviceTime(iHandle,lTime.c_str());
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetCaption(int iHandle,char* iCaption,int iCaptionSize)
{
return MXSetCaption(iHandle,iCaption,iCaptionSize);
}
//---------------------------------------------------------------------------
int TDeviceAccess::ResumeCaption(int iHandle)
{
return MXResumeCaption(iHandle);
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetEntanceRole(int iHandle,BYTE* iRoleData,int iRoleDataSize)
{
//设置设备门禁规则数据
return MXSetRoleData(iHandle,iRoleData,iRoleDataSize);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetEntanceRole(int iHandle,BYTE* oRoleData,int &oRoleDataSize)
{
//得到设备门禁规则数据
return MXGetRoleData(iHandle,oRoleData,&oRoleDataSize);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetLogInfo(int iHandle,int iLogID,LogInfoBase_T &oLogInfo)
{
//得到日志信息
AnsiString lYear,lMonth,lDay,lHour,lMinute,lSecond;
char lDateTime[12]={0};
char lType;
int lRet;
lRet = MXUpLog(iHandle,iLogID,&oLogInfo.MainEmpID,&oLogInfo.ObjEmpID,&lType,lDateTime);
if(lRet == DevNoExist)
return lRet;//不存在记录
if(lRet != OK)
return lRet;//错误
oLogInfo.DeviceLogID = iLogID;//直接从传进来的参数
oLogInfo.OpType = lType;
AnsiString sDateTime = lDateTime;
lYear = "20";
lYear = sDateTime.SubString(1,2);
lMonth = sDateTime.SubString(3,2);
lDay = sDateTime.SubString(5,2);
lHour = sDateTime.SubString(7,2);
lMinute = sDateTime.SubString(9,2);
lSecond = sDateTime.SubString(11,2);
oLogInfo.EventTime.wYear = lYear.ToInt() + 2000;
oLogInfo.EventTime.wMonth = lMonth.ToInt();
oLogInfo.EventTime.wDay = lDay.ToInt();
oLogInfo.EventTime.wHour = lHour.ToInt();
oLogInfo.EventTime.wMinute = lMinute.ToInt();
oLogInfo.EventTime.wSecond = lSecond.ToInt();
return lRet;
}
//---------------------------------------------------------------------------
int TDeviceAccess::ClearLogInfo(int iHandle)
{
return MXClearLog(iHandle);
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetUserInfo(int iHandle,UserInfo_T iUserInfo)
{
int lLen = 40;
BYTE DataD[40]={0};
memcpy(DataD,&iUserInfo.UserID,4);
//用户名
AnsiString lUserName = iUserInfo.EmpName;
int lNameLen = lUserName.Length();
memcpy(DataD+4,iUserInfo.EmpName,lNameLen);
DataD[18] = iUserInfo.ManageClass;
DataD[19] = '0';
DataD[20] = iUserInfo.DeptID; // 用户所属部门,部门ID整型还没转换
DataD[21] = iUserInfo.AttendPlan; // 用户允许开门时间段
DataD[22] = iUserInfo.VerifyMode; // 用户身份验证方式,支持多种身份验证方式
DataD[23] = iUserInfo.FingerCount; // 已添加指纹总数
int lPwd;
lPwd = AnsiString(iUserInfo.EmpPwd).ToInt();
memcpy(DataD+24,&lPwd,4);
return MXDownUserInfo(iHandle,DataD,lLen);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetUserInfo(int iHandle,int iUserID,UserInfo_T &iUserInfo)
{
char lInfo[40]={0};
int lLen = 0;
int lRet,lPwd,i;
lRet = MXUpUserInfo(iHandle,iUserID,lInfo,&lLen);
if(lRet == 9)
return lRet;//不存在记录
if(lRet != 0)
return lRet;//错误
memcpy(&iUserInfo.UserID,lInfo,4);
//用户名
char lName[14]={0};
AnsiString lTrimName;
memcpy(lName,lInfo+4,14);
strcpy(iUserInfo.EmpName,lName);
iUserInfo.ManageClass = lInfo[18];
if(lInfo[21] < 0)
lInfo[21] = 0;
iUserInfo.AttendPlan = lInfo[21];
iUserInfo.DeptID = lInfo[20]; // 用户所属部门,部门ID整型还没转换
iUserInfo.VerifyMode = lInfo[22]; // 用户身份验证方式,支持多种身份验证方式
iUserInfo.FingerCount = lInfo[23]; // 已添加指纹总数
memcpy(&lPwd,lInfo+24,4);
if(lPwd == -1)//-1表示密码为空
{
strcpy(iUserInfo.EmpPwd,"-1");
iUserInfo.IsUsePwd = upNoUse;
}
else
{
strcpy(iUserInfo.EmpPwd,AnsiString(lPwd).c_str());
iUserInfo.IsUsePwd = upUse;
}
return lRet;
}
//---------------------------------------------------------------------------
int TDeviceAccess::DownTemplate(int iHandle,int iUserID,BYTE* iTemplate,int iTemplateSize)
{
return MXDownTemplate(iHandle,iUserID,iTemplate,iTemplateSize);
}
//---------------------------------------------------------------------------
int TDeviceAccess::UpTemplate(int iHandle,int iTemplateID,int &oUserID,BYTE *oTemplate,int &oTemplateSize)
{
return MXUpTemplate(iHandle,iTemplateID,&oUserID,oTemplate,&oTemplateSize);
}
//---------------------------------------------------------------------------
int TDeviceAccess::DeleteUser(int iHandle,int iUserID)
{
//删除用户
return MXDeleteUser(iHandle,iUserID);
}
//---------------------------------------------------------------------------
int TDeviceAccess::ClearUser(int iHandle)
{
//清除用户
return MXClearUser(iHandle);
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetAutoLock(int iHandle,int iSecond)
{
//设置自动锁时间(秒数)
return MXSetAutoLock(iHandle,iSecond);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetAutoLock(int iHandle,int &oSecond)
{
//读取自动锁时间(秒数)
return MXGetAutoLock(iHandle,&oSecond);
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetDoorMode(int iHandle,int iMode)
{
//设置开门模式,0常开,1常闭,2正常
if(iMode == dmLongOpenMode)
iMode = 0;
if(iMode == dmLongCloseMode)
iMode = 1;
if(iMode == dmNormalMode)
iMode = 2;
return MXSetDoorMode(iHandle,iMode);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetDoorMode(int iHandle,int &oMode)
{
//读取开门模式
int lRet = MXGetDoorMode(iHandle,&oMode);
if(oMode == 0)
oMode = dmLongOpenMode;
if(oMode == 1)
oMode = dmLongCloseMode;
if(oMode == 2)
oMode = dmNormalMode;
return lRet;
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetUserCount(int iHandle,int &oCount)
{
//得到员工总数
return MXGetUserCount(iHandle,&oCount);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetTemplateCount(int iHandle,int &oCount)
{
//得到指纹总数
return MXGetTemplateCount(iHandle,&oCount);
}
//---------------------------------------------------------------------------
int TDeviceAccess::GetLogCount(int iHandle,int &oCount)
{
//得到日志总数
return MXGetLogCount(iHandle,&oCount);
}
//---------------------------------------------------------------------------
int TDeviceAccess::EmergencyOpen(int iHandle,int iPwd)
{
//应急开门(后跟4字节,6位密码)
return MXEmergencyOpen(iHandle,iPwd);
}
//---------------------------------------------------------------------------
int TDeviceAccess::SetEmergencyOpenPwd(int iHandle,int nPwd)
{
//应急开门密码(后跟4字节,6位密码)
return MXSetEmergencyOpenPwd(iHandle,nPwd);
}
//---------------------------------------------------------------------------
static TDeviceAccess* pDeviceAccess = NULL;
TDeviceAccess* GetDeviceAccessInstance()
{
if (pDeviceAccess == NULL)
{
pDeviceAccess = new TDeviceAccess();
}
return pDeviceAccess;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -