generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalDefinition3.cs
358 lines (302 loc) · 14.1 KB
/
DigitalDefinition3.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
//******************************************************************************************************
// DigitalDefinition3.cs - Gbtc
//
// Copyright © 2021, 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://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:
// ----------------------------------------------------------------------------------------------------
// 12/27/2020 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Gemstone.Numeric.BitExtensions;
using Gemstone.StringExtensions;
// ReSharper disable VirtualMemberCallInConstructor
namespace Gemstone.PhasorProtocols.IEEEC37_118
{
/// <summary>
/// Represents the IEEE C37.118 configuration frame 3 implementation of a <see cref="IDigitalDefinition"/>.
/// </summary>
[Serializable]
public sealed class DigitalDefinition3 : ChannelDefinitionBase3, IDigitalDefinition
{
#region [ Members ]
// Constants
internal const int ConversionFactorLength = 4;
private const int BitLabelCount = 16;
// Fields
private byte[] m_labelImage;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new <see cref="DigitalDefinition3"/>.
/// </summary>
/// <param name="parent">The <see cref="IConfigurationCell"/> parent of this <see cref="DigitalDefinition3"/>.</param>
public DigitalDefinition3(IConfigurationCell parent)
: base(parent)
{
ScalingValue = 1;
Offset = 0.0D;
}
/// <summary>
/// Creates a new <see cref="DigitalDefinition3"/> from specified parameters.
/// </summary>
/// <param name="parent">The <see cref="ConfigurationCell3"/> parent of this <see cref="DigitalDefinition3"/>.</param>
/// <param name="label">The label of this <see cref="DigitalDefinition3"/>.</param>
/// <param name="normalStatus">The normal status for this <see cref="DigitalDefinition3"/>.</param>
/// <param name="validInputs">The valid input for this <see cref="DigitalDefinition3"/>.</param>
public DigitalDefinition3(ConfigurationCell3 parent, string label, ushort normalStatus, ushort validInputs)
: base(parent, label, 1, 0.0D)
{
NormalStatus = normalStatus;
ValidInputs = validInputs;
}
/// <summary>
/// Creates a new <see cref="DigitalDefinition3"/> 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>
private DigitalDefinition3(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// Deserialize digital definition
NormalStatus = info.GetUInt16("normalStatus");
ValidInputs = info.GetUInt16("validInputs");
Label = info.GetString("digitalLabels");
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets the <see cref="ConfigurationCell3"/> parent of this <see cref="DigitalDefinition3"/>.
/// </summary>
public new ConfigurationCell3 Parent
{
get => base.Parent as ConfigurationCell3;
set => base.Parent = value;
}
/// <summary>
/// Gets or sets normal status for this <see cref="DigitalDefinition3"/>.
/// </summary>
public ushort NormalStatus { get; set; }
/// <summary>
/// Gets or sets valid input for this <see cref="DigitalDefinition3"/>.
/// </summary>
public ushort ValidInputs { get; set; }
/// <summary>
/// Gets the maximum length of the <see cref="Label"/> of this <see cref="DigitalDefinition3"/>.
/// </summary>
public override int MaximumLabelLength => LabelCount * byte.MaxValue;
/// <summary>
/// Gets the number of labels defined in this <see cref="DigitalDefinition3"/>.
/// </summary>
public int LabelCount => BitLabelCount;
/// <summary>
/// Gets array of 16-digital labels.
/// </summary>
public string[] Labels { get; } = new string[BitLabelCount];
/// <summary>
/// Gets or sets the combined set of label images of this <see cref="DigitalDefinition3"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string Label
{
// We hide this from the editor just because this is a large combined string of all digital labels,
// and it will make more sense for consumers to use the "Labels" property
get => string.Join("|", Labels.Select(label => label.RemoveCharacter('|').GetValidLabel()));
set
{
if (string.IsNullOrEmpty(value))
value = "undefined";
if (value.Trim().Length > MaximumLabelLength)
throw new OverflowException($"Label length cannot exceed {MaximumLabelLength}");
string[] labels = value.Split('|');
for (int i = 0; i < BitLabelCount; i++)
Labels[i] = i < labels.Length ? labels[i].Trim() : "";
using (MemoryStream stream = new())
{
for (int i = 0; i < BitLabelCount; i++)
{
byte[] buffer = ConfigurationCell3.EncodeLengthPrefixedString(Labels[i]);
stream.Write(buffer, 0, buffer.Length);
}
m_labelImage = stream.ToArray();
}
}
}
/// <summary>
/// Gets the binary image of the <see cref="Label"/> of this <see cref="DigitalDefinition3"/>.
/// </summary>
public override byte[] LabelImage => m_labelImage;
/// <summary>
/// Gets the length of the <see cref="DigitalDefinition3"/>.
/// </summary>
protected override int BodyLength
{
get
{
// Force a refresh of label image before getting BodyImage
Label = Label;
return LabelImage.Length;
}
}
/// <summary>
/// Gets the <see cref="IEEEC37_118.DraftRevision"/> of this <see cref="DigitalDefinition3"/>.
/// </summary>
public DraftRevision DraftRevision => DraftRevision.Std2011;
/// <summary>
/// Gets the <see cref="PhasorProtocols.DataFormat"/> of this <see cref="DigitalDefinitionBase"/>.
/// </summary>
/// <remarks>
/// Data format for digital values will always be <see cref="DataFormat.FixedInteger"/>.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public override DataFormat DataFormat => DataFormat.FixedInteger;
/// <summary>
/// Gets or sets the offset of this <see cref="DigitalDefinitionBase"/>.
/// </summary>
/// <remarks>
/// Offset for digital values will always be 0; assigning a value other than 0 will thrown an exception.
/// </remarks>
/// <exception cref="NotImplementedException">Digital values represent bit flags and thus do not support an offset.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public override double Offset
{
get => base.Offset;
set
{
if (value == 0)
base.Offset = value;
else
throw new NotImplementedException("Digital values represent bit flags and thus do not support an offset");
}
}
/// <summary>
/// Gets or sets the integer scaling value of this <see cref="DigitalDefinitionBase"/>.
/// </summary>
/// <remarks>
/// Scaling value for digital values will always be 1; assigning a value other than 1 will thrown an exception.
/// </remarks>
/// <exception cref="NotImplementedException">Digital values represent bit flags and thus are not scaled.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public override uint ScalingValue
{
get => base.ScalingValue;
set
{
if (value == 1)
base.ScalingValue = value;
else
throw new NotImplementedException("Digital values represent bit flags and thus are not scaled");
}
}
/// <summary>
/// Gets the scale/bit for the <see cref="ScalingValue"/> of this <see cref="DigitalDefinitionBase"/>.
/// </summary>
/// <remarks>
/// Scale/bit for digital values will always be 1.0.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public override double ScalePerBit => 1.0D;
/// <summary>
/// Gets conversion factor image of this <see cref="DigitalDefinition3"/>.
/// </summary>
internal byte[] ConversionFactorImage
{
get
{
byte[] buffer = new byte[ConversionFactorLength];
BigEndian.CopyBytes(NormalStatus, buffer, 0);
BigEndian.CopyBytes(ValidInputs, buffer, 2);
return buffer;
}
}
/// <summary>
/// Gets a <see cref="Dictionary{TKey,TValue}"/> of string based property names and values for this <see cref="DigitalDefinition3"/> object.
/// </summary>
public override Dictionary<string, string> Attributes
{
get
{
Dictionary<string, string> baseAttributes = base.Attributes;
baseAttributes.Add("Normal Status", NormalStatus.ToString());
baseAttributes.Add("Normal Status (Big Endian Bits)", NormalStatus.ToBinaryString());
baseAttributes.Add("Normal Status (Hexadecimal)", $"0x{NormalStatus:X2)}");
baseAttributes.Add("Valid Inputs", ValidInputs.ToString());
baseAttributes.Add("Valid Inputs (Big Endian Bits)", ValidInputs.ToBinaryString());
baseAttributes.Add("Valid Inputs (Hexadecimal)", $"0x{ValidInputs:X2)}");
baseAttributes.Add("Bit Label Count", LabelCount.ToString());
for (int x = 0; x < LabelCount; x++)
baseAttributes.Add($" Bit {x} Label", Labels[x]);
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)
{
int index = startIndex;
for (int i = 0; i < BitLabelCount; i++)
Labels[i] = ConfigurationCell3.DecodeLengthPrefixedString(buffer, ref index);
return index - startIndex;
}
/// <summary>
/// Parses conversion factor image from the specified <paramref name="buffer"/>.
/// </summary>
/// <param name="buffer">Binary image to parse.</param>
/// <param name="startIndex">Start index into <paramref name="buffer"/> to begin parsing.</param>
internal int ParseConversionFactor(byte[] buffer, int startIndex)
{
NormalStatus = BigEndian.ToUInt16(buffer, startIndex);
ValidInputs = BigEndian.ToUInt16(buffer, startIndex + 2);
return ConversionFactorLength;
}
/// <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 digital definition
info.AddValue("normalStatus", NormalStatus);
info.AddValue("validInputs", ValidInputs);
info.AddValue("digitalLabels", Label);
}
#endregion
#region [ Static ]
// Static Methods
// Delegate handler to create a new IEEE C37.118 digital definition
internal static IDigitalDefinition CreateNewDefinition(IConfigurationCell parent, byte[] buffer, int startIndex, out int parsedLength)
{
IDigitalDefinition digitalDefinition = new DigitalDefinition3(parent);
parsedLength = digitalDefinition.ParseBinaryImage(buffer, startIndex, 0);
return digitalDefinition;
}
#endregion
}
}