-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCAPI_Monitors.pas
580 lines (541 loc) · 18.3 KB
/
CAPI_Monitors.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
unit CAPI_Monitors;
interface
uses
CAPI_Utils,
CAPI_Types;
procedure Monitors_Get_AllNames(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
procedure Monitors_Get_AllNames_GR(); CDECL;
function Monitors_Get_FileName(): PAnsiChar; CDECL;
function Monitors_Get_First(): Integer; CDECL;
function Monitors_Get_Mode(): Integer; CDECL;
function Monitors_Get_Name(): PAnsiChar; CDECL;
function Monitors_Get_Next(): Integer; CDECL;
procedure Monitors_Reset(); CDECL;
procedure Monitors_ResetAll(); CDECL;
procedure Monitors_Sample(); CDECL;
procedure Monitors_Save(); CDECL;
procedure Monitors_Set_Mode(Value: Integer); CDECL;
procedure Monitors_Show(); CDECL;
procedure Monitors_Set_Name(const Value: PAnsiChar); CDECL;
procedure Monitors_Get_ByteStream(var ResultPtr: PByte; ResultCount: PAPISize); CDECL;
procedure Monitors_Get_ByteStream_GR(); CDECL;
function Monitors_Get_SampleCount(): Integer; CDECL;
procedure Monitors_SampleAll(); CDECL;
procedure Monitors_SaveAll(); CDECL;
function Monitors_Get_Count(): Integer; CDECL;
procedure Monitors_Process(); CDECL;
procedure Monitors_ProcessAll(); CDECL;
procedure Monitors_Get_Channel(var ResultPtr: PDouble; ResultCount: PAPISize; Index: Integer); CDECL;
procedure Monitors_Get_Channel_GR(Index: Integer); CDECL;
procedure Monitors_Get_dblFreq(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Monitors_Get_dblFreq_GR(); CDECL;
procedure Monitors_Get_dblHour(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Monitors_Get_dblHour_GR(); CDECL;
function Monitors_Get_FileVersion(): Integer; CDECL;
procedure Monitors_Get_Header(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
procedure Monitors_Get_Header_GR(); CDECL;
function Monitors_Get_NumChannels(): Integer; CDECL;
function Monitors_Get_RecordSize(): Integer; CDECL;
function Monitors_Get_Element(): PAnsiChar; CDECL;
procedure Monitors_Set_Element(const Value: PAnsiChar); CDECL;
function Monitors_Get_Terminal(): Integer; CDECL;
procedure Monitors_Set_Terminal(Value: Integer); CDECL;
// API extensions
function Monitors_Get_idx(): Integer; CDECL;
procedure Monitors_Set_idx(Value: Integer); CDECL;
implementation
uses
CAPI_Constants,
Monitor,
DSSGlobals,
SysUtils,
Classes,
Math,
DSSClass,
DSSHelper,
DSSObjectHelper;
type
SingleArray = array[1..100] of Single;
pSingleArray = ^SingleArray;
//------------------------------------------------------------------------------
function _activeObj(DSSPrime: TDSSContext; out obj: TMonitorObj): Boolean; inline;
begin
Result := False;
obj := NIL;
if InvalidCircuit(DSSPrime) then
Exit;
obj := DSSPrime.ActiveCircuit.Monitors.Active;
if obj = NIL then
begin
if DSS_CAPI_EXT_ERRORS then
begin
DoSimpleMsg(DSSPrime, 'No active %s object found! Activate one and retry.', ['Monitor'], 8989);
end;
Exit;
end;
Result := True;
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_AllNames(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
begin
DefaultResult(ResultPtr, ResultCount);
if InvalidCircuit(DSSPrime) then
Exit;
Generic_Get_AllNames(ResultPtr, ResultCount, DSSPrime.ActiveCircuit.Monitors, False);
end;
procedure Monitors_Get_AllNames_GR(); CDECL;
// Same as Monitors_Get_AllNames but uses global result (GR) pointers
begin
Monitors_Get_AllNames(DSSPrime.GR_DataPtr_PPAnsiChar, @DSSPrime.GR_Counts_PPAnsiChar[0])
end;
//------------------------------------------------------------------------------
function Monitors_Get_FileName(): PAnsiChar; CDECL;
var
pMon: TMonitorObj;
begin
Result := NIL;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := DSS_GetAsPAnsiChar(DSSPrime, PMon.CSVFileName);
end;
//------------------------------------------------------------------------------
function Monitors_Get_First(): Integer; CDECL;
begin
Result := 0; // signify no more
if InvalidCircuit(DSSPrime) then
Exit;
Result := Generic_CktElement_Get_First(DSSPrime, DSSPrime.ActiveCircuit.Monitors);
end;
//------------------------------------------------------------------------------
function Monitors_Get_Next(): Integer; CDECL;
begin
Result := 0; // signify no more
if InvalidCircuit(DSSPrime) then
Exit;
Result := Generic_CktElement_Get_Next(DSSPrime, DSSPrime.ActiveCircuit.Monitors);
if Result <> 0 then Result := 1; //TODO: inconsistent with the rest
end;
//------------------------------------------------------------------------------
function Monitors_Get_Mode(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := PMon.Mode
end;
//------------------------------------------------------------------------------
function Monitors_Get_Name(): PAnsiChar; CDECL;
var
pMon: TMonitorObj;
begin
Result := NIL;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := DSS_GetAsPAnsiChar(DSSPrime, PMon.Name)
end;
//------------------------------------------------------------------------------
procedure Monitors_Reset(); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
PMon.ResetIt();
end;
//------------------------------------------------------------------------------
procedure Monitors_ResetAll(); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
DSSPrime.MonitorClass.ResetAll;
end;
//------------------------------------------------------------------------------
procedure Monitors_Sample(); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
PMon.TakeSample();
end;
//------------------------------------------------------------------------------
procedure Monitors_Save(); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
PMon.Save(); // TranslateToCSV(False);
end;
//------------------------------------------------------------------------------
procedure Monitors_Set_Mode(Value: Integer); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
PMon.Mode := Value;
PMon.ResetIt(); // Always reset the monitor after a Mode change
end;
//------------------------------------------------------------------------------
procedure Monitors_Show(); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
PMon.TranslateToCSV(TRUE);
end;
//------------------------------------------------------------------------------
procedure Monitors_Set_Name(const Value: PAnsiChar); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
if DSSPrime.MonitorClass.SetActive(Value) then
begin
DSSPrime.ActiveCircuit.ActiveCktElement := DSSPrime.MonitorClass.ElementList.Active;
DSSPrime.ActiveCircuit.Monitors.Get(DSSPrime.MonitorClass.Active);
end
else
begin
DoSimpleMsg(DSSPrime, 'Monitor "%s" not found in Active Circuit.', [Value], 5004);
end;
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_ByteStream(var ResultPtr: PByte; ResultCount: PAPISize); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
DSS_RecreateArray_PByte(ResultPtr, ResultCount, pmon.MonitorStream.Size);
pmon.MonitorStream.Seek(0, soFromBeginning);
pmon.MonitorStream.Read(ResultPtr^, pmon.MonitorStream.Size); // Move it all over
// leaves stream at the end
end;
procedure Monitors_Get_ByteStream_GR(); CDECL;
// Same as Monitors_Get_ByteStream but uses global result (GR) pointers
begin
Monitors_Get_ByteStream(DSSPrime.GR_DataPtr_PByte, @DSSPrime.GR_Counts_PByte[0])
end;
//------------------------------------------------------------------------------
function Monitors_Get_SampleCount(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := pMon.SampleCount;
end;
//------------------------------------------------------------------------------
procedure Monitors_SampleAll(); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
DSSPrime.MonitorClass.SampleAll;
end;
//------------------------------------------------------------------------------
procedure Monitors_SaveAll(); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
DSSPrime.MonitorClass.SaveAll;
end;
//------------------------------------------------------------------------------
function Monitors_Get_Count(): Integer; CDECL;
begin
Result := 0;
if InvalidCircuit(DSSPrime) then
Exit;
Result := DSSPrime.ActiveCircuit.Monitors.Count;
end;
//------------------------------------------------------------------------------
procedure Monitors_Process(); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
pMon.PostProcess();
end;
//------------------------------------------------------------------------------
procedure Monitors_ProcessAll(); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
DSSPrime.MonitorClass.PostProcessAll;
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_Channel(var ResultPtr: PDouble; ResultCount: PAPISize; Index: Integer); CDECL;
// Return an array of doubles for selected channel
var
Result: PDoubleArray0;
i: Integer;
pMon: TMonitorObj;
SngBuffer: pSingleArray;
AllocSize: Integer;
begin
DefaultResult(ResultPtr, ResultCount);
if not _activeObj(DSSPrime, pMon) then
Exit;
if pMon.SampleCount <= 0 then
Exit;
pMon.MonitorStream.Seek(256 + 4 * 4, soFromBeginning); // Skip header
if (Index < 1) or (Index > pMon.RecordSize {NumChannels}) then
begin
DoSimpleMsg(DSSPrime,
'Monitors.Channel: invalid channel index (%d), monitor "%s" has %d channels.',
[Index, pMon.Name, pMon.RecordSize],
5888);
Exit;
end;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, pMon.SampleCount);
AllocSize := Sizeof(Single) * (pMon.RecordSize + 2); // Include Hour and Second fields
Index := Index + 2; // Skip Hour and Second fields
SngBuffer := Allocmem(AllocSize); // Need a buffer to convert from float32 to float64
for i := 1 to pMon.SampleCount do
begin
pMon.MonitorStream.Read(sngBuffer^[1], AllocSize); // read rest of record
Result[i - 1] := sngBuffer^[Index];
end;
Reallocmem(SngBuffer, 0); // Dispose of buffer
end;
procedure Monitors_Get_Channel_GR(Index: Integer); CDECL;
// Same as Monitors_Get_Channel but uses global result (GR) pointers
begin
Monitors_Get_Channel(DSSPrime.GR_DataPtr_PDouble, DSSPrime.GR_Counts_PDouble, Index)
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_dblFreq(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return an array of doubles for frequence for Harmonic solutions
var
Result: PDoubleArray0;
k, i: Integer;
FirstCol: String;
pMon: TMonitorObj;
SngBuffer: pSingleArray;
freq: Single;
s: Single;
AllocSize: Integer;
begin
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, (0) + 1);
if not _activeObj(DSSPrime, pMon) then
Exit;
if pMon.SampleCount <= 0 then
Exit;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, (pMon.SampleCount - 1) + 1);
pMon.MonitorStream.Seek(256 + 4 * 4, soFromBeginning); // Skip header
FirstCol := pMon.Header.Strings[0];
// check first col to see if it is "Freq" for harmonics solution
if SysUtils.CompareText(FirstCol, 'freq') = 0 then
begin
AllocSize := Sizeof(Single) * pMon.RecordSize;
SngBuffer := Allocmem(AllocSize);
k := 0;
for i := 1 to pMon.SampleCount do
begin
with pMon.MonitorStream do
begin
Read(freq, SizeOf(freq)); // frequency
Read(s, SizeOf(s)); // harmonic
Read(sngBuffer^[1], AllocSize); // read rest of record
end;
Result[k] := freq;
inc(k);
end;
Reallocmem(SngBuffer, 0); // Dispose of buffer
end
else
begin // Not harmonic solution, so return nil array
pMon.MonitorStream.Seek(0, soFromEnd); // leave stream at end
end;
end;
procedure Monitors_Get_dblFreq_GR(); CDECL;
// Same as Monitors_Get_dblFreq but uses global result (GR) pointers
begin
Monitors_Get_dblFreq(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_dblHour(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return an array of doubles for time in hours
var
Result: PDoubleArray0;
k, i: Integer;
FirstCol: String;
pMon: TMonitorObj;
SngBuffer: pSingleArray;
hr: Single;
s: Single;
AllocSize: Integer;
begin
DefaultResult(ResultPtr, ResultCount);
if not _activeObj(DSSPrime, pMon) then
Exit;
if pMon.SampleCount <= 0 then
Exit;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, pMon.SampleCount);
pMon.MonitorStream.Seek(256 + 4 * 4, soFromBeginning); // Skip header
FirstCol := pMon.Header.Strings[0];
// check first col to see if it is "Hour"
if Sysutils.CompareText(FirstCol, 'hour') = 0 then
begin
AllocSize := Sizeof(Single) * pMon.RecordSize;
SngBuffer := Allocmem(AllocSize);
k := 0;
for i := 1 to pMon.SampleCount do
begin
with pMon.MonitorStream do
begin
Read(hr, SizeOf(hr)); // Hour
Read(s, SizeOf(s)); // Seconds past the hour
Read(sngBuffer^[1], AllocSize); // read rest of record
end;
Result[k] := hr + s / 3600.0;
inc(k);
end;
Reallocmem(SngBuffer, 0); // Dispose of buffer
end
else
begin // Not time solution, so return nil array
pMon.MonitorStream.Seek(0, soFromEnd); // leave stream at end
end;
end;
procedure Monitors_Get_dblHour_GR(); CDECL;
// Same as Monitors_Get_dblHour but uses global result (GR) pointers
begin
Monitors_Get_dblHour(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
function Monitors_Get_FileVersion(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := pMon.FileVersion;
end;
//------------------------------------------------------------------------------
procedure Monitors_Get_Header(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
// Variant list of strings with names of all channels
var
Result: PPAnsiCharArray0;
k: Integer;
ListSize: Integer;
pMon: TMonitorObj;
begin
DefaultResult(ResultPtr, ResultCount);
if not _activeObj(DSSPrime, pMon) then
Exit;
if pMon.RecordSize <= 0 then
Exit;
ListSize := pMon.RecordSize;
DSS_RecreateArray_PPAnsiChar(Result, ResultPtr, ResultCount, ListSize);
with DSSPrime.ActiveCircuit do
begin
k := 0;
while k < ListSize do
begin
Result[k] := DSS_CopyStringAsPChar(pMon.Header.Strings[k + 2]);
Inc(k);
end;
end;
end;
procedure Monitors_Get_Header_GR(); CDECL;
// Same as Monitors_Get_Header but uses global result (GR) pointers
begin
Monitors_Get_Header(DSSPrime.GR_DataPtr_PPAnsiChar, @DSSPrime.GR_Counts_PPAnsiChar[0])
end;
//------------------------------------------------------------------------------
function Monitors_Get_NumChannels(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := pMon.RecordSize;
end;
//------------------------------------------------------------------------------
function Monitors_Get_RecordSize(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := pMon.RecordSize;
end;
//------------------------------------------------------------------------------
function Monitors_Get_Element(): PAnsiChar; CDECL;
var
pMon: TMonitorObj;
begin
Result := NIL;
if not _activeObj(DSSPrime, pMon) then
Exit;
if pMon.MeteredElement <> NIL then
Result := DSS_GetAsPAnsiChar(DSSPrime, AnsiLowerCase(pMon.MeteredElement.FullName));
end;
//------------------------------------------------------------------------------
procedure Monitors_Set_Element(const Value: PAnsiChar); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
pMon.ParsePropertyValue(ord(TMonitorProp.element), Value);
pMon.SetAsNextSeq(ord(TMonitorProp.Element));
pMon.RecalcElementData;
end;
//------------------------------------------------------------------------------
function Monitors_Get_Terminal(): Integer; CDECL;
var
pMon: TMonitorObj;
begin
Result := 0;
if not _activeObj(DSSPrime, pMon) then
Exit;
Result := pMon.MeteredTerminal;
end;
//------------------------------------------------------------------------------
procedure Monitors_Set_Terminal(Value: Integer); CDECL;
var
pMon: TMonitorObj;
begin
if not _activeObj(DSSPrime, pMon) then
Exit;
pMon.MeteredTerminal := Value;
pMon.RecalcElementData();
end;
//------------------------------------------------------------------------------
function Monitors_Get_idx(): Integer; CDECL;
begin
Result := 0;
if InvalidCircuit(DSSPrime) then
Exit;
Result := DSSPrime.ActiveCircuit.Monitors.ActiveIndex
end;
//------------------------------------------------------------------------------
procedure Monitors_Set_idx(Value: Integer); CDECL;
var
pMonitor: TMonitorObj;
begin
if InvalidCircuit(DSSPrime) then
Exit;
pMonitor := DSSPrime.ActiveCircuit.Monitors.Get(Value);
if pMonitor = NIL then
begin
DoSimpleMsg(DSSPrime, 'Invalid %s index: "%d".', ['Monitor', Value], 656565);
Exit;
end;
DSSPrime.ActiveCircuit.ActiveCktElement := pMonitor;
end;
//------------------------------------------------------------------------------
end.