2022-02-17 19:47:22 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace EFBTracker.Tracking
|
|
|
|
{
|
|
|
|
public class Packet
|
|
|
|
{
|
2022-02-17 20:07:53 +00:00
|
|
|
public string Type { get; set; }
|
2022-02-18 10:31:14 +00:00
|
|
|
public int Id { get; set; }
|
|
|
|
public float[] Data { get; set; } = new float[4];
|
2022-02-17 19:47:22 +00:00
|
|
|
|
2022-02-18 10:31:14 +00:00
|
|
|
public Packet(string type, int id, float[] data){
|
2022-02-17 19:47:22 +00:00
|
|
|
Type = type;
|
2022-02-18 10:31:14 +00:00
|
|
|
Id = id;
|
2022-02-17 19:47:22 +00:00
|
|
|
Data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Packet[]? ReadPackets(byte[] data){
|
|
|
|
|
|
|
|
|
|
|
|
if((data.Length - 5) % 36 == 0){//If there are correctly sized packets
|
|
|
|
int packetPos = 0;
|
|
|
|
List<Packet> packets = new List<Packet>();
|
|
|
|
|
|
|
|
//Next 4 bytes are packet header
|
|
|
|
string header = Encoding.UTF8.GetString(data, packetPos, 4);
|
|
|
|
packetPos += 5;
|
|
|
|
|
|
|
|
while(packetPos < data.Length)
|
|
|
|
{
|
|
|
|
|
|
|
|
int id = BitConverter.ToInt32(data, packetPos);
|
|
|
|
packetPos += 4;
|
|
|
|
|
|
|
|
//Array to take 4 values
|
|
|
|
float[] values = new float[4];
|
|
|
|
|
|
|
|
for (var i = 0; i < values.Length; i++)
|
|
|
|
{//Read 4 values from packet and save into array
|
|
|
|
var value = BitConverter.ToSingle(data, packetPos);
|
|
|
|
values[i] = value;
|
|
|
|
packetPos += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate new packet object and add to list
|
2022-02-18 10:31:14 +00:00
|
|
|
packets.Add(new Packet(header, id, values));
|
2022-02-17 19:47:22 +00:00
|
|
|
}
|
|
|
|
return packets.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|