Changeset 24 for trunk/Modules/ChiyuAccessControlers
- Timestamp:
- Nov 19, 2012, 12:31:19 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/ChiyuAccessControlers/UTerminalBF630.pas
r23 r24 6 6 7 7 uses 8 Classes, SysUtils, UBinarySerializer, UCommTCPServer, UCommPin, SyncObjs, DateUtils, 9 SpecializedList, SpecializedStream, Forms, UCommTCPClient, Dialogs; 8 Classes, SysUtils, UBinarySerializer, UCommTCPServer, UCommPin, SyncObjs, 9 SpecializedList, SpecializedStream, Forms, UCommTCPClient, Dialogs, DateUtils, 10 UAccessControler, UAttendance; 10 11 11 12 const … … 45 46 toAntiDuressGet = $97, toSrcurityBypassGetStatus = $98, 46 47 toTimeSet = $a6, toTimeZoneAdd = $a7, toGroupAdd = $a8, 47 toDoorSettingSet = $a9, toHolidayAdd = $aa, toDesignationAdd = $ac, 48 toDoorSettingSet = $a9, toHolidayAdd = $aa, 49 toUserAdd = $ab, toDesignationAdd = $ac, toDepartmentAdd = $ad, 48 50 toTimeDel = $b6, toTimeZoneDel = $b7, toGroupDel = $b8, 49 51 toSecurityBypass = $b9, toHolidayDel = $ba, 50 toDesignationDel = $bc, toDepartment Add= $bd,52 toDesignationDel = $bc, toDepartmentDel = $bd, 51 53 toTimeDelAll = $c6, toZimeZoneDelAll = $c7, toGroupDelAll = $c8, 52 54 toHolidayDelAll = $ca, toDesignationDelAll = $cc, toDepartmentDelAll = $cd, … … 59 61 { TTerminalBF630 } 60 62 61 TTerminalBF630 = class 63 TTerminalBF630 = class(TAccessControler) 62 64 private 63 FActive: Boolean;64 65 ReceiveData: TListByte; 65 66 ReceiveDataThread: TListByte; … … 67 68 SendTime: TDateTime; 68 69 procedure ReceiveDataHandler(Sender: TCommPin; AList: TListByte); 69 procedure SetActive(AValue: Boolean);70 70 procedure WaitForBytes(Count: Integer); 71 protected 72 procedure SetActive(AValue: Boolean); override; 71 73 public 72 Id: Integer;73 74 CommSocket: TCommTCPClient; 74 Pin: TCommPin;75 75 AccessKey: array[0..5] of Byte; 76 76 UseAccessKey: Boolean; … … 80 80 function SendPacket(Command: TTerminalOperation; Request: TListByte = nil; Response: TListByte = nil): Byte; 81 81 function ReadDateTime: TDateTime; 82 function GetUserCount: Integer; 83 property Active: Boolean read FActive write SetActive;84 constructor Create; 82 function GetUserCount: Integer; override; 83 function GetUser(Id: Integer; User: TUser): Boolean; override; 84 constructor Create; override; 85 85 destructor Destroy; override; 86 86 end; … … 102 102 TVirtualTerminalBF630User = class 103 103 Id: Integer; 104 Name: string; 104 FirstName: string; 105 SecondName: string; 105 106 end; 106 107 … … 139 140 resourcestring 140 141 SCommunicatiomTimeout = 'Communication timeout'; 142 SWrongId = 'Wrong id'; 141 143 142 144 { TVirtualTerminalBF630Session } … … 155 157 Command: Byte; 156 158 ResultCode: Byte; 157 OldPos: Integer;158 159 begin 159 160 with Parent do … … 182 183 Dec(DataLength, 6); 183 184 end; 184 Request.Count := 0; 185 Request.AddListPart(List, RecDataSerializer.Position, DataLength - 10); 186 OldPos := Position; 185 if (DataLength - 10) > 0 then begin 186 Request.Count := 0; 187 ReadList(Request, 0, DataLength - 10); 188 end; 187 189 ExpectedCheckSum := 0; 188 for I := 0 to OldPos- 1 do190 for I := 0 to Position - 1 do 189 191 ExpectedCheckSum := (ExpectedCheckSum + List[I]) and $ff; 190 Position := OldPos;191 192 CheckSum := ReadByte; // byte sum from ACK to DATA 192 193 if CheckSum <> ExpectedCheckSum then Exit; … … 282 283 var 283 284 UserId: Integer; 284 RequestStream: TMemoryStreamByte; 285 ResponseStream: TMemoryStreamByte; 286 S: TStream; 287 begin 288 try 289 RequestStream := TMemoryStreamByte.Create(Request); 290 ResponseStream := TMemoryStreamByte.Create(Response); 291 //UserId := RequestStream.ReadInteger; 292 //ResponseStream.WriteInteger(UserId); 293 finally 294 RequestStream.Free; 295 ResponseStream.Free; 285 RequestSerializer: TBinarySerializer; 286 ResponseSerializer: TBinarySerializer; 287 I: Integer; 288 begin 289 try 290 RequestSerializer := TBinarySerializer.Create; 291 RequestSerializer.List := Request; 292 UserId := RequestSerializer.ReadInteger; 293 294 ResponseSerializer := TBinarySerializer.Create; 295 ResponseSerializer.List := Response; 296 with ResponseSerializer do begin 297 WriteInteger(UserId); 298 WriteByte(0); 299 WriteByte(0); 300 for I := 0 to 7 do WriteByte(0); 301 with TVirtualTerminalBF630User(Users[UserId]) do 302 for I := 1 to 15 do 303 if I < Length(FirstName) then WriteByte(Ord(FirstName[I])) 304 else WriteByte(0); 305 with TVirtualTerminalBF630User(Users[UserId]) do 306 for I := 1 to 15 do 307 if I < Length(SecondName) then WriteByte(Ord(SecondName[I])) 308 else WriteByte(0); 309 end; 310 finally 311 RequestSerializer.Free; 312 ResponseSerializer.Free; 296 313 end; 297 314 end; … … 300 317 begin 301 318 if FActive = AValue then Exit; 302 FActive := AValue;319 inherited; 303 320 CommSocket.Active := AValue; 304 321 end; … … 331 348 with TVirtualTerminalBF630User(Users.AddNew(TVirtualTerminalBF630User.Create)) do begin 332 349 Id := Users.Count; 333 Name := 'User ' + IntToStr(Id); 350 FirstName := 'User ' + IntToStr(Id); 351 SecondName := 'Second ' + IntToStr(Id); 334 352 end; 335 353 with TVirtualTerminalBF630User(Users.AddNew(TVirtualTerminalBF630User.Create)) do begin 336 354 Id := Users.Count; 337 Name := 'User ' + IntToStr(Id); 355 FirstName := 'User ' + IntToStr(Id); 356 SecondName := 'Second ' + IntToStr(Id); 338 357 end; 339 358 end; … … 352 371 destructor TVirtualTerminalBF630.Destroy; 353 372 begin 354 CommSocket.Free; 355 Users.Free; 356 Sessions.Free; 373 Active := False; 374 FreeAndNil(CommSocket); 375 FreeAndNil(Users); 376 FreeAndNil(Sessions); 357 377 inherited Destroy; 358 378 end; … … 372 392 procedure TTerminalBF630.SetActive(AValue: Boolean); 373 393 begin 374 if FActive = AValue then Exit; 394 if Active = AValue then Exit; 395 inherited; 375 396 CommSocket.Address := Address; 376 397 CommSocket.Port := Port; 377 398 CommSocket.Active := AValue; 378 FActive := AValue;379 399 end; 380 400 … … 410 430 SendDataSerializer: TBinarySerializer; 411 431 SendDataStream: TMemoryStream; 412 ResponseData: TListByte;432 // ResponseData: TListByte; 413 433 ResponseSerializer: TBinarySerializer; 414 434 CheckSum: Byte; … … 417 437 DataLength: Cardinal; 418 438 DataByte: Byte; 419 OldPos: Integer;420 439 begin 421 440 try … … 424 443 SendDataSerializer.List := SendData; 425 444 SendDataStream := TMemoryStream.Create; 426 ResponseData := TListByte.Create;445 //ResponseData := TListByte.Create; 427 446 ResponseSerializer := TBinarySerializer.Create; 428 ResponseSerializer.List := Re sponseData;447 ResponseSerializer.List := ReceiveData; 429 448 430 449 // Clear buffer … … 448 467 WriteByte(Id); // TID 449 468 WriteByte(Byte(Command)); 450 if Assigned(Request) then 469 if Assigned(Request) then begin 451 470 SendDataSerializer.WriteList(Request, 0, Request.Count); 471 end; 452 472 if UseAccessKey then begin 453 473 WriteByte(AccessKey[0]); … … 471 491 with ReceiveData, ResponseSerializer do begin 472 492 Endianness := enBig; 493 ExpectedCheckSum := 0; 473 494 WaitForBytes(8); 474 495 DataByte := ReadByte; … … 478 499 if ReadByte <> Id then raise Exception.Create('Wrong Id'); // STX 479 500 Result := ReadByte; 480 OldPos := Position;481 501 if Assigned(Response) then begin 502 WaitForBytes(DataLength - 10); 482 503 Response.Count := 0; 483 WaitForBytes(DataLength - 10); 484 485 Response.AddListPart(ReceiveData, 0, DataLength - 10); 504 Response.AddListPart(ReceiveData, Position, DataLength - 10); 505 Position := Position + (DataLength - 10); 486 506 end; 487 OldPos := Position; 488 ExpectedCheckSum := 0; 489 for I := 0 to OldPos - 1 do 507 WaitForBytes(2); 508 for I := 0 to Position - 1 do 490 509 ExpectedCheckSum := (ExpectedCheckSum + ReceiveData[I]) and $ff; 491 Position := OldPos;492 WaitForBytes(2);493 510 CheckSum := ReadByte; // byte sum from BS to DATA 494 if CheckSum <> ExpectedCheckSum then raise Exception.Create('Bad receive checksum'); 495 if ReadByte <> CodeETX then raise Exception.Create('Expected ETX'); // ETX 511 if CheckSum <> ExpectedCheckSum then 512 raise Exception.Create('Bad receive checksum'); 513 if ReadByte <> CodeETX then 514 raise Exception.Create('Expected ETX'); // ETX 496 515 end; 497 516 end; 498 517 finally 499 518 ResponseSerializer.Free; 500 ResponseData.Free;519 //ResponseData.Free; 501 520 SendDataStream.Free; 502 521 SendDataSerializer.Free; … … 531 550 end; 532 551 552 function TTerminalBF630.GetUser(Id: Integer; User: TUser): Boolean; 553 var 554 Request: TListByte; 555 RequestSerializer: TBinarySerializer; 556 Response: TListByte; 557 I: Integer; 558 Title: Integer; 559 Department: Integer; 560 begin 561 try 562 Response := TListByte.Create; 563 Request := TListByte.Create; 564 RequestSerializer := TBinarySerializer.Create; 565 RequestSerializer.List := Request; 566 RequestSerializer.WriteInteger(Id); 567 SendPacket(toUserDataGet, Request, Response); 568 if Response[0] <> Id then raise Exception.Create(SWrongId); 569 Title := Response[4]; 570 Department := Response[5]; 571 User.FirstName := ''; 572 for I := 0 to 14 do begin 573 if Response[14 + I] < 32 then Break; 574 User.FirstName := User.FirstName + Chr(Response[14 + I]); 575 end; 576 User.SecondName := ''; 577 for I := 0 to 14 do begin 578 if Response[29 + I] < 32 then Break; 579 User.SecondName := User.SecondName + Chr(Response[29 + I]); 580 end; 581 582 finally 583 Request.Free; 584 RequestSerializer.Free; 585 Response.Free; 586 end; 587 end; 588 533 589 constructor TTerminalBF630.Create; 534 590 begin 591 inherited; 535 592 ReceiveData := TListByte.Create; 536 593 ReceiveDataThread := TListByte.Create; 537 594 ReceiveDataLock := TCriticalSection.Create; 538 595 CommSocket := TCommTCPClient.Create(nil); 539 Pin := TCommPin.Create;540 596 Pin.Connect(CommSocket.Pin); 541 597 Pin.OnReceive := ReceiveDataHandler; 542 598 Timeout := 3 * OneSecond; 543 Id := 1; 599 Address := 'localhost'; 600 Port := 2000; 544 601 end; 545 602 546 603 destructor TTerminalBF630.Destroy; 547 604 begin 548 Pin.Free;549 CommSocket.Free;550 ReceiveDataLock.Free;551 ReceiveDataThread.Free;552 ReceiveData.Free;605 Active := False; 606 FreeAndNil(CommSocket); 607 FreeAndNil(ReceiveDataLock); 608 FreeAndNil(ReceiveDataThread); 609 FreeAndNil(ReceiveData); 553 610 inherited Destroy; 554 611 end;
Note:
See TracChangeset
for help on using the changeset viewer.