-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomBoardProcess.pas
902 lines (841 loc) · 25.2 KB
/
CustomBoardProcess.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
unit CustomBoardProcess;
interface
uses WinApi.Windows, Winapi.Messages, System.Classes, System.SysUtils, System.Threading,
ExtentionsList, BoardProcessInterface, AbstractBoard, AbstractProtocol, AbstractExtention, ExtentionsLibrary,
EventBusInterface, Vodopad.EventList, Vodopad.ObjectList, CustomChildProcess,
ManualControlFormAccessInterface, LoggerInterface, Vodopad.Timer;
type
TMCLib = class(TObject)
strict private
FWindowHandle: HWND;
procedure FreeFormLibrary;
procedure MessageHandler(var message : TMessage);
public
FormLibrary : HMODULE;
FormInstance : TComponent;
procedure OnControlFormDestroy(Sender: TObject);
constructor Create();
destructor Destroy;override;
end;
TCustomBoardProcess = class(TCustomChildProcess,
IBoardProcess,
IEventBus,
ICreateManualControlForm,
IBoardProcessLog)
private
fProtocol : TAbstractProtocolClass;
fProticolLib : HMODULE;
fSearchTimer, fExtractTmpTimer : TvdTimer;
fFormLib : TMCLib;
fExtentions : TExtentions;
fBoards : TExObjectList;
fTmpBoardsList : TExObjectList;
fEventSubscribers : TCustomObjEventList;
procedure StartSearchInternal(Sender : TObject);
procedure OnExtreactTmp(Sender : TObject);
procedure SearchTimeOut(Sender : TObject);
procedure InitProtocolLib;
procedure DoneProtocolLib;
procedure UpdateBoardConfig(ABoard: TAbstractBoard);
procedure CallOnSearchComplete;
protected
procedure AfterCreate; override;
procedure BeforeDestroy; override;
procedure BoardEvent(Sender : TObject; Event : TGUID; Params : Pointer); virtual;
function QueryInterface(const IID: TGUID; out Obj): HResult; override; stdcall;
{IBoardProcess}
function GetItems(AIndex : byte): TObject; stdcall;
{IEventBus}
procedure IEventBus.Add = EventMethodAdd;
procedure IEventBus.Remove = EventMethodRemove;
procedure EventMethodAdd(const AMethod : EventBusInterface.TCustomObjEvent); stdcall;
procedure EventMethodRemove(const AMethod: EventBusInterface.TCustomObjEvent); stdcall;
function GetManualControlFormClass(out oModule:HMODULE; out oFormClass : TComponentClass):Boolean;virtual;
{ICreateManualControlForm}
function ICreateManualControlForm.Create = ICreateManualControlForm_Create;
function ICreateManualControlForm_Create(var alreadyExists : boolean) : TComponent;stdcall;
property EventSubscribers : TCustomObjEventList read fEventSubscribers;
public
procedure Log(ALevel: TLogInfo; const Text: WideString; Instance: TObject=nil);stdcall;
function LevelEnabled(ALevel : TLogInfo): boolean; stdcall;
{IBoardProcess}
procedure ForEachBoard(AMethod : TForEachBoardMethod; Params : Pointer=nil); stdcall;
procedure StartSearch; stdcall;
function BoardsCount : byte; stdcall;
function PositionsCount: cardinal; stdcall;
function ActivePositionsCount: cardinal; stdcall;
procedure DeleteBoard(AIndex : byte); virtual; stdcall;
procedure Clear; virtual; stdcall;
property Board[index : byte] : TObject read GetItems; default;
function IndexOf(const ABoard : TObject) : integer; overload;
function IndexOf(const ABoardSN : word) : integer; overload;
procedure DeleteInactivePositions;stdcall;
procedure SetAllPositionsToState(AActive : boolean);stdcall;
end;
implementation
uses
System.IniFiles,
AbstractMainProcessInterface,
AbstractBoardInterface,
RS232Phisycal, RS232Utils,
PositionListInterface,
PositionInterface,
FormsControllerInterface;
type
TPackageUnload = procedure;
TPackageLoad = procedure;
var
vgBoardLogger : ILogger = nil;
function InitLogger(AFunct : pLoggerFunct; out Obj):boolean;
var
vConfig : pLogClassConfig;
begin
Result := False;
if not Assigned(AFunct) then
Exit;
New(vConfig);
try
vConfig.ConfigGUID := StringToGUID('{50581E3A-4D39-4EB9-B3C5-45766BB22644}');
vConfig.EnabledLevels := [lInfo, lEvent, lDebug, {lPortWrite,} lPortRead, lTrace, lWarning, lError,
lException, lExceptionOS];
vConfig.DefaultLevelsView := [lInfo, lEvent, lDebug, lTrace, lWarning, lError, lException, lExceptionOS];
vConfig.FileExtension := 'brdlog';
vConfig.ArhiveExtension := 'brdlog7z';
vConfig.ModuleName := 'Ïëàòû';
try
Result := AFunct.AddLogger(vConfig, Obj);
except
Result := false;
end;
finally
Dispose(vConfig);
end;
end;
function GetProtocolClass(AMainProcess : TObject; out AClass:TAbstractProtocolClass):HMODULE;
var
vCC : IConfigController;
vBuildFile : TMemIniFile;
vLibFileName : TFileName;
func: function: TAbstractProtocolClass; stdcall;
begin
Result := 0;
if not Supports(AMainProcess, IConfigController, vCC) then
Exit;
vBuildFile := TMemIniFile.Create(''{, TEncoding.UTF8});
try
vCC.Load('Software', vBuildFile);
try
if vBuildFile.ValueExists('BuildConfiguration','BoardProtocolPlugin') then
begin
vLibFileName := ExtractFilePath(ParamStr(0))+vBuildFile.ReadString('BuildConfiguration','BoardProtocolPlugin','');
if not FileExists(vLibFileName) then
Exit;
Result := SafeLoadLibrary(vLibFileName,
SEM_NOOPENFILEERRORBOX or SEM_FAILCRITICALERRORS or SEM_NOGPFAULTERRORBOX);
if Result = 0 then
Exit;
@func := GetProcAddress(Result, 'GetBoardProtocolClass');
if not Assigned(func) then
begin
FreeLibrary(Result);
Result := 0;
Exit;
end;
AClass := func;
end;
except
if Result<>0 then
begin
FreeLibrary(Result);
Result := 0;
end;
end;
finally
FreeAndNil(vBuildFile);
vCC := nil;
end;
end;
{TCustomBoardProcess}
{$REGION ' internal types '}
type
TSetAllStateParams = record
Active : Boolean;
end;
pSetAllStateParams = ^TSetAllStateParams;
{$ENDREGION}
{$REGION ' internal functions '}
procedure InternalDeleteInactivePositions(const ABoard: TObject; BoardIndex : byte; Params : Pointer); stdcall;
var
vBoard : IAbstractBoard;
vPosList : IPositionsList;
vPosition : IPosition;
i : Word;
begin
if Supports(ABoard, IAbstractBoard, vBoard)
and vBoard.QueryPositionListInterface(IPositionsList, vPosList)
and (vPosList.Count > 0) then
begin
for I := vPosList.Count - 1 downto 0 do
begin
if vPosList.QueryItem(i, IPosition, vPosition) then
begin
if not vPosition.Active then
vPosList.Delete(i);
vPosition := nil;
end else
vPosList.Delete(i);
end;
end;
vPosList := nil;
vBoard := nil;
end;
procedure InternalSetAllPositionsToState(const ABoard: TObject; BoardIndex : byte; Params : Pointer); stdcall;
var
vBoard : IAbstractBoard;
vPosList : IPositionsList;
vPosition : IPosition;
i : Word;
vParams : pSetAllStateParams;
begin
if not Assigned(Params) then
Exit;
vParams := pSetAllStateParams(Params);
if Supports(ABoard, IAbstractBoard, vBoard)
and vBoard.QueryPositionListInterface(IPositionsList, vPosList)
and (vPosList.Count > 0) then
begin
for I := 0 to vPosList.Count - 1 do
begin
if vPosList.QueryItem(i, IPosition, vPosition) then
vPosition.Active := vParams.Active;
vPosition := nil;
end;
end;
vPosList := nil;
vBoard := nil;
end;
{$ENDREGION}
constructor TMCLib.Create;
begin
FWindowHandle := System.Classes.AllocateHWND(MessageHandler);
FormInstance:= nil;
end;
destructor TMCLib.Destroy;
var
vFormOnDestroyEvent : IFormOnDestroyEvent;
begin
if Supports(FormInstance, IFormOnDestroyEvent, vFormOnDestroyEvent) then
begin
vFormOnDestroyEvent.OnDestroy := nil;
vFormOnDestroyEvent := nil;
end;
FreeFormLibrary;
FormInstance := nil;
System.Classes.DeallocateHWND(FWindowHandle);
inherited Destroy;
end;
procedure TMCLib.FreeFormLibrary;
var
vUnloadProc :TPackageUnload;
begin
if (FormLibrary > 0) then
begin
@vUnloadProc := GetProcAddress(FormLibrary, 'Finalize');
if Assigned(vUnloadProc) then
vUnloadProc;
FreeLibrary(FormLibrary);
end;
FormLibrary := 0;
end;
procedure TMCLib.MessageHandler(var message: TMessage);
begin
if message.Msg = WM_USER+8 then
FreeFormLibrary;
end;
procedure TMCLib.OnControlFormDestroy(Sender: TObject);
begin
FormInstance := nil;
Winapi.Windows.PostMessage(FWindowHandle, WM_USER+8, 0, 0);
end;
procedure TCustomBoardProcess.AfterCreate;
begin
fSearchTimer := TvdTimer.Create(self);
fExtractTmpTimer := TvdTimer.Create(self);
fFormLib := TMCLib.Create;
InitLogger(fFunct, vgBoardLogger);
fBoards := TExObjectList.Create;
fTmpBoardsList := TExObjectList.Create;
fEventSubscribers := TCustomObjEventList.Create;
fExtentions := TExtentions.Create(self);
fProticolLib := 0;
fProtocol := nil;
end;
procedure TCustomBoardProcess.BeforeDestroy;
begin
fExtentions.Clear;
self.clear;
FreeAndNil(fBoards);
FreeAndNil(fTmpBoardsList);
FreeAndNil(fEventSubscribers);
DoneProtocolLib;
vgBoardLogger := nil;
FreeAndNil(fFormLib);
end;
procedure TCustomBoardProcess.EventMethodAdd(const AMethod: EventBusInterface.TCustomObjEvent);
begin
fEventSubscribers.Add(AMethod);
end;
procedure TCustomBoardProcess.EventMethodRemove(const AMethod: EventBusInterface.TCustomObjEvent);
begin
fEventSubscribers.Remove(AMethod);
end;
procedure TCustomBoardProcess.ForEachBoard(AMethod: TForEachBoardMethod; Params : Pointer);
var
vIdx : Byte;
begin
if (fBoards.Count < 1) or (not Assigned(AMethod)) then
Exit;
for vIdx := fBoards.Count-1 downto 0 do
AMethod(Board[vIdx], vIdx, Params);
end;
procedure TCustomBoardProcess.Clear;
begin
try
while (BoardsCount > 0) do
DeleteBoard(BoardsCount - 1);
except
end;
end;
procedure TCustomBoardProcess.DeleteBoard(AIndex: byte);
var
vParams : pCBPEventParams;
vIndex : pByte;
vSN : Word;
begin
if AIndex >=fBoards.Count then
Exit;
New(vParams);
New(vIndex);
try
vParams.Board := Board[AIndex];
vSN := TAbstractBoard(vParams.Board).SerialNum;
if Assigned(vgBoardLogger) then
vgBoardLogger.Log(lDebug, Format('Delete board index:%d, SN:%d',[AIndex, vSN]));
vIndex^ := AIndex;
vParams.Params := vIndex;
fEventSubscribers.Execute(Self, C_BeforeBoardDelete, vParams);
fBoards.Delete(AIndex);
vParams.Board := nil;
if Assigned(vgBoardLogger) then
vgBoardLogger.Log(lDebug, Format('Board index:%d, SN:%d - deleted',[AIndex, vSN]));
finally
Dispose(vParams);
Dispose(vIndex);
if (fBoards.Count < 1) then
begin
{fPosListMandatoryExtentions.Clear;
fBoardMandatoryExtentions.Clear; }
DoneProtocolLib;
end;
end;
end;
procedure TCustomBoardProcess.DeleteInactivePositions;
begin
ForEachBoard(InternalDeleteInactivePositions, nil);
end;
procedure TCustomBoardProcess.SetAllPositionsToState(AActive: boolean);
var
vParams : pSetAllStateParams;
begin
New(vParams);
try
vParams.Active := AActive;
ForEachBoard(InternalSetAllPositionsToState, vParams);
finally
Dispose(vParams);
end;
end;
procedure TCustomBoardProcess.CallOnSearchComplete;
begin
fEventSubscribers.Execute(Self, C_SearchComplete, nil);
end;
procedure TCustomBoardProcess.BoardEvent(Sender : TObject; Event : TGUID; Params : Pointer);
var
vIndex : integer;
vPlgIdx : Word;
vParams : pCBPEventParams;
vExtClasses : TExtentionsClasses;
vSysInfo : pSysInfoStruct;
vPortIf : IRS232Phisycal;
begin
New(vParams);
try
vParams.Board := Sender;
vParams.Params := Params;
if IsEqualGUID(Event, C_OnConnected) then
begin
vIndex := IndexOf(vParams.Board);
if (vIndex = -1) then
begin
if (fTmpBoardsList.IndexOf(vParams.Board) <> -1) then
fBoards.Add(fTmpBoardsList.Extract(vParams.Board))
else
fBoards.Add(vParams.Board);
New(vSysInfo);
try
TAbstractBoard(vParams.Board).Protocol.FillSysInfo(vSysInfo);
TAbstractBoard(vParams.Board).Positions.MaxCount := vSysInfo.MaxPositionsCount;
Log(
lEvent, Format('Board connected. '+
'Model Code:%d; Protocol description:%s; '+
'Hardware version:%d; Firmware version:%d; SN:%d',
[vSysInfo.ModelCode, vSysInfo.ProtocolDestription,
vSysInfo.HwVer, vSysInfo.FwVer, vSysInfo.Serial]));
if vSysInfo.LoadedModulesCount > 0 then
begin
Log(
lEvent, Format('Board SN:%d Loaded modules:%s',
[vSysInfo.Serial, vSysInfo.LoadedModules]));
end;
finally
Dispose(vSysInfo);
end;
{ while vPlgIdx < fBoardMandatoryExtentions.Count do
begin
TAbstractBoard(vParams.Board).Extentions.Install(fBoardMandatoryExtentions[vPlgIdx]);
Inc(vPlgIdx);
end;
vPlgIdx := 0;
while vPlgIdx < fPosListMandatoryExtentions.Count do
begin
TAbstractBoard(vParams.Board).Positions.Extentions.Install(fPosListMandatoryExtentions[vPlgIdx]);
Inc(vPlgIdx);
end; }
TAbstractBoard(vParams.Board).Protocol.FillBoardExtentionsClasses(vExtClasses);
if Length(vExtClasses) > 0 then
for vPlgIdx := Low(vExtClasses) to High(vExtClasses) do
TAbstractBoard(vParams.Board).Extentions.Install(vExtClasses[vPlgIdx]);
TAbstractBoard(vParams.Board).Protocol.FillPositionsListExtentionsClasses(vExtClasses);
if Length(vExtClasses) > 0 then
for vPlgIdx := Low(vExtClasses) to High(vExtClasses) do
TAbstractBoard(vParams.Board).Positions.Extentions.Install(vExtClasses[vPlgIdx]);
fEventSubscribers.Execute(Self, C_BoardAdded, vParams);
if fTmpBoardsList.Count < 1 then
begin
fSearchTimer.Enabled := false;
Log(lEvent, 'All boards found');
CallOnSearchComplete;
end;
end;
UpdateBoardConfig(TAbstractBoard(vParams.Board));
end else
if IsEqualGUID(Event, C_OnDisconnected) then
begin
Log(
lWarning, Format('Board SN:%d disconnected.',
[TAbstractBoard(vParams.Board).SerialNum]));
end else
if IsEqualGUID(Event, C_OnConnectionLost) then
begin
Log(
lWarning, Format('Board SN:%d connection lost.',
[TAbstractBoard(vParams.Board).SerialNum]));
end else
if IsEqualGUID(Event, C_OnConnectionRestore) then
begin
Log(
lEvent, Format('Board SN:%d connection restored.',
[TAbstractBoard(vParams.Board).SerialNum]));
end else
if IsEqualGUID(Event, C_OnSystemChanged) then
begin
if Supports(vParams.Board, IRS232Phisycal, vPortIf) then
begin
Log(lWarning,
Format('Ïëàòà íà ïîðòó COM%d çàìåíåíà. Îïðîñ ïîðòà îñòàíîâëåí',[vPortIf.Port]));
vPortIf := nil;
end;
vIndex := self.IndexOf(vParams.Board);
if vIndex <> -1 then
begin
TTask.Run(
procedure
begin
TThread.Queue(nil,
procedure
begin
try
self.DeleteBoard(vIndex);
except
end;
end
);
end
);
end;
end else
if IsEqualGUID(Event, C_OnConnectFail) then
begin
if Supports(vParams.Board, IRS232Phisycal, vPortIf) then
begin
Log(lWarning,
Format('Ïëàòà èç ñïèñêà ïîèñêà íà ïîðòó COM%d íå íàéäåíà',[vPortIf.Port]));
vPortIf := nil;
end;
vIndex := fTmpBoardsList.IndexOf(vParams.Board);
if vIndex <> -1 then
begin
fExtractTmpTimer.Tag := vIndex;
fExtractTmpTimer.Enabled := True;
end;
end else
if IsEqualGUID(Event, C_ListReaded) then
begin
Log(lEvent, Format('Board SN:%d readed from database %d positions.',
[TAbstractBoard(vParams.Board).SerialNum, TAbstractBoard(vParams.Board).Positions.Count]));
end;
fEventSubscribers.Execute(Self, Event, vParams);
finally
Dispose(vParams);
SetLength(vExtClasses, 0);
end;
end;
procedure TCustomBoardProcess.UpdateBoardConfig(ABoard: TAbstractBoard);
var
vCC : IConfigController;
vIni : TMemIniFile;
vSt : TStringList;
begin
if not Supports(self.Owner, IConfigController, vCC) then
begin
vCC := nil;
exit;
end;
vIni := TMemIniFile.Create('');
vSt := TStringList.Create;
try
vCC.Load('BoardsList', vIni);
vIni.WriteInteger(IntToStr(ABoard.SerialNum), 'PositionsCount', ABoard.Positions.MaxCount);
vCC.Save('BoardsList', vIni);
ABoard.Protocol.SaveDBConfig(vSt);
vCC.Save(Format('Board%d',[ABoard.SerialNum]), vSt);
finally
FreeAndNil(vSt);
FreeAndNil(vIni);
vCC := nil;
end;
end;
function TCustomBoardProcess.BoardsCount: byte;
begin
result := byte(fBoards.Count);
end;
function TCustomBoardProcess.PositionsCount : cardinal;
var
i : integer;
vAbstractBoard : IAbstractBoard;
vPositions : IPositionsList;
begin
result := 0;
i := 0;
while (i < BoardsCount) do
begin
if Supports(Board[i], IAbstractBoard, vAbstractBoard)
and vAbstractBoard.QueryPositionListInterface(IPositionsList, vPositions) then
Inc(result, vPositions.Count);
vPositions := nil;
vAbstractBoard := nil;
Inc(i);
end;
end;
function TCustomBoardProcess.ActivePositionsCount: cardinal;
var
i : integer;
vAbstractBoard : IAbstractBoard;
vPositions : IPositionsList;
begin
result := 0;
i := 0;
while (i < BoardsCount) do
begin
if Supports(Board[i], IAbstractBoard, vAbstractBoard)
and vAbstractBoard.QueryPositionListInterface(IPositionsList, vPositions) then
Inc(result, vPositions.ActiveCount);
vPositions := nil;
vAbstractBoard := nil;
Inc(i);
end;
end;
function TCustomBoardProcess.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
Result := inherited QueryInterface(IID, Obj);
if (Result <> S_OK) and Supports(fExtentions, IID, Obj) then
Result := S_OK;
end;
function TCustomBoardProcess.GetItems(AIndex: byte): TObject;
begin
Result := fBoards.Items[AIndex];
end;
function TCustomBoardProcess.IndexOf(const ABoard: TObject): integer;
begin
result := 0;
while (result < fBoards.Count) and (Board[result] <> ABoard) do
inc(result);
if (result = fBoards.Count) then
result := -1;
end;
function TCustomBoardProcess.IndexOf(const ABoardSN: word): integer;
begin
result := 0;
while (result < fBoards.Count) and (TAbstractBoard(Board[result]).SerialNum <> ABoardSN) do
inc(result);
if (result = fBoards.Count) then
result := -1;
end;
function TCustomBoardProcess.GetManualControlFormClass(out oModule: HMODULE; out oFormClass: TComponentClass): Boolean;
var
searchResult : TSearchRec;
GetClassFunc : function : TComponentClass; stdcall;
vCurrPath : string;
begin
result := false;
vCurrPath := ExtractFilePath(ParamStr(0));
if FindFirst(vCurrPath+'*.bpl', faAnyFile, searchResult) = 0 then
try
repeat
try
oModule := SafeLoadLibrary(vCurrPath + searchResult.Name,
SEM_NOOPENFILEERRORBOX or SEM_FAILCRITICALERRORS or SEM_NOGPFAULTERRORBOX);
if (oModule > 0) then
begin
@GetClassFunc := GetProcAddress(oModule, 'GetGetSimpleManualControlFormClass');
if Assigned(GetClassFunc) then
begin
oFormClass := GetClassFunc;
result := Assigned(oFormClass);
if result then
Break
else
FreeLibrary(oModule);
end else
FreeLibrary(oModule);
end;
except
if (oModule > 0) then
FreeLibrary(oModule);
Continue;
end;
until FindNext(searchResult) <> 0;
finally
FindClose(searchResult);
if not result then
begin
oModule := 0;
oFormClass := nil;
end;
end;
end;
function TCustomBoardProcess.ICreateManualControlForm_Create(var alreadyExists : boolean): TComponent;
var
vFormClass: TComponentClass;
vLoadProc : TPackageLoad;
vFormOnDestroyEvent : IFormOnDestroyEvent;
begin
alreadyExists := assigned(fFormLib.FormInstance);
if alreadyExists then
begin
Result := fFormLib.FormInstance;
exit;
end;
if GetManualControlFormClass(fFormLib.FormLibrary, vFormClass) then
begin
@vLoadProc := GetProcAddress(fFormLib.FormLibrary, 'Initialize');
if Assigned(vLoadProc) then
try
vLoadProc;
except on e : Exception do
begin
Self.Log(lWarning, Format('Excetion "%s" message:"%s"',[e.ClassName, e.Message]));
raise e;
end;
end;
fFormLib.FormInstance := vFormClass.Create(self);
if Supports(fFormLib.FormInstance, IFormOnDestroyEvent, vFormOnDestroyEvent) then
begin
vFormOnDestroyEvent.OnDestroy := fFormLib.OnControlFormDestroy;
vFormOnDestroyEvent := nil;
end;
Result := fFormLib.FormInstance;
end else
result := nil;
end;
procedure TCustomBoardProcess.InitProtocolLib;
var
vLoadProc: TPackageLoad;
begin
if fProticolLib = 0 then
begin
fProticolLib := GetProtocolClass(Owner, fProtocol);
if fProticolLib = 0 then
exit;
@vLoadProc := GetProcAddress(fProticolLib, 'Initialize');
if Assigned(vLoadProc) then
vLoadProc;
end;
end;
function TCustomBoardProcess.LevelEnabled(ALevel: TLogInfo): boolean;
begin
Result := Assigned(vgBoardLogger) and vgBoardLogger.LevelEnabled(ALevel);
end;
procedure TCustomBoardProcess.Log(ALevel: TLogInfo; const Text: WideString; Instance: TObject);
var
vBoard : IAbstractBoard;
begin
if not Assigned(vgBoardLogger) then
Exit;
if not vgBoardLogger.LevelEnabled(ALevel) then
Exit;
if Supports(Instance, IAbstractBoard, vBoard) then
vgBoardLogger.Log(ALevel, Format('Board SN:%d Message: %s',[vBoard.SerialNum, Text]))
else if Assigned(Instance) then
vgBoardLogger.Log(ALevel, Instance.ClassName +' '+ Text)
else
vgBoardLogger.Log(ALevel, Text);
vBoard := nil;
end;
procedure TCustomBoardProcess.DoneProtocolLib;
var
vUnloadProc: TPackageUnload;
begin
if fProticolLib <> 0 then
begin
@vUnloadProc := GetProcAddress(fProticolLib, 'Finalize');
try
if Assigned(vUnloadProc) then
vUnloadProc;
finally
FreeLibrary(fProticolLib);
fProticolLib := 0;
fProtocol := nil;
end;
end;
end;
procedure TCustomBoardProcess.StartSearch;
begin
fSearchTimer.Interval := 10;
fSearchTimer.OnTimer := StartSearchInternal;
fSearchTimer.Enabled := True;
fExtractTmpTimer.Interval := 10;
fExtractTmpTimer.OnTimer := OnExtreactTmp;
fExtractTmpTimer.Enabled := False;
fExtractTmpTimer.Tag := -1;
end;
procedure TCustomBoardProcess.StartSearchInternal(Sender: TObject);
var
vPortList : TStringList;
i : integer;
FHandl: THandle;
vPort : Byte;
vBoard: TAbstractBoard;
vBoardEB : IEventBus;
vPortIf : IRS232Phisycal;
begin
fSearchTimer.Interval := 20000;
fSearchTimer.OnTimer := SearchTimeOut;
fSearchTimer.Enabled := True;
fEventSubscribers.Execute(Self, C_SearchStart, nil);
vPortList := TStringList.Create();
try
EnumComPorts(vPortList);
fTmpBoardsList.Clear;
if vPortList.Count < 1 then
Exit;
if fProticolLib = 0 then
InitProtocolLib;
if fProticolLib = 0 then
begin
Log(lWarning, 'No protocol library found');
CallOnSearchComplete;
fSearchTimer.Enabled := false;
Exit;
end;
i := 0;
while (i < vPortList.Count) do
begin
FHandl := CreateFile(PWideChar('\\.\' + vPortList[i]),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if FHandl <> INVALID_HANDLE_VALUE then
begin
CloseHandle(FHandl);
vBoard := TAbstractBoard.Create(Self, fProtocol);
if Supports(vBoard, IEventBus, vBoardEB) then
begin
vBoardEB.Add(BoardEvent);
if Supports(vBoard, IRS232Phisycal, vPortIf) then
begin
vPort := ComNameToByte(vPortList[i]);
Log(lEvent, Format('Test protorcol "%s" on port COM%d',[fProtocol.ProtocolName, vPort]));
if vPortIf.Connect(vPort) then
begin
vPortIf := nil;
vBoardEB := nil;
fTmpBoardsList.Add(vBoard);
end else
begin
vPortIf := nil;
vBoardEB := nil;
FreeAndNil(vBoard);
end;
end else
begin
vBoardEB := nil;
FreeAndNil(vBoard);
end;
end else
begin
vBoardEB := nil;
FreeAndNil(vBoard);
end;
end;
inc(i);
end;
if fTmpBoardsList.Count < 1 then
begin
if vPortList.Count < 1 then
Log(lWarning, 'No vacant ports')
else
Log(lWarning, 'No boards whith try connect');
CallOnSearchComplete;
fSearchTimer.Enabled := false;
end;
finally
FreeAndNil(vPortList);
end;
end;
procedure TCustomBoardProcess.OnExtreactTmp(Sender: TObject);
begin
fExtractTmpTimer.Enabled := False;
if (fExtractTmpTimer.Tag <> -1)
and (fExtractTmpTimer.Tag < fTmpBoardsList.Count) then
fTmpBoardsList.Delete(fExtractTmpTimer.Tag);
fExtractTmpTimer.Tag := -1;
if fTmpBoardsList.Count < 1 then
begin
CallOnSearchComplete;
fSearchTimer.Enabled := false;
end;
end;
procedure TCustomBoardProcess.SearchTimeOut(Sender: TObject);
begin
fSearchTimer.Enabled := false;
fTmpBoardsList.Clear;
if (fBoards.Count < 1) then
begin
DoneProtocolLib;
Log(lEvent, 'All ports challenged. No boards found');
end;
CallOnSearchComplete;
end;
end.