-
Notifications
You must be signed in to change notification settings - Fork 0
/
IlmDevice.pas
executable file
·490 lines (434 loc) · 15.1 KB
/
IlmDevice.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
unit IlmDevice;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ComCtrls,
ExtCtrls, DatabaseEdit, HeliumDataMgmt;
resourcestring
CREATE_NEW_CATEGORY = 'Create new category';
type
TIlmDeviceForm = class(TForm)
NameLab: TLabel;
AddressLab: TLabel;
CommentLab: TLabel;
ilmLab: TLabel;
HintLab: TLabel;
ilmCoBox: TComboBox;
devPanel: TPanel;
NameEd: TOraEdit;
AddressEd: TOraEdit;
CommentRiEd: TRichEdit;
SaveBtn: TButton;
DeleteBtn: TButton;
CancelBtn: TButton;
Ch1SpanLab: TLabel;
Ch1ZeroEd: TOraEdit;
Ch1ZeroLab: TLabel;
Ch1ChkBox: TCheckBox;
Ch1SpanEd: TOraEdit;
Ch2SpanLab: TLabel;
Ch2ZeroEd: TOraEdit;
Ch2ZeroLab: TLabel;
Ch2ChkBox: TCheckBox;
Ch2SpanEd: TOraEdit;
Ch3SpanLab: TLabel;
Ch3ZeroEd: TOraEdit;
Ch3ZeroLab: TLabel;
Ch3SpanEd: TOraEdit;
Ch3ChkBox: TCheckBox;
Ch1GasCoBox: TComboBox;
Ch1GasLab: TLabel;
Ch2GasLab: TLabel;
Ch3GasLab: TLabel;
Ch2GasCoBox: TComboBox;
Ch3GasCoBox: TComboBox;
CBCategory: TComboBox;
Label1: TLabel;
Label2: TLabel;
EdNewCatName: TEdit;
procedure ilmCoBoxChange(Sender: TObject);
procedure DeleteBtnClick(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure Ch1ChkBoxClick(Sender: TObject);
procedure Ch2ChkBoxClick(Sender: TObject);
procedure Ch3ChkBoxClick(Sender: TObject);
procedure CBCategoryChange(Sender: TObject);
private
FAddCategoryIndex: integer;
FRegisteredClasses: TStringList;
FDataMgmt: TDataMgmt;
procedure InitForm;
public
constructor CreateWithProperties(ADataMgmt: TDataMgmt; AOwner: TComponent);
destructor Destroy; override;
end;
implementation
{$R *.dfm}
{ TIlmDeviceForm }
constructor TIlmDeviceForm.CreateWithProperties(ADataMgmt: TDataMgmt;
AOwner: TComponent);
begin
inherited Create(AOwner);
FAddCategoryIndex := -1;
HintLab.Caption := '';
FDataMgmt := ADataMgmt;
InitForm;
end;
destructor TIlmDeviceForm.Destroy;
begin
if Assigned(FRegisteredClasses) then
FRegisteredClasses.Free;
inherited;
end;
procedure TIlmDeviceForm.InitForm;
var i: integer;
begin
ilmCoBox.Clear;
ilmCoBox.AddItem('- CREATE NEW CRYOSTAT -', nil);
with FDataMgmt do
try
LockWhileWorkingWithList;
if Assigned(FRegisteredClasses) then
FRegisteredCLasses.Free;
FRegisteredClasses := GetRegisteredCryoClasses;
// Initializing Categories
CBCategory.Items.Clear;
with FRegisteredClasses do
while CBCategory.Items.Count < Count do
CBCategory.Items.Add(Strings[CBCategory.Items.Count]);
FAddCategoryIndex := CBCategory.Items.Add(Format('- %s -', [Uppercase(CREATE_NEW_CATEGORY)]));
RefreshDevListByDB;
for i := 0 to DeviceList.Count - 1 do
ilmCoBox.AddItem(TCryostat(DeviceList[i]).dev_name,
TObject(TCryostat(DeviceList[i]).dev_id));
finally
UnlockAfterWorkingWithList;
end;
ilmCoBox.ItemIndex := 0;
ActiveControl := ilmCoBox;
IlmCoBoxChange(nil);
end;
procedure TIlmDeviceForm.ilmCoBoxChange(Sender: TObject);
var LDev: TCryostat;
function GetGasIndex(const AGas: string): integer;
begin
Result := -1;
if AGas = 'He' then
Result := 0
else
if AGas = 'N' then
Result := 1;
end;
function GetClassName(AID: integer): string;
var i: integer;
begin
result := '';
i := 0;
while (i < FRegisteredCLasses.Count) and (result = '') do
begin
if AID = Integer(FRegisteredCLasses.Objects[i]) then
result := FRegisteredClasses[i];
inc(i);
end;
end;
var s: string;
begin
if Assigned(Sender) then HintLab.Caption := '';
if (ilmCoBox.ItemIndex = 0) then
begin
NameEd.Text := '';
AddressEd.Text := '';
CommentRiEd.Text := '';
CBCategory.Text := '';
Ch1ChkBox.Checked := false;
Ch2ChkBox.Checked := false;
Ch3ChkBox.Checked := false;
Ch1ChkBoxClick(nil);
Ch2ChkBoxClick(nil);
Ch3ChkBoxClick(nil);
end;
if (ilmCoBox.ItemIndex > 0) then
begin
with FDataMgmt, ilmCoBox do
try
LockWhileWorkingWithList;
LDev := GetCopyOfIlmDevice(GetDevIndex(integer(Items.Objects[ItemIndex])));
finally
UnlockAfterWorkingWithList;
end;
if Assigned(LDev) then
begin
NameEd.Text := LDev.dev_name;
AddressEd.Text := LDev.dev_address;
CommentRiEd.Text := LDev.dev_comment;
Ch1ChkBox.Checked := LDev.dev_ch1_enabled;
Ch2ChkBox.Checked := LDev.dev_ch2_enabled;
Ch3ChkBox.Checked := LDev.dev_ch3_enabled;
s := GetClassName(LDev.dev_class);
if s <> '' then
begin
if CBCategory.Items.IndexOf(s) < 0 then
CBCategory.Items.Add(s);
CBCategory.ItemIndex := CBCategory.Items.IndexOf(s);
CBCategory.Text := s;
end;
if LDev.dev_ch1_enabled then
begin
Ch1GasCoBox.ItemIndex := GetGasIndex(LDev.dev_ch1_gas);
Ch1SpanEd.Text := floatToStr(LDev.dev_ch1_span);
Ch1ZeroEd.Text := floatToStr(LDev.dev_ch1_zero);
end
else begin
Ch1GasCoBox.ItemIndex := -1;
Ch1SpanEd.Text := '';
Ch1ZeroEd.Text := '';
end;
if LDev.dev_ch2_enabled then
begin
Ch2GasCoBox.ItemIndex := GetGasIndex(LDev.dev_ch2_gas);
Ch2SpanEd.Text := floatToStr(LDev.dev_ch2_span);
Ch2ZeroEd.Text := floatToStr(LDev.dev_ch2_zero);
end
else begin
Ch2GasCoBox.ItemIndex := -1;
Ch2SpanEd.Text := '';
Ch2ZeroEd.Text := '';
end;
if LDev.dev_ch3_enabled then
begin
Ch3GasCoBox.ItemIndex := GetGasIndex(LDev.dev_ch3_gas);
Ch3SpanEd.Text := floatToStr(LDev.dev_ch3_span);
Ch3ZeroEd.Text := floatToStr(LDev.dev_ch3_zero);
end
else begin
Ch3GasCoBox.ItemIndex := -1;
Ch3SpanEd.Text := '';
Ch3ZeroEd.Text := '';
end;
end
else
InitForm;
end;
end;
procedure TIlmDeviceForm.CBCategoryChange(Sender: TObject);
begin
EdNewCatName.Enabled := (FAddCategoryIndex > -1) and
(CBCategory.ItemIndex = FAddCategoryIndex);
end;
procedure TIlmDeviceForm.Ch1ChkBoxClick(Sender: TObject);
begin
if not Ch1ChkBox.Checked then
begin
Ch1GasCoBox.ItemIndex := -1;
Ch1SpanEd.Text := '';
Ch1ZeroEd.Text := '';
end;
Ch1GasCoBox.Enabled := Ch1ChkBox.Checked;
Ch1SpanEd.Enabled := Ch1ChkBox.Checked;
Ch1ZeroEd.Enabled := Ch1ChkBox.Checked;
end;
procedure TIlmDeviceForm.Ch2ChkBoxClick(Sender: TObject);
begin
if not Ch2ChkBox.Checked then
begin
Ch2GasCoBox.ItemIndex := -1;
Ch2SpanEd.Text := '';
Ch2ZeroEd.Text := '';
end;
Ch2GasCoBox.Enabled := Ch2ChkBox.Checked;
Ch2SpanEd.Enabled := Ch2ChkBox.Checked;
Ch2ZeroEd.Enabled := Ch2ChkBox.Checked;
end;
procedure TIlmDeviceForm.Ch3ChkBoxClick(Sender: TObject);
begin
if not Ch3ChkBox.Checked then
begin
Ch3GasCoBox.ItemIndex := -1;
Ch3SpanEd.Text := '';
Ch3ZeroEd.Text := '';
end;
Ch3GasCoBox.Enabled := Ch3ChkBox.Checked;
Ch3SpanEd.Enabled := Ch3ChkBox.Checked;
Ch3ZeroEd.Enabled := Ch3ChkBox.Checked;
end;
procedure TIlmDeviceForm.SaveBtnClick(Sender: TObject);
var LVal: double;
LDev: TCryostat;
function GasChannelSettingsOK: boolean;
begin
Result := true;
if Ch1ChkBox.Checked then
if not (Ch1GasCoBox.ItemIndex in [0, 1]) or
not TryStrToFloat(Ch1SpanEd.Text, LVal) or
not TryStrToFloat(Ch1ZeroEd.Text, LVal) then
Result := false;
if Ch2ChkBox.Checked then
if not (Ch2GasCoBox.ItemIndex in [0, 1]) or
not TryStrToFloat(Ch2SpanEd.Text, LVal) or
not TryStrToFloat(Ch2ZeroEd.Text, LVal) then
Result := false;
if Ch3ChkBox.Checked then
if not (Ch3GasCoBox.ItemIndex in [0, 1]) or
not TryStrToFloat(Ch3SpanEd.Text, LVal) or
not TryStrToFloat(Ch3ZeroEd.Text, LVal) then
Result := false;
result := result and
((CBCategory.ItemIndex <> FAddCategoryIndex) or
((EdNewCatName.Text <> '') and (CBCategory.Items.IndexOf(EdNewCatName.Text) < 0)));
end;
var lClassID: integer;
begin
LDev := nil;
if (ilmCoBox.ItemIndex <> -1) then
if (trim(NameEd.Text) <> '') then
if (trim(AddressEd.Text) <> '') then
if GasChannelSettingsOK then
begin
if (CBCategory.ItemIndex = FAddCategoryIndex) then
lClassID := FDataMgmt.InsertNewCryoClass(EdNewCatName.Text)
else
lClassID := Integer(FRegisteredClasses.Objects[FRegisteredClasses.IndexOf(CBCategory.Text)]);
if (ilmCoBox.ItemIndex = 0) then
if (MessageDlg('Do you really want to save the new cryostat?',
mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt do
try
LockWhileWorkingWithList;
LDev := TCryostat.Create;
LDev.dev_class := lClassID;
LDev.dev_id := 0;
LDev.dev_name := NameEd.Text;
LDev.dev_address := AddressEd.Text;
LDev.dev_comment := CommentRiEd.Text;
LDev.dev_ch1_enabled := Ch1ChkBox.Checked;
LDev.dev_ch2_enabled := Ch2ChkBox.Checked;
LDev.dev_ch3_enabled := Ch3ChkBox.Checked;
if LDev.dev_ch1_enabled then
begin
LDev.dev_ch1_gas := Ch1GasCoBox.Text;
LDev.dev_ch1_span := strToFloat(Ch1SpanEd.Text);
LDev.dev_ch1_zero := strToFloat(Ch1ZeroEd.Text);
end
else begin
LDev.dev_ch1_gas := '';
LDev.dev_ch1_span := 0;
LDev.dev_ch1_zero := 0;
end;
if LDev.dev_ch2_enabled then
begin
LDev.dev_ch2_gas := Ch2GasCoBox.Text;
LDev.dev_ch2_span := strToFloat(Ch2SpanEd.Text);
LDev.dev_ch2_zero := strToFloat(Ch2ZeroEd.Text);
end
else begin
LDev.dev_ch2_gas := '';
LDev.dev_ch2_span := 0;
LDev.dev_ch2_zero := 0;
end;
if LDev.dev_ch3_enabled then
begin
LDev.dev_ch3_gas := Ch3GasCoBox.Text;
LDev.dev_ch3_span := strToFloat(Ch3SpanEd.Text);
LDev.dev_ch3_zero := strToFloat(Ch3ZeroEd.Text);
end
else begin
LDev.dev_ch3_gas := '';
LDev.dev_ch3_span := 0;
LDev.dev_ch3_zero := 0;
end;
if InsertIlmDeviceToDb(LDev) then
HintLab.Caption := 'Creating new cryostat succeeded.'
else
HintLab.Caption := 'Creating new cryostat failed.';
finally
UnlockAfterWorkingWithList;
InitForm;
end;
if (ilmCoBox.ItemIndex > 0) then
if (MessageDlg('Do you really want to change the cryostat settings?',
mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt, ilmCoBox do
try
LockWhileWorkingWithList;
LDev := GetDevItem(integer(Items.Objects[ItemIndex]));
if Assigned(LDev) then
begin
LDev.dev_class := lCLassID;
LDev.dev_name := NameEd.Text;
LDev.dev_address := AddressEd.Text;
LDev.dev_comment := CommentRiEd.Text;
LDev.dev_ch1_enabled := Ch1ChkBox.Checked;
LDev.dev_ch2_enabled := Ch2ChkBox.Checked;
LDev.dev_ch3_enabled := Ch3ChkBox.Checked;
if LDev.dev_ch1_enabled then
begin
LDev.dev_ch1_gas := Ch1GasCoBox.Text;
LDev.dev_ch1_span := strToFloat(Ch1SpanEd.Text);
LDev.dev_ch1_zero := strToFloat(Ch1ZeroEd.Text);
end
else begin
LDev.dev_ch1_gas := '';
LDev.dev_ch1_span := 0;
LDev.dev_ch1_zero := 0;
end;
if LDev.dev_ch2_enabled then
begin
LDev.dev_ch2_gas := Ch2GasCoBox.Text;
LDev.dev_ch2_span := strToFloat(Ch2SpanEd.Text);
LDev.dev_ch2_zero := strToFloat(Ch2ZeroEd.Text);
end
else begin
LDev.dev_ch2_gas := '';
LDev.dev_ch2_span := 0;
LDev.dev_ch2_zero := 0;
end;
if LDev.dev_ch3_enabled then
begin
LDev.dev_ch3_gas := Ch3GasCoBox.Text;
LDev.dev_ch3_span := strToFloat(Ch3SpanEd.Text);
LDev.dev_ch3_zero := strToFloat(Ch3ZeroEd.Text);
end
else begin
LDev.dev_ch3_gas := '';
LDev.dev_ch3_span := 0;
LDev.dev_ch3_zero := 0;
end;
if UpdateIlmDeviceToDb(LDev) then
HintLab.Caption := 'Update on ' + ilmCoBox.Text + ' succeeded.'
else
HintLab.Caption := 'Update on ' + ilmCoBox.Text + ' failed.';
end
else
HintLab.Caption := 'Update on ' + ilmCoBox.Text + ' failed.';
finally
LDev := nil;
UnlockAfterWorkingWithList;
InitForm;
end;
end
else ShowMessage('Invalid channel settings!')
else showMessage('Please enter the XBee address of the cryostat!')
else showMessage('Please enter a name for the cryostat!');
if Assigned(LDev) then LDev.Free;
end;
procedure TIlmDeviceForm.DeleteBtnClick(Sender: TObject);
begin
if (ilmCoBox.ItemIndex > 0) then
begin
if (MessageDlg('Do you really want to delete ' + ilmCoBox.Text + '?',
mtCustom, [mbYes, mbNo], 0) = mrYes) then
with FDataMgmt, ilmCoBox do
try
LockWhileWorkingWithList;
if DeleteIlmDeviceToDb(integer(Items.Objects[ItemIndex])) then
HintLab.Caption := ilmCoBox.Text + ' successfully deleted.'
else
HintLab.Caption := 'Database operation failed.';
finally
UnlockAfterWorkingWithList;
InitForm;
end;
end
else
MessageDLg('Select a cryostat to delete!', mtError, [mbOK], 0);
end;
end.