168 lines
7.6 KiB
C#
Raw Permalink Normal View History

2022-02-08 18:23:55 +13:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Starlink_Desktop_Monitor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Timer t;
public MainWindow()
{
InitializeComponent();
PacketReciever pr = new PacketReciever(this);
byte[] statsSequence = new byte[] { 0, 0, 0, 0, 3, 0xE2, 0x3E, 0 };
t = new(new((x) => pr.Update(statsSequence)), null, 2000, 500); // Fire at a regular interval
}
public void UpdateDisplay(float download, float upload)
{
// Low Limit = -20 deg, high limit = -160 deg.
double dlRate = download / 367001600; // Get % of full gauge
double ulRate = upload / 52428800;
// Clamp values
if (ulRate > 1.0) ulRate = 1.0;
if (dlRate > 1.0) dlRate = 1.0;
// Apply an e^logx function to exaggerate smaller results
dlRate = Math.Pow( Math.E, 0.66 * Math.Log(dlRate));
ulRate = Math.Pow( Math.E, 0.66 * Math.Log(ulRate));
// Full range is 220 deg. (-20 -> -160)
double realDLDeg = dlRate * 220.0;
double realULDeg = ulRate * 220.0;
// Calc the true rot
realDLDeg -= 20;
realULDeg -= 20;
// Finally, if the rotation is actually greater than 180, correctly set it
if (realDLDeg > 180.0) realDLDeg = -160 - (realDLDeg - 180);
if (realULDeg > 180.0) realULDeg = -160 - (realULDeg - 180);
RotateTransform ulRotateTransform = new(realULDeg);
RotateTransform dlRotateTransform = new(realDLDeg);
DoubleAnimation ulRotAnim = new DoubleAnimation(realULDeg, new Duration(TimeSpan.FromMilliseconds(1500)));
DoubleAnimation dlRotAnim = new DoubleAnimation(realDLDeg, new Duration(TimeSpan.FromMilliseconds(1500)));
ulRotAnim.AccelerationRatio = 0.01;
dlRotAnim.AccelerationRatio = 0.01;
ulRotAnim.DecelerationRatio = 0.01;
dlRotAnim.DecelerationRatio = 0.01;
NeedleDL.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, dlRotAnim);
NeedleUL.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, ulRotAnim);
//NeedleDL.RenderTransform = dlRotateTransform;
//NeedleUL.RenderTransform = ulRotateTransform;
DL_TextRate.Text = string.Format("{0:0.000} {1}B/s", download / (download < 8e6f ? (8.0f * 1024.0f) : (8.0f * 1024.0f * 1024.0f)), download < 8e6 ? "k" : "M");
UL_TextRate.Text = string.Format("{0:0.000} {1}B/s", upload / (upload < 8e6f ? (8.0f * 1024.0f) : (8.0f * 1024.0f * 1024.0f)), upload < 8e6 ? "k" : "M");
}
}
class PacketReciever
{
MainWindow main;
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(MainWindow main)
{
this.main = main;
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);*/
Application.Current.Dispatcher.Invoke(() => { main.UpdateDisplay(download, upload); });
}
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;
}
}
}