-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerConfigs.pas
More file actions
551 lines (459 loc) · 16.5 KB
/
ControllerConfigs.pas
File metadata and controls
551 lines (459 loc) · 16.5 KB
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
unit ControllerConfigs;
interface
uses
System.SysUtils, System.Classes, Vcl.Forms, Vcl.Controls, Vcl.ExtCtrls,
UtilsTypes, System.JSON, JSONHelpers;
type
TControllerConfigsClass = class
private
class var FInstance: TControllerConfigsClass;
// è¤°æ’³å¢ é–«ï¿½ï¿½è…‘é¨å‹«î˜²é‰å‚šç˜éŽ¬Ñ…è¢«ï¿½?
FCurrentPropertyType: TComplexPropertyType;
// è¤°æ’³å¢ ç¼‚ï¿½ï¿½ç·«é¨å‡§SON瀵硅ï¿?
FCurrentJson: TJSONObject;
// è¤°æ’³å¢ ç¼‚ï¿½ï¿½ç·«é¨å‹«ç˜éŽ¬Ñƒæ‚•ç»?
FCurrentPropertyName: string;
// é–è·ºî†é£ã„©æ½°é‰åŒ¡ç´é¢ã„¤ç°¬é€å‰§ç–†ï¿½ï¿½ï¿½æ ¬ç·«é£â€µrame
FParentPanel: TPanel;
// è¤°æ’³å¢ ç¼‚ï¿½ï¿½ç·«é£â€µrame
FCurrentEditorFrame: TFrame;
// 閫氱敤çžç‚´â‚¬Ñ…ç´ªæˆæˆç•¬éŽ´æ„¬æ´–ç’‹?
FOnEditComplete: TNotifyEvent;
// é’æ¶˜ï¿½ï¿½ï¿½éšå‹è¢«é¨å¬¬ç´ªæˆæˆæ«’é¨å‹¬æŸŸå¨‰?
function CreateEditorFrame(PropertyType: TComplexPropertyType): TFrame;
// é”犺���JSONé版åµé’ï¿½ï¿½ï¿½ç´ªæˆæˆï¿?
procedure LoadJsonToEditor(Editor: TFrame; Json: TJSONObject);
// ï¿½ï¿½ï¿½åº£ç´ªæˆæˆæ«’淇濆瓨é版åµé’癑SON
procedure SaveEditorToJson(Editor: TFrame; var Json: TJSONObject);
constructor Create;
public
// é—æ›šç·¥ç’���棶é‚è§„ï¿?
class function GetInstance: TControllerConfigsClass;
// éè§„åµTagé„å‰§ãšæ¾¶å¶†æ½…çžç‚´â‚¬Ñ‡åŽ¤ï¿½ï¿½ï¿½î†¾æ™«é—ˆ?
procedure ShowConfigByTag(Tag: Integer);
// ç’剧疆é–è·ºî†é£ã„©æ½°é‰?
procedure SetParentPanel(Panel: TPanel);
// ï¿½ï¿½ï¿½æ ¬ç·«æ¾¶å¶†æ½…çžç‚´ï¿?
procedure EditComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; Json: TJSONObject; OnComplete: TNotifyEvent);
// é’æ¶˜ï¿½ï¿½ï¿½é‚���殑澶嶆潅çžç‚´â‚¬?
function CreateComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; OnComplete: TNotifyEvent): TJSONObject;
// æ·‡æ¿†ç“¨è¤°æ’³å¢ ï¿½ï¿½ï¿½æ ¬ç·«é¨å‹«ç˜ï¿?
procedure SaveCurrentProperty;
// 陿 ¨ï¿½Æ·è¤°æ’³å¢ ï¿½ï¿½ï¿½æ ¬ï¿?
procedure CancelCurrentEdit;
// 閲婃�Ä璧勬簮
destructor Destroy; override;
// çžç‚´ï¿?
function GetCurrentPropertyType: TComplexPropertyType;
function GetCurrentJson: TJSONObject;
function GetCurrentPropertyName: string;
property CurrentPropertyType: TComplexPropertyType read GetCurrentPropertyType;
property CurrentJson: TJSONObject read GetCurrentJson;
property CurrentPropertyName: string read GetCurrentPropertyName;
end;
// éㄥ眬ç’���棶ï¿?
function GetControllerConfigs: TControllerConfigsClass;
implementation
uses
Vcl.Dialogs, System.Generics.Collections, ConfigFrameBase,
FrameBgDrawEditor, FrameVideoClipEditor, FrameVideoEditor, FrameDateTimeRangeEditor,
FrameKeyValueDictEditor, FrameUrlConfigEditor, FrameNetConfigEditor, FrameGeoLocationEditor,
FrameEncryptEditor;
var
// 淇濆瓨鎵€éˆï¿½ï¿½åž±ï¿½Ô¤è™¹æ®‘Frame���岀敤浜�ȡ悗������é€?
FrameList: TList<TFrame>;
function GetControllerConfigs: TControllerConfigsClass;
begin
Result := TControllerConfigsClass.GetInstance;
end;
{ TControllerConfigsClass }
constructor TControllerConfigsClass.Create;
begin
inherited;
FCurrentJson := nil;
FCurrentEditorFrame := nil;
end;
destructor TControllerConfigsClass.Destroy;
begin
// 娓呯悊璧勬簮
if Assigned(FCurrentJson) then
FCurrentJson.Free;
if Assigned(FCurrentEditorFrame) then
FCurrentEditorFrame.Free;
inherited;
end;
function TControllerConfigsClass.GetCurrentPropertyType: TComplexPropertyType;
begin
Result := FCurrentPropertyType;
end;
function TControllerConfigsClass.GetCurrentJson: TJSONObject;
begin
Result := FCurrentJson;
end;
function TControllerConfigsClass.GetCurrentPropertyName: string;
begin
Result := FCurrentPropertyName;
end;
class function TControllerConfigsClass.GetInstance: TControllerConfigsClass;
begin
if not Assigned(FInstance) then
FInstance := TControllerConfigsClass.Create;
Result := FInstance;
end;
procedure TControllerConfigsClass.SetParentPanel(Panel: TPanel);
begin
FParentPanel := Panel;
end;
procedure TControllerConfigsClass.ShowConfigByTag(Tag: Integer);
var
CPT: TComplexPropertyType;
PropertyName: string;
Json: TJSONObject;
begin
// 获å–��æ‚属性类ï¿?
CPT := TComplexPropertyType(Tag);
// 获å–属性å�ư(用户���入ï¿?
PropertyName := '';
if not InputQuery('New property', 'Enter property name', PropertyName) then
Exit;
if PropertyName = '' then
Exit;
// ���建�İ的��æ‚属ï¿?
Json := CreateComplexProperty(CPT, PropertyName, nil);
// �Ǿ示æˆåŠŸæ¶ˆæ¯
if Assigned(Json) then
ShowMessage('Property ' + PropertyName + ' added successfully');
end;
function TControllerConfigsClass.CreateEditorFrame(
PropertyType: TComplexPropertyType): TFrame;
begin
Result := nil;
// Create editor frame based on property type
case PropertyType of
// Priority complex properties
cptBgDraw, cptTextOnBg, cptImageOnBg, cptCaptionOnBg:
begin
Result := TFrameBgDrawEditor.Create(nil);
end;
cptVideoClip:
begin
Result := TFrameVideoClipEditor.Create(nil);
end;
cptVideo:
begin
Result := TFrameVideoEditor.Create(nil);
end;
// New complex property editors
cptDateTimeRange:
begin
Result := TFrameDateTimeRangeEditor.Create(nil);
end;
cptKeyValueDict:
begin
Result := TFrameKeyValueDictEditor.Create(nil);
end;
cptUrlConfig:
begin
Result := TFrameUrlConfigEditor.Create(nil);
end;
cptNetConfig:
begin
Result := TFrameNetConfigEditor.Create(nil);
end;
cptGeoLocation:
begin
Result := TFrameGeoLocationEditor.Create(nil);
end;
cptEncrypt:
begin
Result := TFrameEncryptEditor.Create(nil);
end;
// Other complex properties
else
begin
ShowMessage('Editor for property type ' + IntToStr(Ord(PropertyType)) + ' not implemented yet');
end;
end;
// If Frame was created, add it to the list
if Assigned(Result) and not Assigned(FrameList) then
FrameList := TList<TFrame>.Create;
if Assigned(Result) and Assigned(FrameList) then
FrameList.Add(Result);
end;
procedure TControllerConfigsClass.EditComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; Json: TJSONObject; OnComplete: TNotifyEvent);
begin
// æ·‡æ¿†ç“¨è¤°æ’³å¢ é˜èˆµï¿?
FCurrentPropertyType := PropertyType;
FCurrentPropertyName := PropertyName;
// é–²å©ƒï¿½Äæ¶”å¬ªå¢ é¨å‡§SON瀵硅ï¿?
if Assigned(FCurrentJson) then
FCurrentJson.Free;
// 澶嶅埗JSON瀵硅�
if Assigned(Json) then
FCurrentJson := TJSONObject(Json.Clone)
else
FCurrentJson := TJSONObject.Create;
// é–²å©ƒï¿½Äæ¶”å¬ªå¢ é¨å‹ç´ªæˆæˆæ«’
if Assigned(FCurrentEditorFrame) then
begin
FCurrentEditorFrame.Free;
FCurrentEditorFrame := nil;
end;
// é’æ¶˜ï¿½ï¿½ï¿½é‚ï¿½ï¿½ï¿½æ®‘ï¿½ï¿½ï¿½æ ¬ç·«é£?
FCurrentEditorFrame := CreateEditorFrame(PropertyType);
// ç’剧疆é¥ç‚¶çšŸ
FOnEditComplete := OnComplete;
if Assigned(FCurrentEditorFrame) and Assigned(FParentPanel) then
begin
// ç’剧疆缂��緫é£â‰’arent
FCurrentEditorFrame.Parent := FParentPanel;
FCurrentEditorFrame.Align := alClient;
// é”犺���é版åµé’æ‰®ç´ªæˆæˆæ«’
LoadJsonToEditor(FCurrentEditorFrame, FCurrentJson);
// é„剧ãšç¼‚��緫�?
FCurrentEditorFrame.Visible := True;
end;
end;
function TControllerConfigsClass.CreateComplexProperty(PropertyType: TComplexPropertyType;
PropertyName: string; OnComplete: TNotifyEvent): TJSONObject;
var
Json: TJSONObject;
begin
// é’æ¶˜ï¿½ï¿½ï¿½é‚���殑JSON瀵硅ï¿?
Json := TJSONObject.Create;
// éè§„åµçžç‚´â‚¬Ñ…被é¨å¬ªï½žé呴粯ç’ゅ€?
case PropertyType of
// ���樺厛瀹炵幇é¨å‹«î˜²é‰å‚šç˜éެ?
cptBgDraw:
begin
Json.AddPair('_type', 'etBgDraw');
Json.AddPair('_id', 'etBgDraw.' + PropertyName);
Json.AddPair('background', '');
Json.AddPair('elements', TJSONArray.Create);
end;
cptTextOnBg:
begin
Json.AddPair('_type', 'etTextOnBg');
Json.AddPair('_id', 'etTextOnBg.' + PropertyName);
Json.AddPair('text', 'é‚囨湰éå‘î†');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('font', TJSONObject.Create);
end;
cptImageOnBg:
begin
Json.AddPair('_type', 'etImageOnBg');
Json.AddPair('_id', 'etImageOnBg.' + PropertyName);
Json.AddPair('image', '');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('scale', '1.0');
end;
cptCaptionOnBg:
begin
Json.AddPair('_type', 'etCaptionOnBg');
Json.AddPair('_id', 'etCaptionOnBg.' + PropertyName);
Json.AddPair('text', '瀛�箷é‚ͼ�湰');
Json.AddPair('x', '0');
Json.AddPair('y', '0');
Json.AddPair('font', TJSONObject.Create);
Json.AddPair('duration', '5.0');
end;
cptVideoClip:
begin
Json.AddPair('_type', 'etVideoClip');
Json.AddPair('_id', 'etVideoClip.' + PropertyName);
Json.AddPair('background', '');
Json.AddPair('duration', '10.0');
Json.AddPair('fps', '30');
Json.AddPair('audio', '');
Json.AddPair('captions', TJSONArray.Create);
end;
cptVideo:
begin
Json.AddPair('_type', 'etVideo');
Json.AddPair('_id', 'etVideo.' + PropertyName);
Json.AddPair('cover', '');
Json.AddPair('ending', '');
Json.AddPair('bg_directory', '');
Json.AddPair('audio_directory', '');
Json.AddPair('subtitle_file', '');
Json.AddPair('media_settings', TJSONObject.Create);
Json.AddPair('clips', TJSONArray.Create);
end;
// 邿¿î–ƒé¨å‹«î˜²é‰å‚šç˜éެ?
cptDateTimeRange:
begin
Json.AddPair('_type', 'etDateTimeRange');
Json.AddPair('_id', 'etDateTimeRange.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('start_date', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Now));
Json.AddPair('end_date', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Now + 1));
Json.AddPair('use_time', TJSONBool.Create(True));
Json.AddPair('duration_seconds', TJSONNumber.Create(86400)); // 榛���?
Json.AddPair('repeat', TJSONBool.Create(False));
Json.AddPair('repeat_type', TJSONNumber.Create(0)); // 姣忓�
Json.AddPair('repeat_interval', TJSONNumber.Create(1));
Json.AddPair('enable_notification', TJSONBool.Create(False));
Json.AddPair('notify_before', TJSONNumber.Create(15));
Json.AddPair('notify_unit', '����');
end;
cptKeyValueDict:
begin
Json.AddPair('_type', 'etKeyValueDict');
Json.AddPair('_id', 'etKeyValueDict.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('enable_type_check', TJSONBool.Create(False));
Json.AddPair('default_type', '�ַ���');
Json.AddPair('items', TJSONArray.Create);
end;
cptUrlConfig:
begin
Json.AddPair('_type', 'etUrlConfig');
Json.AddPair('_id', 'etUrlConfig.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('base_url', '');
Json.AddPair('protocol', 'https');
Json.AddPair('use_ssl', TJSONBool.Create(True));
Json.AddPair('port', '443');
Json.AddPair('timeout', '30000');
Json.AddPair('enable_proxy', TJSONBool.Create(False));
Json.AddPair('headers', TJSONArray.Create);
Json.AddPair('parameters', TJSONArray.Create);
end;
cptNetConfig:
begin
Json.AddPair('_type', 'etNetConfig');
Json.AddPair('_id', 'etNetConfig.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('dhcp', TJSONBool.Create(True));
Json.AddPair('ip_address', '');
Json.AddPair('subnet_mask', '');
Json.AddPair('gateway', '');
Json.AddPair('mac_address', '');
Json.AddPair('ipv6', TJSONBool.Create(False));
Json.AddPair('network_name', '');
Json.AddPair('network_type', TJSONNumber.Create(0));
Json.AddPair('mtu', '1500');
Json.AddPair('auto_connect', TJSONBool.Create(True));
Json.AddPair('enable_proxy', TJSONBool.Create(False));
Json.AddPair('custom_dns', TJSONBool.Create(False));
end;
cptGeoLocation:
begin
Json.AddPair('_type', 'etGeoLocation');
Json.AddPair('_id', 'etGeoLocation.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('latitude', '39.9042'); // æ¦›ï¿½î…»é–æ¤¾å«é§æ„ï¿?
Json.AddPair('longitude', '116.4074');
Json.AddPair('altitude', '0');
Json.AddPair('accuracy', '10');
Json.AddPair('location_name', '');
Json.AddPair('location_type', '����');
Json.AddPair('is_favorite', TJSONBool.Create(False));
Json.AddPair('formatted_address', '');
Json.AddPair('show_on_map', TJSONBool.Create(True));
Json.AddPair('map_provider', 'é§æƒ§å®³é¦æ¿æµ˜');
Json.AddPair('default_zoom', TJSONNumber.Create(12));
end;
cptEncrypt:
begin
Json.AddPair('_type', 'etEncrypt');
Json.AddPair('_id', 'etEncrypt.' + PropertyName);
Json.AddPair('name', PropertyName);
Json.AddPair('description', '');
Json.AddPair('encryption_type', '�ԳƼ���');
Json.AddPair('algorithm', 'AES');
Json.AddPair('key_size', '256 λ');
Json.AddPair('mode', 'CBC');
Json.AddPair('padding', 'PKCS7');
Json.AddPair('use_iv', TJSONBool.Create(True));
Json.AddPair('default_iv', TJSONBool.Create(True));
Json.AddPair('key_format', 'Base64');
Json.AddPair('password', '');
Json.AddPair('salt', '');
Json.AddPair('iterations', TJSONNumber.Create(10000));
Json.AddPair('key_derivation', 'PBKDF2');
end;
// éæœµç²¬æ¾¶å¶†æ½…çžç‚´ï¿?
else
begin
Json.AddPair('_type', 'etObject');
Json.AddPair('_id', 'etObject.' + PropertyName);
Json.AddPair('name', PropertyName);
end;
end;
// ï¿½ï¿½ï¿½æ ¬ç·«çžç‚´â‚¬?
EditComplexProperty(PropertyType, PropertyName, Json, OnComplete);
Result := Json;
end;
procedure TControllerConfigsClass.LoadJsonToEditor(Editor: TFrame; Json: TJSONObject);
begin
// å®¸æŸ¥×¢ï¿½éŽ´æ„®æ®‘ï¿½ï¿½ï¿½æ ¬ç·«é£?
if Editor is TBaseConfigFrame then
begin
TBaseConfigFrame(Editor).SetJSONObject(Json);
TBaseConfigFrame(Editor).LoadFromJSON;
end
else
begin
ShowMessage('δ֪�ıà¼ï¿½ï¿½ï¿½ï¿½ï¿½Í£ï¿½ï¿½Þ·ï¿½ï¿½ï¿½ï¿½ï¿½JSON����');
end;
end;
procedure TControllerConfigsClass.SaveEditorToJson(Editor: TFrame; var Json: TJSONObject);
begin
// å®¸æŸ¥×¢ï¿½éŽ´æ„®æ®‘ï¿½ï¿½ï¿½æ ¬ç·«é£?
if Editor is TBaseConfigFrame then
begin
// éå ¢'淇滼SON瀵硅薄瀛樺ï¿?
if not Assigned(Json) then
Json := TJSONObject.Create;
// çå—™ç´ªæˆæˆæ«’éç‰ˆåµæ·‡æ¿†ç“¨é’癑SON
TBaseConfigFrame(Editor).SaveToJSON;
end
else
begin
ShowMessage('δ֪�ıà¼ï¿½ï¿½ï¿½ï¿½ï¿½Í£ï¿½ï¿½Þ·ï¿½ï¿½ï¿½ï¿½ï¿½JSON����');
end;
end;
procedure TControllerConfigsClass.SaveCurrentProperty;
begin
// æ·‡æ¿†ç“¨è¤°æ’³å¢ çžç‚´ï¿?
if Assigned(FCurrentEditorFrame) and Assigned(FCurrentJson) then
begin
SaveEditorToJson(FCurrentEditorFrame, FCurrentJson);
// 璋冪敤é¥ç‚¶çšŸ
if Assigned(FOnEditComplete) then
FOnEditComplete(Self);
end;
end;
procedure TControllerConfigsClass.CancelCurrentEdit;
begin
// 陿 ¨ï¿½Æ·è¤°æ’³å¢ ï¿½ï¿½ï¿½æ ¬ï¿?
if Assigned(FCurrentEditorFrame) then
begin
FCurrentEditorFrame.Visible := False;
// æ¿¡å‚›ç‰é—‡â‚¬ç‘•侊ç´é™îˆ™ï¿½ï¿½ï¿½é¦ã„¨ç¹–é–²å²„ï¿½ï¿½ï¿½é€æç¥«ï¿?
end;
end;
initialization
finalization
// å¨“å‘¯æ‚Šé—æ›šç·¥ç€¹ç‚°ï¿?
if Assigned(TControllerConfigsClass.FInstance) then
TControllerConfigsClass.FInstance.Free;
// 娓呯悊鎵€éˆï¿½ï¿½åž±ï¿½Ô¤è™¹æ®‘Frame
if Assigned(FrameList) then
begin
for var Frame in FrameList do
Frame.Free;
FrameList.Free;
end;
end.