⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u_mainthread.pas

📁 中控800型号考勤机的自动采集数据程序。能够手动采集
💻 PAS
字号:
unit u_MainThread;

interface
//{$DEFINE DEBUG}

uses
  Classes, ADODB, Windows, Forms, SysUtils, zkemkeeper_TLB,CommonUnit,ShareUnit;
type
  TLoadThread = Class(TThread)
  private
    CZKEM1: TCZKEM;
    aAdoCon: TAdoConnection;
    aAttendPunch: TAdoQuery;
    aTemp: TAdoQuery;
    IP: string;
    Port: integer;
    PassWord: integer;
    cErrorCode: integer;
    CurUDevID: UDevID;
    DevID: integer;
    Spec: string;
    { Private declarations }
    //获取网络ID
    function GetDeviceInfo(cDevID:integer):boolean;
    //连接机器
    function ConnectDevice(aIP: string;cDevID,aPort,aPwd: integer):boolean;
    //开始采集数据
    procedure BeginCollData;
    //检查数据是否存在存在则不添加至数据库
    function DataIfExists(aDevID,aEmpID: integer;aDate: TDateTime): boolean;
  protected
    procedure Execute; override;
  public
    constructor Create(aDevID: UDevID;aConnectR: UConnectRecord);
    destructor  Destroy; override;
  end;
