68 lines
2.5 KiB
C#
68 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DTS.Serialization.IRIGCH10.Packets
|
|
{
|
|
/// <summary>
|
|
/// this class helps encapsulate a secondary time format header, but it's really
|
|
/// only designed for one type of format currently
|
|
/// </summary>
|
|
public class SecondaryTimeFormatHeader : ISecondaryTimeFormatHeader
|
|
{
|
|
private byte[] _bytes = new byte[SECONDARY_TIME_HEADER_LENGTH];
|
|
public int NanoSeconds { get; }
|
|
public int Seconds { get; }
|
|
public ushort Reserved { get; }
|
|
public ushort CheckSum { get; }
|
|
|
|
public DateTime LocalTime
|
|
{
|
|
get => (new DateTime(1970, 1, 1)).AddSeconds(Seconds).AddTicks(NanoSeconds / 100).ToLocalTime();
|
|
}
|
|
public SecondaryTimeFormatHeader(byte[] input)
|
|
{
|
|
Array.Copy(input, 0, _bytes, 0, SECONDARY_TIME_HEADER_LENGTH);
|
|
NanoSeconds = BitConverter.ToInt32(_bytes, 0);
|
|
Seconds = BitConverter.ToInt32(_bytes, 4);
|
|
Reserved = BitConverter.ToUInt16(_bytes, 8);
|
|
CheckSum = BitConverter.ToUInt16(_bytes, 10);
|
|
byte[] bytesToCompute;
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
using (var bw = new BinaryWriter(ms))
|
|
{
|
|
bw.Write(BitConverter.GetBytes(NanoSeconds));
|
|
bw.Write(BitConverter.GetBytes(Seconds));
|
|
bw.Write(Reserved);
|
|
}
|
|
bytesToCompute = ms.ToArray();
|
|
}
|
|
var computedChecksum = Utils.Utils.GetCheckSum8(bytesToCompute);
|
|
if (computedChecksum != CheckSum)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine("Secondary time header CRC does not match expectations");
|
|
}
|
|
}
|
|
public static byte[] GetBytes(int nanoseconds, int seconds)
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
using (var bw = new BinaryWriter(ms))
|
|
{
|
|
bw.Write(BitConverter.GetBytes(nanoseconds));
|
|
bw.Write(BitConverter.GetBytes(seconds));
|
|
bw.Write((ushort)0);
|
|
|
|
bw.Write(Utils.Utils.GetCheckSum8(ms.ToArray()));
|
|
}
|
|
return ms.ToArray();
|
|
}
|
|
}
|
|
public const int SECONDARY_TIME_HEADER_LENGTH = 12;
|
|
}
|
|
}
|