generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataCell.cs
264 lines (228 loc) · 10.4 KB
/
DataCell.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
//******************************************************************************************************
// DataCell.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:
// ----------------------------------------------------------------------------------------------------
// 04/27/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
// ReSharper disable VirtualMemberCallInConstructor
namespace Gemstone.PhasorProtocols.SelFastMessage
{
/// <summary>
/// Represents the SEL Fast Message implementation of a <see cref="IDataCell"/> that can be sent or received.
/// </summary>
[Serializable]
public class DataCell : DataCellBase
{
#region [ Constructors ]
/// <summary>
/// Creates a new <see cref="DataCell"/>.
/// </summary>
/// <param name="parent">The reference to parent <see cref="IDataFrame"/> of this <see cref="DataCell"/>.</param>
/// <param name="configurationCell">The <see cref="IConfigurationCell"/> associated with this <see cref="DataCell"/>.</param>
public DataCell(IDataFrame parent, IConfigurationCell configurationCell)
: base(parent, configurationCell, (ushort)(StatusFlags.TSOK | StatusFlags.PMDOK), Common.MaximumPhasorValues, Common.MaximumAnalogValues, Common.MaximumDigitalValues)
{
}
/// <summary>
/// Creates a new <see cref="DataCell"/> from specified parameters.
/// </summary>
/// <param name="parent">The reference to parent <see cref="DataFrame"/> of this <see cref="DataCell"/>.</param>
/// <param name="configurationCell">The <see cref="ConfigurationCell"/> associated with this <see cref="DataCell"/>.</param>
/// <param name="addEmptyValues">If <c>true</c>, adds empty values for each defined configuration cell definition.</param>
public DataCell(DataFrame parent, ConfigurationCell configurationCell, bool addEmptyValues)
: this(parent, configurationCell)
{
if (!addEmptyValues)
return;
// Define needed phasor values
foreach (IPhasorDefinition phasorDefinition in configurationCell.PhasorDefinitions)
PhasorValues.Add(new PhasorValue(this, phasorDefinition));
// Define a frequency and df/dt
FrequencyValue = new FrequencyValue(this, configurationCell.FrequencyDefinition);
}
/// <summary>
/// Creates a new <see cref="DataCell"/> 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 DataCell(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets the reference to parent <see cref="DataFrame"/> of this <see cref="DataCell"/>.
/// </summary>
public new DataFrame Parent
{
get => base.Parent as DataFrame;
set => base.Parent = value;
}
/// <summary>
/// Gets or sets the <see cref="ConfigurationCell"/> associated with this <see cref="DataCell"/>.
/// </summary>
public new ConfigurationCell ConfigurationCell
{
get => base.ConfigurationCell as ConfigurationCell;
set => base.ConfigurationCell = value;
}
/// <summary>
/// Gets the numeric ID code for this <see cref="DataCell"/>.
/// </summary>
public new uint IDCode => ConfigurationCell.IDCode;
/// <summary>
/// Gets or sets status flags for this <see cref="DataCell"/>.
/// </summary>
public new StatusFlags StatusFlags
{
get => (StatusFlags)base.StatusFlags;
set => base.StatusFlags = (ushort)value;
}
/// <summary>
/// Gets or sets flag that determines if data of this <see cref="DataCell"/> is valid.
/// </summary>
public override bool DataIsValid
{
get => (StatusFlags & StatusFlags.PMDOK) > 0;
set
{
if (value)
StatusFlags |= StatusFlags.PMDOK;
else
StatusFlags &= ~StatusFlags.PMDOK;
}
}
/// <summary>
/// Gets or sets flag that determines if timestamp of this <see cref="DataCell"/> is valid based on GPS lock.
/// </summary>
public override bool SynchronizationIsValid
{
get => (StatusFlags & StatusFlags.TSOK) > 0;
set
{
if (value)
StatusFlags |= StatusFlags.TSOK;
else
StatusFlags &= ~StatusFlags.TSOK;
}
}
/// <summary>
/// Gets or sets <see cref="PhasorProtocols.DataSortingType"/> of this <see cref="DataCell"/>.
/// </summary>
public override DataSortingType DataSortingType
{
get => SynchronizationIsValid ? DataSortingType.ByTimestamp : DataSortingType.ByArrival;
set
{
// We just ignore this value as we have defined data sorting type as a derived value based on synchronization validity
}
}
/// <summary>
/// Gets or sets flag that determines if source device of this <see cref="DataCell"/> is reporting an error.
/// </summary>
/// <remarks>SEL Fast Message doesn't define bits for device error.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool DeviceError
{
get => false;
set
{
// We just ignore this value as SEL Fast Message defines no flags for data errors
}
}
/// <summary>
/// Gets <see cref="AnalogValueCollection"/> of this <see cref="DataCell"/>.
/// </summary>
/// <remarks>
/// SEL Fast Message doesn't define any analog values.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public override AnalogValueCollection AnalogValues => base.AnalogValues;
/// <summary>
/// Gets <see cref="DigitalValueCollection"/> of this <see cref="DataCell"/>.
/// </summary>
/// <remarks>
/// SEL Fast Message doesn't define any digital values.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public override DigitalValueCollection DigitalValues => base.DigitalValues;
/// <summary>
/// <see cref="Dictionary{TKey,TValue}"/> of string based property names and values for the <see cref="DataCell"/> object.
/// </summary>
public override Dictionary<string, string> Attributes
{
get
{
Dictionary<string, string> baseAttributes = base.Attributes;
baseAttributes.Add("SEL Status Flags", $"{(int)StatusFlags}: {StatusFlags}");
return baseAttributes;
}
}
#endregion
#region [ Methods ]
/// <summary>
/// Parses the binary body image.
/// </summary>
/// <param name="buffer">Binary image to parse.</param>
/// <param name="startIndex">Start index into <paramref name="buffer"/> to begin parsing.</param>
/// <param name="length">Length of valid data within <paramref name="buffer"/>.</param>
/// <returns>The length of the data that was parsed.</returns>
protected override int ParseBodyImage(byte[] buffer, int startIndex, int length)
{
ConfigurationCell configCell = ConfigurationCell;
IPhasorValue phasorValue;
int x, index = startIndex;
// Parse out frequency value
FrequencyValue = SelFastMessage.FrequencyValue.CreateNewValue(this, configCell.FrequencyDefinition, buffer, index, out int parsedLength);
index += parsedLength;
// Parse out phasor values
for (x = 0; x < configCell.PhasorDefinitions.Count; x++)
{
phasorValue = PhasorValue.CreateNewValue(this, configCell.PhasorDefinitions[x], buffer, index, out parsedLength);
PhasorValues.Add(phasorValue);
index += parsedLength;
}
// Parse out status flags
StatusFlags = (StatusFlags)BigEndian.ToUInt16(buffer, index);
index += 2;
// Return total parsed length
return index - startIndex;
}
#endregion
#region [ Static ]
// Static Methods
// Delegate handler to create a new SEL Fast Message data cell
internal static IDataCell CreateNewCell(IChannelFrame parent, IChannelFrameParsingState<IDataCell> state, int index, byte[] buffer, int startIndex, out int parsedLength)
{
DataCell dataCell = new(parent as IDataFrame, (state as IDataFrameParsingState).ConfigurationFrame.Cells[index]);
parsedLength = dataCell.ParseBinaryImage(buffer, startIndex, 0);
return dataCell;
}
#endregion
}
}