implementation
uses u_ChildLoadData;
function TLoadThread.DataIfExists(aDevID,aEmpID: integer;aDate: TDateTime): boolean;
begin
 with atemp do
 begin
  if active then close;
  SQL.text := 'select count(*) from AttendPunch where DeviceID='+IntToStr(aDevID)+' and EmpID='+IntToStr(aEmpID)+' and '
             +' PunchTime='''+DateTimeToStr(aDate)+''''+' and Direction=1 and DealWith=0';
  Open;
  Result := (Fields[0].AsInteger>0);
  close;
 end;
end;
//开始采集数据
procedure TLoadThread.BeginCollData;
var
  dwTMachine, dwEnrollNumber,dwEMachineNumber,dwVerifyMode: Integer;
  dwInOutMode,dwYear,dwMonth,dwDay,dwHour,dwMinute: Integer;
  s: string;
  f: tfilestream;
  aFileName,aBackFileName: string;
  aDateTime: String;
  atemp: TDateTime;
  i: integer;
  aFileExists: boolean;
  adevindex: integer;
  aEmpID1,aEmpID2,aEmpID3: integer;
begin
 for adevindex :=0 to High(CurUDevID)  do
 begin
 DevID := CurUDevID[adevindex];
 aFileName := ExePath+'DataLog\'+ForMatDateTime('yyyymmdd',Date)+Copy(ForMatDateTime('hhmmss',Time),1,2)+'-'+IntToStr(DevID)+'.bak';
 aBackFileName := ExePath+'BackData\'+ForMatDateTime('yyyymmdd',Date)+Copy(ForMatDateTime('hhmmss',Time),1,2)+'-'+IntToStr(DevID)+'.bak';
 if FileExists(aFileName) then
  aFileExists := true
 else
  aFileExists := false;
 if GetDeviceInfo(DevID) then
  begin
   if ConnectDevice(IP,DevID,Port,PassWord) then
    begin
    i := 0;
    if not aFileExists then
    f:=tfilestream.Create(aFileName, fmOpenWrite or fmCreate);
    try
    if CZKEM1.ReadGeneralLogData(DevId) then
    while CZKEM1.GetGeneralLogData(DevId,dwTMachine, dwEnrollNumber,dwEMachineNumber,dwVerifyMode,
      dwInOutMode,dwYear,dwMonth,dwDay,dwHour,dwMinute) do
    begin
     aEmpID1 := dwEnrollNumber;
     aEmpID2 := dwEnrollNumber;
     aEmpID3 := dwEnrollNumber;
     if not aFileExists then
     begin
      s:=IntToStr(aEmpID1)+','+IntToStr(dwYear)+'-'+IntToStr(dwMonth)+'-'+IntToStr(dwDay)
         +' '+IntToStr(dwHour)+':'+IntToStr(dwMinute)+':00'+#13#10;
      f.Write(s[1],length(s));
     end;
     aDateTime := IntToStr(dwYear)+'-'+IntToStr(dwMonth)+'-'+IntToStr(dwDay)+' '+IntToStr(dwHour)+':'+IntToStr(dwMinute)+':00';
     if tryStrToDateTime(aDateTime,atemp) then
      begin
       if not DataIfExists(DevID,aEmpID2,aTemp) then
       begin
       with aAttendPunch do
        begin
         if active then close;
         SQL.Text := 'Insert Into AttendPunch (DeviceID,EmpID,PunchTime,Direction,DealWith) Values ('
                    +IntToStr(DevID)+','+IntToStr(aEmpID3)+','+''''+DateTimeToStr(aTemp)+''''+','
                    +'1'+','+'0)';
         ExecSQL;
        end;
       end; //end insert database if
       inc(i);
       frm_ChildLoadData.AddInfo_List1(2,DevID,'','',1,i,i,1);
      end;
    end;
    finally
      frm_ChildLoadData.AddInfo_List1(2,DevID,'','',1,i,i,2);
      f.Free;
    end;
    end; //end while
  end//end getdevinfo
  else
  frm_ChildLoadData.AddInfo(ForMatDateTime('hh:mm:ss',now),'Get Record 0!',2);
   if ClearUpData then
   begin
   CZKEM1.ClearGLog(DevID);
   end;
  CZKEM1.Disconnect;
 end; //end for CurUDevID;
end;
//连接机器
function TLoadThread.ConnectDevice(aIP: string;cDevID,aPort,aPwd: integer):boolean;
var aResult: boolean;
begin
 Result := false;
 frm_ChildLoadData.AddInfo_List1(1,cDevID,'','',0,0,0,0);
 cErrorCode := 8888;
 try
 CZKEM1.SetCommPassword(aPwd);
 except
  aResult := false;
  frm_ChildLoadData.AddInfo(ForMatDateTime('hh:mm:ss',now),'Set PassWord Error!',2);
 end;
 aResult := CZKEM1.Connect_Net(aIP,aPort);
 //if aResult then
//frm_ChildLoadData.AddInfo(ForMatDateTime('hh:mm:ss',now),'Connect Success!',1);
 if not aResult then
  begin
   CZKEM1.GetLastError(cErrorCode);
   frm_ChildLoadData.AddInfo(ForMatDateTime('hh:mm:ss',now),'Disconnect ErrorCode'+IntToStr(cErrorCode),2);
   frm_ChildLoadData.AddInfo_List1(1,cDevID,'','',2,0,0,0);
  end
 else
  frm_ChildLoadData.AddInfo_List1(2,cDevID,'','',1,0,0,0);
  //ReadDeviceStatus;

 Result := aResult;
end;
//获取网络参数
function TLoadThread.GetDeviceInfo(cDevID: integer):boolean;
begin
 result := false;
 with aTemp do
 begin
  if active then close;
  SQL.Text := 'select IP,Port,sysPwd,Spec from Device where DeviceID='+IntToStr(cDevID);
  Open;
  if not eof then
  begin
   IP := Fields[0].AsString;
   Port := Fields[1].AsInteger;
   PassWord := Fields[2].AsInteger;
   Spec := Fields[3].AsString;
   Result := true;
  end;
 end;
end;
//采集线程建立
constructor TLoadThread.Create(aDevID: UDevID;aConnectR: UConnectRecord);
begin
 inherited create(false);
 //aAdoCon := aFouseAdoConnect;
 aAdoCon := TAdoConnection.Create(nil);
 aAdoCon.LoginPrompt := false;
 aAdoCon.KeepConnection := true;
 try
    if aAdoCon.Connected then aAdoCon.Close;
    //Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=TestDB;Data Source=IT014
    aAdoCon.ConnectionString:='Provider=SQLOLEDB.1;'
                              +'Persist Security Info=False;'
                              +'Initial Catalog='+trim(aConnectR.DataBase)+';'
                              +'User ID='+trim(aConnectR.LoginName)+';'
                              +'Password='+trim(aConnectR.LoginPwd)+';'
                              +'Data Source='+trim(aConnectR.ServerName);
    aAdoCon.Open;
  except
    frm_ChildLoadData.AddInfo(ForMatDateTime('hh:mm:ss',now),'Connected DataBas Server Error!',2);
  end;
 //临时用处ado
 aTemp := TAdoQuery.Create(nil);
 aTemp.CursorLocation := clUseServer;
 aTemp.Connection := aAdoCon;
 //数据ado
 aAttendPunch := TAdoQuery.Create(nil);
 aAttendPunch.CursorLocation := clUseServer;
 aAttendPunch.Connection := aAdoCon;
 //设备编号
 CurUDevID := aDevID;
 CZKEM1 := TCZKEM.Create(nil);
end;
//采集线程释放
destructor TLoadThread.Destroy;
begin
 inherited Destroy;
end;
//采集线程执行
procedure TLoadThread.Execute;
begin
BeginCollData;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -