generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationCell3.cs
645 lines (537 loc) · 27.5 KB
/
ConfigurationCell3.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//******************************************************************************************************
// ConfigurationCell3.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:
// ----------------------------------------------------------------------------------------------------
// 09/08/2011 - Andrew Krohne
// Generated original version of source code.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using Gemstone.ArrayExtensions;
using Gemstone.GuidExtensions;
using Gemstone.Numeric.EE;
// ReSharper disable VirtualMemberCallInConstructor
namespace Gemstone.PhasorProtocols.IEEEC37_118
{
/// <summary>
/// Represents the IEEE C37.118 implementation of a <see cref="IConfigurationCell"/> that can be sent or received.
/// </summary>
[Serializable]
public sealed class ConfigurationCell3 : ConfigurationCellBase
{
#region [ Members ]
// Constants
/// <summary>
/// Represents <see cref="decimal"/> infinity value for longitude and latitude coordinate values.
/// </summary>
public const decimal CoordinateDecimalInfinity = 999M;
// Fields
private FormatFlags m_formatFlags;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new <see cref="ConfigurationCell3"/>.
/// </summary>
/// <param name="parent">The reference to parent <see cref="IConfigurationFrame"/> of this <see cref="ConfigurationCell3"/>.</param>
public ConfigurationCell3(IConfigurationFrame parent) : base(parent, 0, Common.MaximumPhasorValues, Common.MaximumAnalogValues, Common.MaximumDigitalValues, false)
{
// Define new parsing state which defines constructors for key configuration values
State = new ConfigurationCellParsingState(
PhasorDefinition3.CreateNewDefinition,
IEEEC37_118.FrequencyDefinition.CreateNewDefinition,
AnalogDefinition3.CreateNewDefinition,
DigitalDefinition3.CreateNewDefinition);
}
/// <summary>
/// Creates a new <see cref="ConfigurationCell3"/> from specified parameters.
/// </summary>
/// <param name="parent">The reference to parent <see cref="ConfigurationFrame1"/> of this <see cref="ConfigurationCell3"/>.</param>
/// <param name="idCode">The numeric ID code for this <see cref="ConfigurationCell3"/>.</param>
/// <param name="nominalFrequency">The nominal <see cref="LineFrequency"/> of the <see cref="FrequencyDefinition"/> of this <see cref="ConfigurationCell3"/>.</param>
public ConfigurationCell3(ConfigurationFrame1 parent, ushort idCode, LineFrequency nominalFrequency) //FIXME: Does this need to use config frame 3?
: this(parent)
{
IDCode = idCode;
NominalFrequency = nominalFrequency;
}
/// <summary>
/// Creates a new <see cref="ConfigurationCell3"/> 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 ConfigurationCell3(SerializationInfo info, StreamingContext context)
: base(info, context)
{
float getSingle(string name)
{
return info.GetValue(name, typeof(string)) is not string element || element == "INF" ?
float.PositiveInfinity :
float.Parse(element, CultureInfo.InvariantCulture);
}
// Deserialize configuration cell
m_formatFlags = (FormatFlags)info.GetValue("formatFlags", typeof(FormatFlags));
GlobalID = Guid.TryParse(info.GetOrDefault("globalID", Guid.Empty.ToString()), out Guid globalID) ? globalID : Guid.Empty;
// Decode PMU_LAT, PMU_LON, PMU_ELEV, SVC_CLASS, WINDOW, GRP_DLY values
Latitude = getSingle("latitude");
Longitude = getSingle("longitude");
Elevation = getSingle("elevation");
ServiceClass = info.GetChar("serviceClass");
Window = info.GetInt32("window");
GroupDelay = info.GetInt32("groupDelay");
DataModified = info.GetBoolean("dataModified");
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets a reference to the parent <see cref="ConfigurationFrame1"/> for this <see cref="ConfigurationCell3"/>.
/// </summary>
public new ConfigurationFrame1 Parent
{
get => base.Parent as ConfigurationFrame1;
set => base.Parent = value;
}
/// <summary>
/// Gets or sets format flags of this <see cref="ConfigurationCell"/>.
/// </summary>
/// <remarks>
/// These are bit flags, use properties to change basic values.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public FormatFlags FormatFlags
{
get => m_formatFlags;
set => m_formatFlags = 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_formatFlags & FormatFlags.Phasors) > 0 ? DataFormat.FloatingPoint : DataFormat.FixedInteger;
set
{
if (value == DataFormat.FloatingPoint)
m_formatFlags |= FormatFlags.Phasors;
else
m_formatFlags &= ~FormatFlags.Phasors;
}
}
/// <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_formatFlags & FormatFlags.Coordinates) > 0 ? CoordinateFormat.Polar : CoordinateFormat.Rectangular;
set
{
if (value == CoordinateFormat.Polar)
m_formatFlags |= FormatFlags.Coordinates;
else
m_formatFlags &= ~FormatFlags.Coordinates;
}
}
/// <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_formatFlags & FormatFlags.Frequency) > 0 ? DataFormat.FloatingPoint : DataFormat.FixedInteger;
set
{
if (value == DataFormat.FloatingPoint)
m_formatFlags |= FormatFlags.Frequency;
else
m_formatFlags &= ~FormatFlags.Frequency;
}
}
/// <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_formatFlags & FormatFlags.Analog) > 0 ? DataFormat.FloatingPoint : DataFormat.FixedInteger;
set
{
if (value == DataFormat.FloatingPoint)
m_formatFlags |= FormatFlags.Analog;
else
m_formatFlags &= ~FormatFlags.Analog;
}
}
/// <summary>
/// Gets or sets G_PMU_ID value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public Guid GlobalID { get; set; } = Guid.Empty;
/// <summary>
/// Gets or sets PMU_LAT value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public float Latitude { get; set; } = float.PositiveInfinity;
/// <summary>
/// Gets <see cref="Latitude"/> as a <see cref="decimal"/>, infinity represented by <see cref="CoordinateDecimalInfinity"/>.
/// </summary>
public decimal LatitudeM => GetDecimalCoordinate(Latitude);
/// <summary>
/// Gets or sets PMU_LON value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public float Longitude { get; set; } = float.PositiveInfinity;
/// <summary>
/// Gets <see cref="Longitude"/> as a <see cref="decimal"/>, infinity represented by <see cref="CoordinateDecimalInfinity"/>.
/// </summary>
public decimal LongitudeM => GetDecimalCoordinate(Longitude);
/// <summary>
/// Gets or sets PMU_ELEV value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public float Elevation { get; set; } = float.PositiveInfinity;
/// <summary>
/// Gets or sets SVC_CLASS value, 'M' or 'P', for this <see cref="ConfigurationCell3"/>.
/// </summary>
public char ServiceClass { get; set; } = 'M';
/// <summary>
/// Gets or sets WINDOW value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public int Window { get; set; }
/// <summary>
/// Gets or sets GRP_DLY value for this <see cref="ConfigurationCell3"/>.
/// </summary>
public int GroupDelay { get; set; }
/// <summary>
/// Gets or sets flag indicating if data in cell is modified through configuration for this <see cref="ConfigurationCell3"/>.
/// </summary>
public bool DataModified { get; set; }
/// <summary>
/// Gets the maximum length of the <see cref="ConfigurationCellBase.StationName"/> of this <see cref="ConfigurationCell3"/>.
/// </summary>
public override int MaximumStationNameLength => byte.MaxValue;
/// <summary>
/// Gets flag that indicates if <see cref="ConfigurationCellBase.StationNameImage"/> should auto pad-right value to <see cref="MaximumStationNameLength"/>.
/// </summary>
public override bool AutoPadStationNameImage => false;
/// <summary>
/// Gets the length of the <see cref="HeaderImage"/>.
/// </summary>
protected override int HeaderLength => GetEncodedStringLength(StationName) + 26;
/// <summary>
/// Gets the binary header image of the <see cref="ConfigurationCell3"/> object.
/// </summary>
protected override byte[] HeaderImage
{
get
{
byte[] buffer = new byte[HeaderLength];
int index = 0;
index += EncodeLengthPrefixedString(StationName, buffer, index);
index += BigEndian.CopyBytes(IDCode, buffer, index);
index += GlobalID.ToRfcBytes(buffer, index);
index += BigEndian.CopyBytes((ushort)m_formatFlags, buffer, index);
index += BigEndian.CopyBytes((ushort)PhasorDefinitions.Count, buffer, index);
index += BigEndian.CopyBytes((ushort)AnalogDefinitions.Count, buffer, index);
BigEndian.CopyBytes((ushort)DigitalDefinitions.Count, buffer, index);
return buffer;
}
}
/// <summary>
/// Gets the length of the <see cref="ConfigurationCellBase.BodyImage"/>.
/// </summary>
protected override int BodyLength
{
get
{
int length = 0;
// Configuration Frame 3 cells have variable length labels
foreach (IPhasorDefinition definition in PhasorDefinitions)
{
if (definition is PhasorDefinition3 phasorDefinition)
length += phasorDefinition.LabelImage.Length;
}
foreach (IAnalogDefinition definition in AnalogDefinitions)
{
if (definition is AnalogDefinition3 analogDefinition)
length += analogDefinition.LabelImage.Length;
}
foreach (IDigitalDefinition definition in DigitalDefinitions)
{
if (definition is DigitalDefinition3 digitalDefinition)
{
// Force a refresh of label image before getting image length
digitalDefinition.Label = digitalDefinition.Label;
length += digitalDefinition.LabelImage.Length;
}
}
return length;
}
}
/// <summary>
/// Gets the length of the <see cref="FooterImage"/>.
/// </summary>
protected override int FooterLength =>
PhasorDefinitions.Count * PhasorDefinition3.ConversionFactorLength +
AnalogDefinitions.Count * AnalogDefinition3.ConversionFactorLength +
DigitalDefinitions.Count * DigitalDefinition3.ConversionFactorLength +
21 + base.FooterLength + 2;
/// <summary>
/// Gets the binary footer image of the <see cref="ConfigurationCell3"/> object.
/// </summary>
protected override byte[] FooterImage
{
get
{
byte[] buffer = new byte[FooterLength];
int index = 0;
// Include conversion factors in configuration cell footer
foreach (IPhasorDefinition definition in PhasorDefinitions)
{
if (definition is PhasorDefinition3 phasorDefinition)
phasorDefinition.ConversionFactorImage.CopyImage(buffer, ref index, PhasorDefinition3.ConversionFactorLength);
}
foreach (IAnalogDefinition definition in AnalogDefinitions)
{
if (definition is AnalogDefinition3 analogDefinition)
analogDefinition.ConversionFactorImage.CopyImage(buffer, ref index, AnalogDefinition3.ConversionFactorLength);
}
foreach (IDigitalDefinition definition in DigitalDefinitions)
{
if (definition is DigitalDefinition3 digitalDefinition)
digitalDefinition.ConversionFactorImage.CopyImage(buffer, ref index, DigitalDefinition3.ConversionFactorLength);
}
// Add PMU_LAT, PMU_LON, PMU_ELEV, SVC_CLASS, WINDOW, GRP_DLY values
index += BigEndian.CopyBytes(Latitude, buffer, index);
index += BigEndian.CopyBytes(Longitude, buffer, index);
index += BigEndian.CopyBytes(Elevation, buffer, index);
buffer[index++] = (byte)ServiceClass;
index += BigEndian.CopyBytes(Window, buffer, index);
index += BigEndian.CopyBytes(GroupDelay, buffer, index);
// Include nominal frequency
base.FooterImage.CopyImage(buffer, ref index, base.FooterLength);
BigEndian.CopyBytes(RevisionCount, buffer, index);
return buffer;
}
}
/// <summary>
/// <see cref="Dictionary{TKey,TValue}"/> of string based property names and values for the <see cref="ConfigurationCell3"/> object.
/// </summary>
public override Dictionary<string, string> Attributes
{
get
{
Dictionary<string, string> baseAttributes = base.Attributes;
baseAttributes.Add("Format Flags", $"{(int)m_formatFlags}: {m_formatFlags}");
baseAttributes.Add("Global ID", $"{GlobalID}");
baseAttributes.Add("Latitude", float.IsInfinity(Latitude) ? "INF" : Latitude.ToString(CultureInfo.InvariantCulture));
baseAttributes.Add("Longitude", float.IsInfinity(Longitude) ? "INF" : Longitude.ToString(CultureInfo.InvariantCulture));
baseAttributes.Add("Elevation", float.IsInfinity(Elevation) ? "INF" : Elevation.ToString(CultureInfo.InvariantCulture));
baseAttributes.Add("Service Class", $"{ServiceClass}");
baseAttributes.Add("Window", $"{Window}");
baseAttributes.Add("Group Delay", $"{GroupDelay}");
baseAttributes.Add("Data Modified", $"{DataModified}");
return baseAttributes;
}
}
#endregion
#region [ Methods ]
/// <summary>
/// Parses the binary header 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 ParseHeaderImage(byte[] buffer, int startIndex, int length)
{
IConfigurationCellParsingState state = State;
int index = startIndex;
// Parse out station name
StationName = DecodeLengthPrefixedString(buffer, ref index);
IDCode = BigEndian.ToUInt16(buffer, index);
index += 2;
GlobalID = buffer.ToRfcGuid(index);
index += 16;
m_formatFlags = (FormatFlags)BigEndian.ToUInt16(buffer, index);
index += 2;
// Parse out total phasors, analogs and digitals defined for this device
state.PhasorCount = BigEndian.ToUInt16(buffer, index);
index += 2;
state.AnalogCount = BigEndian.ToUInt16(buffer, index);
index += 2;
state.DigitalCount = BigEndian.ToUInt16(buffer, index);
index += 2;
return index - startIndex;
}
/// <summary>
/// Parses the binary footer 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 ParseFooterImage(byte[] buffer, int startIndex, int length)
{
int index = startIndex;
// Parse conversion factors from configuration cell footer
foreach (IPhasorDefinition definition in PhasorDefinitions)
{
if (definition is PhasorDefinition3 phasorDefinition)
index += phasorDefinition.ParseConversionFactor(buffer, index);
}
foreach (IAnalogDefinition definition in AnalogDefinitions)
{
if (definition is AnalogDefinition3 analogDefinition)
index += analogDefinition.ParseConversionFactor(buffer, index);
}
foreach (IDigitalDefinition definition in DigitalDefinitions)
{
if (definition is DigitalDefinition3 digitalDefinition)
index += digitalDefinition.ParseConversionFactor(buffer, index);
}
// Parse PMU_LAT, PMU_LON, PMU_ELEV, SVC_CLASS, WINDOW, GRP_DLY values
Latitude = BigEndian.ToSingle(buffer, index);
index += 4;
Longitude = BigEndian.ToSingle(buffer, index);
index += 4;
Elevation = BigEndian.ToSingle(buffer, index);
index += 4;
ServiceClass = (char)buffer[index++];
Window = BigEndian.ToInt32(buffer, index);
index += 4;
GroupDelay = BigEndian.ToInt32(buffer, index);
index += 4;
// Parse nominal frequency
index += base.ParseFooterImage(buffer, index, length);
RevisionCount = BigEndian.ToUInt16(buffer, index);
index += 2;
return index - startIndex;
}
// GSF time-series databases define lat/long fields as decimal(9,6) which
// results in a max value of 999.999999, so we just use 999 as the max to
// represent decimal infinity in the databases. Value 999 is far clear of
// latitudes spanning -90 to 90 and longitudes spanning -180 to 180.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private decimal GetDecimalCoordinate(float coordinate) =>
float.IsInfinity(coordinate) || float.IsNaN(coordinate) ? CoordinateDecimalInfinity : (decimal)coordinate;
/// <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("formatFlags", m_formatFlags, typeof(FormatFlags));
info.AddValue("globalID", GlobalID.ToString());
// Encode PMU_LAT, PMU_LON, PMU_ELEV, SVC_CLASS, WINDOW, GRP_DLY values
info.AddValue("latitude", Latitude);
info.AddValue("longitude", Longitude);
info.AddValue("elevation", Elevation);
info.AddValue("serviceClass", ServiceClass);
info.AddValue("window", Window);
info.AddValue("groupDelay", GroupDelay);
info.AddValue("dataModified", DataModified);
}
#endregion
#region [ Static ]
// Static Methods
// Delegate handler to create a new IEEE C37.118 configuration cell
internal static IConfigurationCell CreateNewCell(IChannelFrame parent, IChannelFrameParsingState<IConfigurationCell> state, int index, byte[] buffer, int startIndex, out int parsedLength)
{
ConfigurationCell3 configCell = new(parent as IConfigurationFrame);
parsedLength = configCell.ParseBinaryImage(buffer, startIndex, 0);
return configCell;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetEncodedStringLength(string value)
{
if (value == null)
return 1;
return Encoding.UTF8.GetByteCount(value) + 1;
}
// Encode a string into a UTF-8 byte array, validating length.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte[] EncodeUTF8String(string value)
{
if (value == null)
value = "";
if (value.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException(nameof(value), $"Cannot encode a length prefixed string for IEEE C37.118-2011 that is longer than {byte.MaxValue} bytes. Source string length before encoding is already {value.Length:N0} characters.");
byte[] bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length > byte.MaxValue)
throw new ArgumentOutOfRangeException(nameof(value), $"Cannot encode a length prefixed string for IEEE C37.118-2011 that is longer than {byte.MaxValue} bytes. UTF-8 encoded string length is {bytes.Length:N0} bytes.");
return bytes;
}
/// <summary>
/// Encodes a length-prefixed string. Length size is one byte per IEEE C37.118-2011 specification.
/// </summary>
/// <param name="value">String value to encode using UTF-8 encoding..</param>
/// <param name="buffer">Target buffer.</param>
/// <param name="offset">Target buffer offset.</param>
/// <returns>Total number of encoded bytes copied into <paramref name="buffer"/>.</returns>
public static int EncodeLengthPrefixedString(string value, byte[] buffer, int offset)
{
byte[] bytes = EncodeUTF8String(value);
int totalLength = bytes.Length + 1;
buffer.ValidateParameters(offset, totalLength);
buffer[offset] = (byte)bytes.Length;
Buffer.BlockCopy(bytes, 0, buffer, offset + 1, bytes.Length);
return totalLength;
}
/// <summary>
/// Encodes a length-prefixed string. Length size is one byte per IEEE C37.118-2011 specification.
/// </summary>
/// <param name="value">String value to encode using UTF-8 encoding..</param>
/// <returns>Encoded string.</returns>
public static byte[] EncodeLengthPrefixedString(string value)
{
byte[] bytes = EncodeUTF8String(value);
byte[] buffer = new byte[bytes.Length + 1];
buffer[0] = (byte)bytes.Length;
Buffer.BlockCopy(bytes, 0, buffer, 1, bytes.Length);
return buffer;
}
/// <summary>
/// Decodes a length-prefixed string. Length size is one byte per IEEE C37.118-2011 specification.
/// </summary>
/// <param name="buffer">Source buffer.</param>
/// <param name="offset">Source buffer offset. Value is auto-incremented.</param>
/// <returns>Decoded string from bytes encoded in <paramref name="buffer"/>.</returns>
public static string DecodeLengthPrefixedString(byte[] buffer, ref int offset)
{
if (buffer.Length - 1 < offset)
throw new ArgumentOutOfRangeException(nameof(offset), $"offset of {offset} and length of 1 will exceed buffer size of {buffer.Length:N0}");
int length = buffer[offset++];
buffer.ValidateParameters(offset, length);
string result = Encoding.UTF8.GetString(buffer, offset, length);
offset += length;
return result;
}
/// <summary>
/// Gets longitude or latitude coordinate stored as a <see cref="decimal"/> as a <see cref="float"/>
/// accounting for possible <see cref="CoordinateDecimalInfinity"/> value.
/// </summary>
/// <param name="coordinate">Source value.</param>
/// <returns><paramref name="coordinate"/> as <see cref="float"/>.</returns>
public static float GetCoordinateFromDecimal(decimal? coordinate) =>
coordinate is null || coordinate >= CoordinateDecimalInfinity ? float.PositiveInfinity : (float)coordinate;
#endregion
}
}