-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeliumDataMgmt.pas
executable file
·1569 lines (1363 loc) · 50.4 KB
/
HeliumDataMgmt.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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit HeliumDataMgmt;
interface
uses
SysUtils, Classes, Contnrs, Math, SyncObjs, EvaluateDb, IsaacStandardInterfaces,
SQLClientInterfaces;
type
TLogErrorProc = procedure(const AMsg: string) of object;
TVessel = class(TObject)
ves_id: integer;
ves_type_id: integer;
ves_name: word;
ves_type: string;
ves_volume: double;
ves_tare: double;
ves_comment1: string;
ves_at_hzb_since: string;
{ - - - - - - - - - - - - - - }
ves_shortinterval: integer;
ves_longinterval: integer;
ves_minresistance: double;
ves_maxresistance: double;
ves_heattime: integer;
ves_adcloops: integer;
ves_filltimeout: integer;
ves_span: integer;
ves_zero: integer;
ves_comment2: string;
ves_options_in_db: boolean;
{ - - - - - - - - - - - - - - }
ves_he_level: double;
ves_pressure: word;
ves_position: string;
end;
TCoordinator = class(TObject)
coo_id: integer;
coo_name: string;
coo_comport: string;
coo_ip: string;
coo_address: string;
coo_position: string;
coo_comment: string;
{ - - - - - - - - - - - - - - }
coo_status: byte; // 0 inactive 1 active 2 failure
coo_xbeestatus: byte; // 0 reachable 1 testing 2 failure
end;
TModule = class(TObject)
mod_id: integer;
mod_name: string;
mod_address: string;
mod_lastactive: string;
mod_minvoltage: double;
mod_maxvoltage: double;
mod_criticvoltage: byte;
mod_comment: string;
{ - - - - - - - - - - - - - - }
mod_status: byte;
mod_vessel: TVessel;
mod_coordinator: TCoordinator;
mod_accumulator: byte;
mod_position: string;
mod_statusbyte: char;
{ - - - - - - - - - - - - - - }
mod_getStatus: boolean;
mod_getOptions: boolean;
mod_setOptions: boolean;
mod_getPositions: boolean;
mod_setPositions: boolean;
mod_getPassword: boolean;
mod_setPassword: boolean;
mod_getSleepTime: boolean;
mod_setSleepTime: boolean;
mod_getAwakeTime: boolean;
mod_setAwakeTime: boolean;
mod_displayText: string;
end;
TCryostat = class(TObject)
dev_id: integer;
dev_name: string;
dev_address: string;
dev_comment: string;
dev_ch1_enabled: boolean;
dev_ch1_gas: string;
dev_ch1_span: double;
dev_ch1_zero: double;
dev_ch2_enabled: boolean;
dev_ch2_gas: string;
dev_ch2_span: double;
dev_ch2_zero: double;
dev_ch3_enabled: boolean;
dev_ch3_gas: string;
dev_ch3_span: double;
dev_ch3_zero: double;
dev_class : integer;
{ - - - - - - - - - - - - - - }
dev_lastactive: string;
dev_ch1_level: double;
dev_ch2_level: double;
dev_ch3_level: double;
{ - - - - - - - - - - - - - - }
dev_coordinator: TCoordinator;
end;
TDataMgmt = class(TObject)
private
FLogErrorProc: TLogErrorProc;
FLock: TCriticalSection;
FDbClient: ISQLClient;
FEvaluate: TEvaluateDbThread;
FVesList: TObjectList;
FModList: TObjectList;
FCooList: TObjectList;
FDevList: TObjectList;
procedure SQLError(const AObj: ISQLImplementor; const AMessage: string);
public
constructor Create(const ALogErrorProc: TLogErrorProc); reintroduce;
destructor Destroy; override;
function Init: boolean;
procedure LockWhileWorkingWithList;
procedure UnlockAfterWorkingWithList;
procedure ConfigureDbConnection;
function GetDbConnection: boolean;
function GetDbClient: ISQLClient;
procedure DisconnectDbConnection;
procedure LastActiveToDb(AId: integer);
function MeasurementToDb(ACode: char; AVessel: TVessel; AModule: TModule; ACooId: integer; ADate: string; ALevel: double; APressure: word; AAccu: byte): boolean; overload;
function MeasurementToDb(AVesId, AModId, ACooId: integer; APosition: string; ALevel: double; APressure: word; AAccu: byte; ADate: string; AStatus: integer): boolean; overload;
function CryoMeasurementToDb(const ADevId, ACooId: integer;
const ACH1Level, ACh2Level, ACh3Level: double;
const ADate: string): boolean;
function InsertVesOptionsToDb(AVessel: TVessel): boolean;
function UpdateVesOptionsToDb(AVessel: TVessel): boolean;
function DeleteVesOptionsToDb(const AId: integer): boolean;
function InsertModuleToDb(AModule: TModule): boolean;
function UpdateModuleToDb(AModule: TModule): boolean;
function DeleteModuleToDb(const AId: integer): boolean;
function InsertCoordinatorToDb(ACoo: TCoordinator): boolean;
function UpdateCoordinatorToDb(ACoo: TCoordinator): boolean;
function DeleteCoordinatorToDb(const AId: integer): boolean;
function InsertIlmDeviceToDb(ADev: TCryostat): boolean;
function UpdateIlmDeviceToDb(ADev: TCryostat): boolean;
function DeleteIlmDeviceToDb(const AId: integer): boolean;
function SqlCommand(const AQuery: string): boolean;
function RefreshVesListByDB: boolean;
function RefreshModListByDB: boolean;
function RefreshCooListByDB: boolean;
function RefreshDevListByDB: boolean;
function GetVesItem(const AId: integer): TVessel;
function GetModItem(const AId: integer): TModule;
function GetCooItem(const AId: integer): TCoordinator;
function GetDevItem(const AId: integer): TCryostat;
function GetVesIndex(const AId: integer): integer;
function GetModIndex(const AId: integer): integer;
function GetCooIndex(const AId: integer): integer;
function GetDevIndex(const AId: integer): integer;
function GetVesItemByName(const AName: word): TVessel;
function GetModItemByAddr(const AAddr: string): TModule;
function GetDevItemByAddr(const AAddr: string): TCryostat;
function GetVesIndexByName(const AName: word): integer;
function GetModIndexByAddr(const AAddr: string): integer;
function GetDevIndexByAddr(const AAddr: string): integer;
function GetCopyOfVessel(const AIndex: integer): TVessel;
function GetCopyOfModule(const AIndex: integer): TModule;
function GetCopyOfCoordinator(const AIndex: integer): TCoordinator;
function GetCopyOfIlmDevice(const AIndex: integer): TCryostat;
function InsertNewCryoClass(const AClassName: string): integer;
function GetRegisteredCryoClasses: TStringList;
procedure EvaluateDatabase;
procedure FreeEvaluateThread(Sender: TObject);
published
property VesselList: TObjectList read FVesList;
property ModuleList: TObjectList read FModList;
property CoordinatorList: TObjectList read FCooList;
property DeviceList: TObjectList read FDevList;
end;
function BoolToNumber(const ABool: boolean): Char;
function NumberToBool(const ANumber: byte): boolean;
function GetModStatus(const AStatus: byte): string;
function GetCooStatus(const AStatus: byte): string;
function NewSQLClient: ISQLClient;
function GetSQLClientFactories: IDataModule; stdcall; external 'SQLClients.dll' name 'GetSQLClientFactories';
implementation
uses
HeliumFunctions, Dialogs;
const EClassConvertError = 'Error converting %s into DB_Key.';
const EKeyConvertError = 'Error converting %d into ClassName.';
function BoolToNumber(const ABool: boolean): Char;
begin
if ABool then
Result := '1'
else
Result := '0';
end;
function NumberToBool(const ANumber: byte): boolean;
begin
Result := not (ANumber = 0);
end;
function GetModStatus(const AStatus: byte): string;
begin
Result := '';
case AStatus of
0 : Result := 'inactive';
1 : Result := 'connecting';
2 : Result := 'connected';
3 : Result := 'fill begin';
4 : Result := 'filling';
5 : Result := 'timeout';
end;
end;
function GetCooStatus(const AStatus: byte): string;
begin
Result := '';
case AStatus of
0 : Result := 'inactive';
1 : Result := 'connected';
2 : Result := 'not connected';
end;
end;
function NewSQLClient: ISQLClient;
var
lRegId, lRegGUID: string;
lFactories: IDataModule;
lIntf: IInterface;
lFactory: ISQLImplementorFactory;
begin
Result := nil;
lFactories := GetSQLClientFactories;
if Assigned(lFactories) then
try
lRegGUID := getRegString('', 'SQLClientClassId');
lRegId := getRegString('', 'SQLClientId');
if lRegGUID <> '' then
begin
lFactories.GetInterfaceValue(PChar(lRegGUID), lIntf);
if not(supports(lIntf, ISQLImplementorFactory, lFactory)) then
raise Exception.Create(Format('ClassFactory for database ''%s'' could not be initialized', [lRegId]));
end
else
raise Exception.Create(Format('SQLClientFactory %s could not be initialized, no DataBase type specified', [lRegId]));
Result := lFactory.CreateClient;
finally
lFactories := nil;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// << T D a t a - M a n a g e m e n t >> - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TDataMgmt.Create(const ALogErrorProc: TLogErrorProc);
begin
inherited Create;
FLogErrorProc := ALogErrorProc;
FLock := TCriticalSection.Create;
FVesList := TObjectList.Create(true);
FModList := TObjectList.Create(true);
FCooList := TObjectList.Create(true);
FDevList := TObjectList.Create(true);
FDbClient := nil;
FEvaluate := nil;
end;
destructor TDataMgmt.Destroy;
begin
FLock.Acquire;
try
FVesList.Free;
FModList.Free;
FCooList.Free;
FDevList.Free;
FDbClient := nil;
finally
FLock.Release;
FLock.Free;
end;
inherited Destroy;
end;
procedure TDataMgmt.LockWhileWorkingWithList;
begin
FLock.Acquire;
end;
procedure TDataMgmt.UnlockAfterWorkingWithList;
begin
FLock.Release;
end;
procedure TDataMgmt.ConfigureDbConnection;
begin
if Assigned(FDbClient) then FDbClient := nil;
FDbClient := NewSQLClient;
if not Assigned(FDbClient) then
raise Exception.Create('Failure configuring Db, SQLClient not assigned');
if not(Assigned(FDbClient.GetSession)) then
raise Exception.Create('Failure configuring Db, session not assigned');
FDbClient.GetSession.InitSession(getRegString('', 'DbServer'),
getRegString('', 'DbUser'),
decrypt(getRegString('', 'DbKey')));
end;
function TDataMgmt.GetDbClient: ISQLClient;
begin
result := FDbClient;
end;
function TDataMgmt.GetDbConnection: boolean;
var
lSession: ISQLSession;
begin
lSession := FDbClient.GetSession;
Result := lSession.Connected;
if not(Result) then
if not ((lSession.GetDataSource = '') or
(lSession.GetUsername = '') or
(lSession.GetPassword = '')) then
Result := lSession.Connect;
if not(Result) then
SQLError(lSession, Format(EConnectSession, [lSession.GetDataSource, lSession.GetUsername]));
end;
procedure TDataMgmt.DisconnectDbConnection;
begin
if assigned(FDbClient) then
FDbClient.GetSession.Disconnect;
end;
procedure TDataMgmt.LastActiveToDb(AId: integer);
begin
if (AId > -1) and GetDbConnection then
begin
setDbQuery(FDbClient, 'UPDATE ' + db_t_module + ' SET '
+ 'MOD_LASTTIMEACTIVE = SYSDATE WHERE '
+ 'MOD_ID = ' + intToStr(AId));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
end;
end;
function TDataMgmt.MeasurementToDb(ACode: char; AVessel: TVessel; AModule: TModule; ACooId: integer; ADate: string; ALevel: double; APressure: word; AAccu: byte): boolean;
var LMeaStatus: integer;
LPosition: string;
begin
Result := false;
LMeaStatus := 1;
LPosition := '';
if not (AModule.mod_status in [0,1]) then
case ACode of
#3 : LMeaStatus := 2;
#6 : begin
if (AModule.mod_status = 3)
then LMeaStatus := 3
else LMeaStatus := 4;
LPosition := AVessel.ves_position;
end;
#7 : begin
LMeaStatus := 5;
LPosition := AVessel.ves_position;
end;
#8 : LMeaStatus := 6;
#11: LMeaStatus := 7;
end;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_measurement + ' VALUES (1, '
+ intToStr(AVessel.ves_id) + ', '
+ intToStr(AModule.mod_id) + ', '
+ intToStr(ACooId) + ', '
+ #39 + LPosition + #39 + ', '
+ floatToDbFloat(ALevel) + ', '
+ intToStr(APressure) + ', '
+ intToStr(AAccu) + ', '
+ 'to_date(' + #39 + ADate + #39
+ ',' + #39 + 'dd.mm.yyyy hh24:mi:ss' + #39 + '), '
+ intToStr(LMeaStatus) + ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.MeasurementToDb(AVesId, AModId, ACooId: integer; APosition: string; ALevel: double; APressure: word; AAccu: byte; ADate: string; AStatus: integer): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_measurement + ' VALUES (1, '
+ intToStr(AVesId) + ', '
+ intToStr(AModId) + ', '
+ intToStr(ACooId) + ', '
+ #39 + APosition + #39 + ', '
+ floatToDbFloat(ALevel) + ', '
+ intToStr(APressure) + ', '
+ intToStr(AAccu) + ', '
+ 'to_date(' + #39 + ADate + #39
+ ',' + #39 + 'dd.mm.yyyy hh24:mi:ss' + #39 + '), '
+ intToStr(AStatus) + ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.CryoMeasurementToDb(const ADevId, ACooId: Integer;
const ACH1Level, ACh2Level, ACh3Level: Double;
const ADate: string): boolean;
function GetLevel(const ALevel: double): string;
begin
Result := 'null';
if not (ALevel > 200) then
Result := floatToDbFloat(ALevel);
end;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_ilmmeasurement + ' VALUES (1, '
+ intToStr(ADevId) + ', '
+ intToStr(ACooId) + ', '
+ GetLevel(ACh1Level) + ', '
+ GetLevel(ACh2Level) + ', '
+ GetLevel(ACh3Level) + ', '
+ 'to_date(' + #39 + ADate + #39
+ ',' + #39 + 'dd.mm.yyyy hh24:mi:ss' + #39 + '))');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.InsertVesOptionsToDb(AVessel: TVessel): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_hlm_vessel + ' VALUES ('
+ intToStr(AVessel.ves_id) + ', '
+ intToStr(AVessel.ves_shortinterval) + ', '
+ intToStr(AVessel.ves_longinterval) + ', '
+ floatToDbFloat(AVessel.ves_minresistance) + ', '
+ floatToDbFloat(AVessel.ves_maxresistance) + ', '
+ intToStr(AVessel.ves_heattime) + ', '
+ intToStr(AVessel.ves_adcloops) + ', '
+ intToStr(AVessel.ves_filltimeout) + ', '
+ intToStr(AVessel.ves_span) + ', '
+ intToStr(AVessel.ves_zero) + ', '
+ #39 + AVessel.ves_comment2 + #39 + ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.UpdateVesOptionsToDb(AVessel: TVessel): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'UPDATE ' + db_t_hlm_vessel + ' SET '
+ 'VES_SHORTINTERVAL = ' + intToStr(AVessel.ves_shortinterval) + ', '
+ 'VES_LONGINTERVAL = ' + intToStr(AVessel.ves_longinterval) + ', '
+ 'VES_MINRESISTANCE = ' + floatToDbFloat(AVessel.ves_minresistance) + ', '
+ 'VES_MAXRESISTANCE = ' + floatToDbFloat(AVessel.ves_maxresistance) + ', '
+ 'VES_HEATTIME = ' + intToStr(AVessel.ves_heattime) + ', '
+ 'VES_ADCLOOP = ' + intToStr(AVessel.ves_adcloops) + ', '
+ 'VES_FILLTIMEOUT = ' + intToStr(AVessel.ves_filltimeout) + ', '
+ 'VES_SPAN = ' + intToStr(AVessel.ves_span) + ', '
+ 'VES_ZERO = ' + intToStr(AVessel.ves_zero) + ', '
+ 'VES_COMMENT = ' + #39 + AVessel.ves_comment2 + #39
+ ' WHERE VES_V_ID = ' + intToStr(AVessel.ves_id));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.DeleteVesOptionsToDb(const AId: Integer): boolean;
begin
Result := false;
if (AId > 0) then
if GetDbConnection then
try
setDbQuery(FDbClient, 'DELETE FROM ' + db_t_hlm_vessel
+ ' WHERE VES_V_ID = ' + intToStr(AId));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.InsertModuleToDb(AModule: TModule): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_module + ' VALUES ('
+ '((SELECT MAX(MOD_ID) FROM ' + db_t_module + ') + 1),'
+ #39 + AModule.mod_name + #39 + ', '
+ #39 + AModule.mod_address + #39 + ', '
+ #39 + #39 + ', '
+ floatToDbFloat(AModule.mod_minvoltage) + ', '
+ floatToDbFloat(AModule.mod_maxvoltage) + ', '
+ intToStr(AModule.mod_criticvoltage) + ', '
+ #39 + AModule.mod_comment + #39 + ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.UpdateModuleToDb(AModule: TModule): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'UPDATE ' + db_t_module + ' SET '
+ 'MOD_NAME = ' + #39 + AModule.mod_name + #39 + ', '
+ 'MOD_ADDRESS = ' + #39 + AModule.mod_address + #39 + ', '
+ 'MOD_MINVOLTAGE = ' + floatToDbFloat(AModule.mod_minvoltage) + ', '
+ 'MOD_MAXVOLTAGE = ' + floatToDbFloat(AModule.mod_maxvoltage) + ', '
+ 'MOD_CRITICALVOLTAGE = ' + intToStr(AModule.mod_criticvoltage) + ', '
+ 'MOD_COMMENT = ' + #39 + AModule.mod_comment + #39
+ ' WHERE MOD_ID = ' + intToStr(AModule.mod_id));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.DeleteModuleToDb(const AId: Integer): boolean;
begin
Result := false;
if (AId > 0) then
if GetDbConnection then
try
setDbQuery(FDbClient, 'DELETE FROM ' + db_t_module
+ ' WHERE MOD_ID = ' + intToStr(AId));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.Init: Boolean;
begin
Result := True;
try
ConfigureDbConnection;
if not(GetDbConnection) then
raise Exception.Create(Format('Failure connecting to database (data source: %s, user: %s)', [FDbClient.GetSession.GetDataSource, FDbClient.GetSession.GetUsername]));
except
on E: Exception do
begin
MessageDlg(Format('Failure initializing DataMgmt, message: %s', [E.Message]),
mtError, [mbOk], 0);
Result := False;
end;
end;
end;
function TDataMgmt.InsertCoordinatorToDb(ACoo: TCoordinator): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_coordinator + ' VALUES ('
+ '((SELECT MAX(COO_ID) FROM ' + db_t_coordinator + ') + 1),'
+ #39 + ACoo.coo_name + #39 + ', '
+ #39 + ACoo.coo_comport + #39 + ', '
+ #39 + ACoo.coo_ip + #39 + ', '
+ #39 + ACoo.coo_address + #39 + ', '
+ #39 + ACoo.coo_position + #39 + ', '
+ #39 + ACoo.coo_comment + #39 + ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.UpdateCoordinatorToDb(ACoo: TCoordinator): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'UPDATE ' + db_t_coordinator + ' SET '
+ 'COO_NAME = ' + #39 + ACoo.coo_name + #39 + ', '
+ 'COO_COMPORT = ' + #39 + ACoo.coo_comport + #39 + ', '
+ 'COO_IP = ' + #39 + ACoo.coo_ip + #39 + ', '
+ 'COO_ADDRESS = ' + #39 + ACoo.coo_address + #39 + ', '
+ 'COO_POSITION = ' + #39 + ACoo.coo_position + #39 + ', '
+ 'COO_COMMENT = ' + #39 + ACoo.coo_comment + #39
+ ' WHERE COO_ID = ' + intToStr(ACoo.coo_id));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.DeleteCoordinatorToDb(const AId: Integer): boolean;
begin
Result := false;
if (AId > 0) then
if GetDbConnection then
try
setDbQuery(FDbClient, 'DELETE FROM ' + db_t_coordinator
+ ' WHERE COO_ID = ' + intToStr(AId));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.InsertNewCryoClass(const AClassName: string): integer;
function GetClassID(AClassName: string): integer;
var i: integer;
begin
SetDBQuery(FDbClient, Format('SELECT ILC_ID FROM HLM_ILMCLASS ' +
'WHERE ILC_NAME=%s', [AClassName]));
if FDbClient.Open then
try
if FDbClient.Active then
result := FDbClient.FieldByName('ILC_ID').AsInteger
else
raise Exception.Create(Format(EClassConvertError, [AClassName]));
finally
FDbClient.Close;
end
else
SQLError(FDbClient, EOpenClient);
end;
begin
result := -1;
if GetDbConnection then
begin
SetDbQuery(FDbClient,
Format('insert into hlm_ilmclass(ilc_name) ' +
'values(''%s'')', [AClassName]));
if not(FDbClient.Submit) then
raise Exception(Format(ESubmitSQL, [FDbClient.GetErrorBuffer.Message]));
result := GetClassID(AClassName);
end;
end;
function TDataMgmt.InsertIlmDeviceToDb(ADev: TCryostat): boolean;
function GetClassID(AClassName: string): integer;
var i: integer;
begin
SetDBQuery(FDbClient, Format('SELECT ILC_ID FROM HLM_ILMCLASS ' +
'WHERE ILC_NAME=%s', [AClassName]));
if FDbClient.Open then
try
if FDbClient.Active then
result := FDbClient.FieldByName('ILC_ID').AsInteger
else
raise Exception.Create(Format(EClassConvertError, [AClassName]));
finally
FDbClient.Close;
end
else
SQLError(FDbClient, EOpenClient);
end;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'INSERT INTO ' + db_t_device + ' VALUES ('
+ '((SELECT MAX(ILD_ID) FROM ' + db_t_device + ') + 1),'
+ #39 + ADev.dev_name + #39 + ', '
+ #39 + ADev.dev_address + #39 + ', '
+ #39 + ADev.dev_comment + #39 + ', '
+ BoolToNumber(ADev.dev_ch1_enabled) + ', '
+ #39 + ADev.dev_ch1_gas + #39 + ', '
+ floatToDbFloat(ADev.dev_ch1_span) + ', '
+ floatToDbFloat(ADev.dev_ch1_zero) + ', '
+ BoolToNumber(ADev.dev_ch2_enabled) + ', '
+ #39 + ADev.dev_ch2_gas + #39 + ', '
+ floatToDbFloat(ADev.dev_ch2_span) + ', '
+ floatToDbFloat(ADev.dev_ch2_zero) + ', '
+ BoolToNumber(ADev.dev_ch3_enabled) + ', '
+ #39 + ADev.dev_ch3_gas + #39 + ', '
+ floatToDbFloat(ADev.dev_ch3_span) + ', '
+ floatToDbFloat(ADev.dev_ch3_zero) + ', '
+ IntToStr(ADev.dev_Class)+ ')');
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.UpdateIlmDeviceToDb(ADev: TCryostat): boolean;
begin
Result := false;
if GetDbConnection then
try
setDbQuery(FDbClient, 'UPDATE ' + db_t_device + ' SET '
+ 'ILD_NAME = ' + #39 + ADev.dev_name + #39 + ', '
+ 'ILD_ADDRESS = ' + #39 + ADev.dev_address + #39 + ', '
+ 'ILD_COMMENT = ' + #39 + ADev.dev_comment + #39 + ', '
+ 'ILD_CH1_ENABLED = ' + BoolToNumber(ADev.dev_ch1_enabled) + ', '
+ 'ILD_CH1_SUBSTANCE = ' + #39 + ADev.dev_ch1_gas + #39 + ', '
+ 'ILD_CH1_SPAN = ' + floatToDbFloat(ADev.dev_ch1_span) + ', '
+ 'ILD_CH1_ZERO = ' + floatToDbFloat(ADev.dev_ch1_zero) + ', '
+ 'ILD_CH2_ENABLED = ' + BoolToNumber(ADev.dev_ch2_enabled) + ', '
+ 'ILD_CH2_SUBSTANCE = ' + #39 + ADev.dev_ch2_gas + #39 + ', '
+ 'ILD_CH2_SPAN = ' + floatToDbFloat(ADev.dev_ch2_span) + ', '
+ 'ILD_CH2_ZERO = ' + floatToDbFloat(ADev.dev_ch2_zero) + ', '
+ 'ILD_CH3_ENABLED = ' + BoolToNumber(ADev.dev_ch3_enabled) + ', '
+ 'ILD_CH3_SUBSTANCE = ' + #39 + ADev.dev_ch3_gas + #39 + ', '
+ 'ILD_CH3_SPAN = ' + floatToDbFloat(ADev.dev_ch3_span) + ', '
+ 'ILD_CH3_ZERO = ' + floatToDbFloat(ADev.dev_ch3_zero) + ', '
+ 'ILD_CLASS = ' + IntToStr(ADev.dev_class)
+ ' WHERE ILD_ID = ' + intToStr(ADev.dev_id));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.DeleteIlmDeviceToDb(const AId: integer): boolean;
begin
Result := false;
if (AId > 0) then
if GetDbConnection then
try
setDbQuery(FDbClient, 'DELETE FROM ' + db_t_device
+ ' WHERE ILD_ID = ' + intToStr(AId));
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
function TDataMgmt.SqlCommand(const AQuery: string): boolean;
begin
Result := false;
if not (AQuery = '') then
if GetDbConnection then
try
setDbQuery(FDbClient, AQuery);
if not(FDbClient.Submit) then
SQLError(FDbClient, ESubmitSQL);
Result := true;
finally
end;
end;
procedure TDataMgmt.SQLError(const AObj: ISQLImplementor;
const AMessage: string);
var
lMsg: string;
lErrBuffer: IException;
begin
lErrBuffer := AObj.GetErrorBuffer;
if Assigned(lErrBuffer) then
try
lMsg := Format('%s, message: %s', [AMessage, lErrBuffer.Message]);
finally
lErrBuffer := nil;
end
else
lMsg := Format(AMessage, ['No error message']);
if Assigned(FLogErrorProc) then
FLogErrorProc(lMsg)
else
raise Exception.Create(lMsg);
end;
function TDataMgmt.GetRegisteredCryoClasses: TStringList;
function GetClassName(AID: integer): string;
begin
SetDBQuery(FDbClient, Format('SELECT ILC_NAME FROM HLM_ILMCLASS ' +
'WHERE ILC_ID=%d', [AID]));
if FDbClient.Open then
try
if FDbClient.Active then
result := FDbClient.FieldByName('ILC_NAME').AsString
else
raise Exception.Create(Format(EKeyConvertError, [AID]));
finally
FDbClient.Close;
end
else
SQLError(FDbClient, EOpenClient);
end;
begin
result := TStringList.Create;
if GetDbConnection then
begin
SetDBQuery(FDbClient , 'SELECT * FROM HLM_ILMCLASS');
try
if FDbClient.Open then
begin
if FDbClient.Active then
with FDbClient do
while not Eof do
begin
result.AddObject(FieldByName('ILC_NAME').AsString, Pointer(FieldByName('ILC_ID').AsInteger));
Next;
end;
end
else
SQLError(FDbClient, EOpenClient);
finally
FDbClient.Close;
end;
end;
end;
function TDataMgmt.RefreshVesListByDB: boolean;
var LVesList: TObjectList;
LVessel: TVessel;
i: integer;
function CompareVes(Item1: Pointer; Item2: Pointer): integer;
begin
Result := CompareValue(TVessel(Item1).ves_name, TVessel(Item2).ves_name);
end;
begin
Result := false;
LVesList := TObjectList.Create(false);
if GetDbConnection then
try
for i := 0 to FVesList.Count - 1 do LVesList.Add(FVesList[i]);
setDbQuery(FDbClient, 'SELECT * FROM ' + db_t_type
+ ' RIGHT OUTER JOIN ' + db_t_hen2_vessel
+ ' ON V_T_ID = T_ID'
+ ' LEFT OUTER JOIN ' + db_t_weighing
+ ' ON W_V_ID = V_ID '
+ ' LEFT OUTER JOIN ' + db_t_hlm_vessel
+ ' ON VES_V_ID = V_ID'
+ ' WHERE T_SUBSTANCE = ' + #39 + 'He' + #39
+ ' AND W_ARRIVING_DATE ='
+ ' (SELECT MAX(W_ARRIVING_DATE) FROM ' + db_t_weighing
+ ' WHERE W_V_ID = V_ID)'
+ ' ORDER BY UPPER(V_NAME)');
if FDbClient.Open then
begin
Result := FDbClient.Active;
if Result then
with FDbClient do
while not Eof do
begin
i := GetVesIndex(FieldByName('V_ID').AsInteger);
if (i = -1) then
begin
LVessel := TVessel.Create;
FVesList.Add(LVessel);
LVessel.ves_id := FieldByName('V_ID').AsInteger;
LVessel.ves_he_level := 255;
LVessel.ves_pressure := 0;
LVessel.ves_position := '';
end
else begin
LVessel := TVessel(FVesList[i]);
LVesList[i] := nil;
end;
LVessel.ves_type_id := FieldByName('T_ID').AsInteger;
LVessel.ves_name := FieldByName('V_NAME').AsInteger;
LVessel.ves_type := FieldByName('T_NAME').AsString;
LVessel.ves_volume := FieldByName('T_VOLUME').AsFloat;
LVessel.ves_tare := FieldByName('V_TARE').AsFloat;
LVessel.ves_comment1 := FieldByName('V_COMMENT').AsString;
if (FieldByName('W_LEAVING_DATE').AsString = '')
then LVessel.ves_at_hzb_since := FieldByName('W_ARRIVING_DATE').AsString
else LVessel.ves_at_hzb_since := '';
LVessel.ves_options_in_db := FieldByName('VES_V_ID').AsInteger > 0;
if LVessel.ves_options_in_db then
begin
LVessel.ves_shortinterval := FieldByName('VES_SHORTINTERVAL').AsInteger;
LVessel.ves_longinterval := FieldByName('VES_LONGINTERVAL').AsInteger;
LVessel.ves_minresistance := FieldByName('VES_MINRESISTANCE').AsFloat;
LVessel.ves_maxresistance := FieldByName('VES_MAXRESISTANCE').AsFloat;
LVessel.ves_heattime := FieldByName('VES_HEATTIME').AsInteger;
LVessel.ves_adcloops := FieldByName('VES_ADCLOOP').AsInteger;
LVessel.ves_filltimeout := FieldByName('VES_FILLTIMEOUT').AsInteger;
LVessel.ves_span := FieldByName('VES_SPAN').AsInteger;
LVessel.ves_zero := FieldByName('VES_ZERO').AsInteger;
LVessel.ves_comment2 := FieldByName('VES_COMMENT').AsString;
end;
Next;
end;
end
else
SQLError(FDbClient, EOpenClient);
for i := LVesList.Count - 1 downto 0 do
if Assigned(LVesList[i]) then
begin
LVesList[i] := nil;
FVesList.Delete(i);
end;
FVesList.Sort(@CompareVes);
finally
FDbClient.Close;
end;
LVesList.Free;