generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationCell.cs
430 lines (370 loc) · 19 KB
/
ConfigurationCell.cs
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
//******************************************************************************************************
// ConfigurationCell.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/05/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 05/19/2011 - J. Ritchie Carroll
// Removed generic dictionary from serialization since these can't be SOAP serialized.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Gemstone.Numeric.EE;
using Gemstone.Timeseries;
namespace Gemstone.PhasorProtocols.Anonymous
{
/// <summary>
/// Represents a protocol independent implementation of a <see cref="IConfigurationCell"/> that can be sent or received.
/// </summary>
[Serializable]
public class ConfigurationCell : ConfigurationCellBase, IDevice
{
#region [ Members ]
// Fields
private DataFormat m_analogDataFormat;
private DataFormat m_frequencyDataFormat;
private DataFormat m_phasorDataFormat;
private CoordinateFormat m_phasorCoordinateFormat;
// Add cached signal type and statistical tracking information to our protocol independent configuration cell
private readonly string[][] m_generatedSignalReferenceCache;
private readonly MeasurementKey[][] m_generatedMeasurementKeyCache;
// Keep track of the object that was last used to lookup the MeasurementMetadata. If objects changes, cache must be recomputed.
private Dictionary<string, MeasurementKey> m_keyLookupObject;
private Ticks m_lastReportTime;
private long m_totalFrames;
private long m_dataQualityErrors;
private long m_timeQualityErrors;
private long m_deviceErrors;
private long m_measurementsReceived;
private long m_measurementsExpected;
private long m_measurementsWithError;
private long m_measurementsDefined;
private object m_source;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new <see cref="ConfigurationCell"/> from specified parameters.
/// </summary>
/// <param name="idCode">The numeric ID code for this <see cref="ConfigurationCell"/>.</param>
public ConfigurationCell(ushort idCode)
: this(null, idCode)
{
}
/// <summary>
/// Creates a new <see cref="ConfigurationCell"/> from specified parameters.
/// </summary>
/// <param name="parent">The reference to parent <see cref="ConfigurationFrame"/> of this <see cref="ConfigurationCell"/>.</param>
/// <param name="idCode">The numeric ID code for this <see cref="ConfigurationCell"/>.</param>
public ConfigurationCell(ConfigurationFrame parent, ushort idCode)
: base(parent, idCode, int.MaxValue, int.MaxValue, int.MaxValue)
{
// Create a cached signal reference dictionary for generated signal references
m_generatedSignalReferenceCache = new string[Enum.GetValues(typeof(SignalKind)).Length][];
m_generatedMeasurementKeyCache = new MeasurementKey[Enum.GetValues(typeof(SignalKind)).Length][];
m_analogDataFormat = DataFormat.FloatingPoint;
m_frequencyDataFormat = DataFormat.FloatingPoint;
m_phasorDataFormat = DataFormat.FloatingPoint;
m_phasorCoordinateFormat = CoordinateFormat.Polar;
}
/// <summary>
/// Creates a new <see cref="ConfigurationCell"/> from serialization parameters.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> with populated with data.</param>
/// <param name="context">The source <see cref="StreamingContext"/> for this deserialization.</param>
protected ConfigurationCell(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// Create a cached signal reference dictionary for generated signal references
m_generatedSignalReferenceCache = new string[Enum.GetValues(typeof(SignalKind)).Length][];
m_generatedMeasurementKeyCache = new MeasurementKey[Enum.GetValues(typeof(SignalKind)).Length][];
// Deserialize configuration cell
m_lastReportTime = info.GetInt64("lastReportTime");
m_analogDataFormat = (DataFormat)info.GetValue("analogDataFormat", typeof(DataFormat));
m_frequencyDataFormat = (DataFormat)info.GetValue("frequencyDataFormat", typeof(DataFormat));
m_phasorDataFormat = (DataFormat)info.GetValue("phasorDataFormat", typeof(DataFormat));
m_phasorCoordinateFormat = (CoordinateFormat)info.GetValue("phasorCoordinateFormat", typeof(CoordinateFormat));
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets the <see cref="DataFormat"/> for the <see cref="IAnalogDefinition"/> objects in the <see cref="ConfigurationCellBase.AnalogDefinitions"/> of this <see cref="ConfigurationCell"/>.
/// </summary>
public override DataFormat AnalogDataFormat
{
get => m_analogDataFormat;
set => m_analogDataFormat = value;
}
/// <summary>
/// Gets or sets the <see cref="DataFormat"/> of the <see cref="FrequencyDefinition"/> of this <see cref="ConfigurationCell"/>.
/// </summary>
public override DataFormat FrequencyDataFormat
{
get => m_frequencyDataFormat;
set => m_frequencyDataFormat = value;
}
/// <summary>
/// Gets or sets the <see cref="DataFormat"/> for the <see cref="IPhasorDefinition"/> objects in the <see cref="ConfigurationCellBase.PhasorDefinitions"/> of this <see cref="ConfigurationCell"/>.
/// </summary>
public override DataFormat PhasorDataFormat
{
get => m_phasorDataFormat;
set => m_phasorDataFormat = value;
}
/// <summary>
/// Gets or sets the <see cref="CoordinateFormat"/> for the <see cref="IPhasorDefinition"/> objects in the <see cref="ConfigurationCellBase.PhasorDefinitions"/> of this <see cref="ConfigurationCell"/>.
/// </summary>
public override CoordinateFormat PhasorCoordinateFormat
{
get => m_phasorCoordinateFormat;
set => m_phasorCoordinateFormat = value;
}
/// <summary>
/// Gets or sets last report time of this <see cref="ConfigurationCell"/>.
/// </summary>
public Ticks LastReportTime
{
get => m_lastReportTime;
set => m_lastReportTime = value;
}
/// <summary>
/// Gets or sets total frames of this <see cref="ConfigurationCell"/>.
/// </summary>
public long TotalFrames
{
get => m_totalFrames;
set => m_totalFrames = value;
}
/// <summary>
/// Gets or sets total data quality errors of this <see cref="ConfigurationCell"/>.
/// </summary>
public long DataQualityErrors
{
get => m_dataQualityErrors;
set => m_dataQualityErrors = value;
}
/// <summary>
/// Gets or sets total time quality errors of this <see cref="ConfigurationCell"/>.
/// </summary>
public long TimeQualityErrors
{
get => m_timeQualityErrors;
set => m_timeQualityErrors = value;
}
/// <summary>
/// Gets or sets total device errors of this <see cref="ConfigurationCell"/>.
/// </summary>
public long DeviceErrors
{
get => m_deviceErrors;
set => m_deviceErrors = value;
}
/// <summary>
/// Gets or sets total measurements received for this <see cref="ConfigurationCell"/>.
/// </summary>
public long MeasurementsReceived
{
get => m_measurementsReceived;
set => m_measurementsReceived = value;
}
/// <summary>
/// Gets or sets total measurements expected to have been received for this <see cref="ConfigurationCell"/>.
/// </summary>
public long MeasurementsExpected
{
get => m_measurementsExpected;
set => m_measurementsExpected = value;
}
/// <summary>
/// Gets or sets the number of measurements received while this <see cref="IDevice"/> was reporting errors.
/// </summary>
public long MeasurementsWithError
{
get => m_measurementsWithError;
set => m_measurementsWithError = value;
}
/// <summary>
/// Gets or sets the number of measurements (per frame) defined for this <see cref="ConfigurationCell"/>.
/// </summary>
public long MeasurementsDefined
{
get => m_measurementsDefined;
set => m_measurementsDefined = value;
}
/// <summary>
/// Gets or sets the reference to the source of this <see cref="ConfigurationCell"/>.
/// </summary>
public object Source
{
get => m_source;
set => m_source = value;
}
/// <summary>
/// Gets the maximum length of the <see cref="ConfigurationCellBase.StationName"/> of this <see cref="ConfigurationCell"/>.
/// </summary>
/// <remarks>
/// This length is not restricted for anonymous protocol definitions.
/// </remarks>
public override int MaximumStationNameLength => int.MaxValue;
#endregion
#region [ Methods ]
/// <summary>
/// Get signal reference for specified <see cref="SignalKind"/>.
/// </summary>
/// <param name="type"><see cref="SignalKind"/> to request signal reference for.</param>
/// <returns>Signal reference of given <see cref="SignalKind"/>.</returns>
public string GetSignalReference(SignalKind type)
{
// We cache non-indexed signal reference strings so they don't need to be generated at each mapping call.
// This helps with performance since the mappings for each signal occur 30 times per second.
int typeIndex = (int)type;
// Look up synonym in dictionary based on signal type, if found return single element
string[] references = m_generatedSignalReferenceCache[typeIndex];
if (references is not null)
return references[0];
// Create a new signal reference array (for single element)
references = new string[1];
// Create and cache new non-indexed signal reference
references[0] = SignalReference.ToString(IDLabel, type);
// Cache generated signal synonym
m_generatedSignalReferenceCache[typeIndex] = references;
return references[0];
}
/// <summary>
/// Get signal reference for specified <see cref="SignalKind"/> and <paramref name="index"/>.
/// </summary>
/// <param name="type"><see cref="SignalKind"/> to request signal reference for.</param>
/// <param name="index">Index <see cref="SignalKind"/> to request signal reference for.</param>
/// <param name="count">Number of signals defined for this <see cref="SignalKind"/>.</param>
/// <returns>Signal reference of given <see cref="SignalKind"/> and <paramref name="index"/>.</returns>
public string GetSignalReference(SignalKind type, int index, int count)
{
// We cache indexed signal reference strings so they don't need to be generated at each mapping call.
// This helps with performance since the mappings for each signal occur 30 times per second.
// For speed purposes we intentionally do not validate that signalIndex falls within signalCount, be
// sure calling procedures are very careful with parameters...
int typeIndex = (int)type;
// Look up synonym in dictionary based on signal type
string[] references = m_generatedSignalReferenceCache[typeIndex];
if (references is not null)
{
// Verify signal count has not changed (we may have received new configuration from device)
if (count == references.Length)
{
// Create and cache new signal reference if it doesn't exist
return references[index] ?? (references[index] = SignalReference.ToString(IDLabel, type, index + 1));
}
}
// Create a new indexed signal reference array
references = new string[count];
// Create and cache new signal reference
references[index] = SignalReference.ToString(IDLabel, type, index + 1);
// Cache generated signal synonym array
m_generatedSignalReferenceCache[typeIndex] = references;
return references[index];
}
/// <summary>
/// Get <see cref="MeasurementMetadata"/> for specified <see cref="SignalKind"/>.
/// </summary>
/// <param name="lookup">A lookup table by signal reference.</param>
/// <param name="type"><see cref="SignalKind"/> to request signal reference for.</param>
/// <returns>The <see cref="MeasurementMetadata"/> for a given <see cref="SignalKind"/>; otherwise <c>null</c> if value does not exist.</returns>
public MeasurementMetadata GetMetadata(Dictionary<string, MeasurementKey> lookup, SignalKind type)
{
// Clear the cache if the lookup dictionary has changed.
// Since the instance of Lookup is effectively readonly as implemented in PhasorMeasurementMapper
// a simple reference check is all that is needed. If it could be modified, this would likely be a
// concurrent dictionary instead.
if (m_keyLookupObject != lookup)
{
Array.Clear(m_generatedMeasurementKeyCache, 0, m_generatedMeasurementKeyCache.Length);
m_keyLookupObject = lookup;
}
// Gets the cache for the supplied SignalKind
int typeIndex = (int)type;
MeasurementKey[] keyArray = m_generatedMeasurementKeyCache[typeIndex];
// If this SignalKind is null, create the sub array and generate all item lookups
if (keyArray is null)
{
keyArray = new MeasurementKey[1];
m_generatedMeasurementKeyCache[typeIndex] = keyArray;
string signalReference = GetSignalReference(type);
if (lookup.TryGetValue(signalReference, out MeasurementKey key))
keyArray[0] = key;
}
return keyArray[0]?.Metadata;
}
/// <summary>
/// Get <see cref="MeasurementMetadata"/> for specified <see cref="SignalKind"/>.
/// </summary>
/// <param name="lookup">A lookup table by signal reference.</param>
/// <param name="type"><see cref="SignalKind"/> to request signal reference for.</param>
/// <param name="index">Index <see cref="SignalKind"/> to request signal reference for.</param>
/// <param name="count">Number of signals defined for this <see cref="SignalKind"/>.</param>
/// <returns>The MeasurementMetadata for a given <see cref="SignalKind"/>. Null if it does not exist.</returns>
public MeasurementMetadata GetMetadata(Dictionary<string, MeasurementKey> lookup, SignalKind type, int index, int count)
{
// Clear the cache if the lookup dictionary has changed.
// Since the instance of Lookup is effectively readonly as implemented in PhasorMeasurementMapper
// a simple reference check is all that is needed. If it could be modified, this would likely be a
// concurrent dictionary instead.
if (m_keyLookupObject != lookup)
{
Array.Clear(m_generatedMeasurementKeyCache, 0, m_generatedMeasurementKeyCache.Length);
m_keyLookupObject = lookup;
}
// Gets the cache for the supplied SignalKind
int typeIndex = (int)type;
MeasurementKey[] keyArray = m_generatedMeasurementKeyCache[typeIndex];
// If this SignalKind is null, create the sub array and generate all item lookups, also, rebuild
// if the count is not the same. This could be because a new config frame was received.
if (keyArray is null || keyArray.Length != count)
{
keyArray = new MeasurementKey[count];
m_generatedMeasurementKeyCache[typeIndex] = keyArray;
for (int x = 0; x < count; x++)
{
string signalReference = GetSignalReference(type, x, count);
if (lookup.TryGetValue(signalReference, out MeasurementKey key))
keyArray[x] = key;
else
keyArray[x] = MeasurementKey.Undefined;
}
}
return keyArray[index]?.Metadata;
}
/// <summary>
/// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
/// <param name="context">The destination <see cref="StreamingContext"/> for this serialization.</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
// Serialize configuration cell
info.AddValue("lastReportTime", (long)m_lastReportTime);
info.AddValue("analogDataFormat", m_analogDataFormat, typeof(DataFormat));
info.AddValue("frequencyDataFormat", m_frequencyDataFormat, typeof(DataFormat));
info.AddValue("phasorDataFormat", m_phasorDataFormat, typeof(DataFormat));
info.AddValue("phasorCoordinateFormat", m_phasorCoordinateFormat, typeof(CoordinateFormat));
}
#endregion
}
}