From 1addb3c4e1c5a7baf1e082e890154a399e033098 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Tue, 8 Feb 2022 18:23:55 +1300 Subject: [PATCH] Add project files. --- Starlink Desktop Monitor.sln | 25 +++ Starlink Desktop Monitor/App.xaml | 9 + Starlink Desktop Monitor/App.xaml.cs | 17 ++ Starlink Desktop Monitor/AssemblyInfo.cs | 10 ++ Starlink Desktop Monitor/MainWindow.xaml | 70 ++++++++ Starlink Desktop Monitor/MainWindow.xaml.cs | 167 ++++++++++++++++++ .../Starlink Desktop Monitor.csproj | 11 ++ 7 files changed, 309 insertions(+) create mode 100644 Starlink Desktop Monitor.sln create mode 100644 Starlink Desktop Monitor/App.xaml create mode 100644 Starlink Desktop Monitor/App.xaml.cs create mode 100644 Starlink Desktop Monitor/AssemblyInfo.cs create mode 100644 Starlink Desktop Monitor/MainWindow.xaml create mode 100644 Starlink Desktop Monitor/MainWindow.xaml.cs create mode 100644 Starlink Desktop Monitor/Starlink Desktop Monitor.csproj diff --git a/Starlink Desktop Monitor.sln b/Starlink Desktop Monitor.sln new file mode 100644 index 0000000..f12e63d --- /dev/null +++ b/Starlink Desktop Monitor.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32127.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Starlink Desktop Monitor", "Starlink Desktop Monitor\Starlink Desktop Monitor.csproj", "{E90822C7-1FC9-4FB7-89E2-D60CD9AC8D8A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E90822C7-1FC9-4FB7-89E2-D60CD9AC8D8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E90822C7-1FC9-4FB7-89E2-D60CD9AC8D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E90822C7-1FC9-4FB7-89E2-D60CD9AC8D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E90822C7-1FC9-4FB7-89E2-D60CD9AC8D8A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AD4A0B2A-DDC8-4EAA-80ED-680B115D5FD4} + EndGlobalSection +EndGlobal diff --git a/Starlink Desktop Monitor/App.xaml b/Starlink Desktop Monitor/App.xaml new file mode 100644 index 0000000..bb78a05 --- /dev/null +++ b/Starlink Desktop Monitor/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/Starlink Desktop Monitor/App.xaml.cs b/Starlink Desktop Monitor/App.xaml.cs new file mode 100644 index 0000000..2a580a6 --- /dev/null +++ b/Starlink Desktop Monitor/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace Starlink_Desktop_Monitor +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/Starlink Desktop Monitor/AssemblyInfo.cs b/Starlink Desktop Monitor/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/Starlink Desktop Monitor/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/Starlink Desktop Monitor/MainWindow.xaml b/Starlink Desktop Monitor/MainWindow.xaml new file mode 100644 index 0000000..18c68f2 --- /dev/null +++ b/Starlink Desktop Monitor/MainWindow.xaml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Starlink Desktop Monitor/MainWindow.xaml.cs b/Starlink Desktop Monitor/MainWindow.xaml.cs new file mode 100644 index 0000000..aaa9006 --- /dev/null +++ b/Starlink Desktop Monitor/MainWindow.xaml.cs @@ -0,0 +1,167 @@ +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 +{ + /// + /// Interaction logic for MainWindow.xaml + /// + 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; + /// + /// List of known data tags + /// + static readonly List 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 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; + } + } +} diff --git a/Starlink Desktop Monitor/Starlink Desktop Monitor.csproj b/Starlink Desktop Monitor/Starlink Desktop Monitor.csproj new file mode 100644 index 0000000..7eaba2e --- /dev/null +++ b/Starlink Desktop Monitor/Starlink Desktop Monitor.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net5.0-windows + Starlink_Desktop_Monitor + enable + true + + +