102 lines
4.6 KiB
C#
Raw Permalink Normal View History

2022-02-08 18:35:24 +13:00
using System.Net;
using System.Net.Http.Headers;
using System.Net.Sockets;
internal class Program
{
static void Main(string[] args)
{
PacketReciever pr = new PacketReciever();
// This sequence is used to poll either the router (192.168.1.1) or dish (192.168.100.1) for statistics.
byte[] statsSequence = new byte[] { 0, 0, 0, 0, 3, 0xE2, 0x3E, 0 };
Timer t = new(new((x) => pr.Update(statsSequence)), null, 2000, 5000); // Fire at a regular interval
Console.ReadLine();
}
class PacketReciever
{
readonly HttpClient client;
/// <summary>
/// List of known data tags
/// </summary>
static readonly List<byte[]> KnownTags = new()
{
new byte[] { 0xa5, 0x3f }, // BoresiteElevation
new byte[] { 0x9d, 0x3f }, // Boresite Azimuth
new byte[] { 0x8d, 0x3f }, // Ping
new byte[] { 0x85, 0x3f }, // Upload
new byte[] { 0xfd, 0x3e }, // Download
//new byte[] { 0xfd, 0x3f }, // Unknown
// ??? // Ping loss rate
};
public PacketReciever()
{
client = new();
client.DefaultRequestVersion = HttpVersion.Version11;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
}
public async void Update(byte[] requestContent)
{
try
{
// Create the request
ByteArrayContent content = new(requestContent);
// gRPC expects the content type to be a gRPC request, else will not respond with valid data
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/grpc-web+proto");
// Send the request
HttpResponseMessage r = await client.PostAsync("http://192.168.100.1:9201/SpaceX.API.Device.Device/Handle", content);
Console.Clear();
Dictionary<string, byte[]> tagValues = new();
byte[] data = await r.Content.ReadAsByteArrayAsync();
int dataIndex = LastIndexOf(data, new byte[] { 0x67, 0x72, 0x70, 0x63, 0x2D, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3A });
while (dataIndex > -1) // Search for `grpc-status:`; is the known end of data
{
// Valid data packet, parse;
foreach (var item in KnownTags)
{
if (data[dataIndex] == item[0] && data[dataIndex + 1] == item[1])
{
byte[] dataBytes = new byte[] { data[dataIndex+2], data[dataIndex+3], data[dataIndex+4], data[dataIndex+5] };
tagValues.Add(string.Format("{0:X}{1:X}", item[0], item[1]), dataBytes);
break;
}
}
dataIndex--;
}
float download = tagValues.ContainsKey("FD3E") ? BitConverter.ToSingle(tagValues["FD3E"]) : 0.00f;
float upload = tagValues.ContainsKey("853F") ? BitConverter.ToSingle(tagValues["853F"]) : 0.00f;
Console.WriteLine(" DL: \t{0} b/s ({1:0.000} {2}B/s)", download, download / (download < 8e6f ? (8.0f * 1024.0f) : (8.0f * 1024.0f * 1024.0f)), download < 8e6 ? "k" : "M");
Console.WriteLine(" UL: \t{0} b/s ({1:0.000} {2}B/s)", upload, upload / (upload < 8e6f ? (8.0f * 1024.0f) : (8.0f *1024.0f * 1024.0f)), upload < 8e6 ? "k" : "M");
Console.WriteLine(" Ping: \t{0} ms", tagValues.ContainsKey("8D3F") ? BitConverter.ToSingle(tagValues["8D3F"]) : 0.00);
Console.WriteLine(" Boresite Azimuth: \t{0}°", tagValues.ContainsKey("9D3F") ? BitConverter.ToSingle(tagValues["9D3F"]) : 0.00);
Console.WriteLine("Boresite Elevation: \t{0}°", tagValues.ContainsKey("A53F") ? BitConverter.ToSingle(tagValues["A53F"]) : 0.00);
}
catch { }
}
public static int LastIndexOf(byte[] src, byte[] searchBytes)
{
if (src.Length <= searchBytes.Length) return -1;
int pos = src.Length - searchBytes.Length;
while (pos > -1 && !exactMatch(src, searchBytes, pos))
{
pos--;
}
return pos;
}
static bool exactMatch(byte[] src, byte[] search, int pos)
{
for (int i = 0; i < search.Length; i++)
{
if (src[pos+i] != search[i])
{
return false;
}
}
return true;
}
}
}