|
7 | 7 | using System.Net.NetworkInformation; |
8 | 8 | using System.Net.Sockets; |
9 | 9 | using System.Text; |
| 10 | +using System.Text.Json; |
10 | 11 | using System.Threading; |
11 | 12 | using System.Timers; |
12 | 13 |
|
@@ -135,6 +136,65 @@ public static string GetPublicIPAddress() |
135 | 136 | else return "Not online"; |
136 | 137 | } |
137 | 138 |
|
| 139 | + /// <summary> |
| 140 | + /// Runs Ookla Speedtest, installs Speedtest CLI tool if it is not present |
| 141 | + /// </summary> |
| 142 | + /// <returns></returns> |
| 143 | + public static int RunSpeedTest() |
| 144 | + { |
| 145 | + try |
| 146 | + { |
| 147 | + if (File.Exists(Properties.Settings.Default.Tools_FolderURI + "speedtest.exe") == false) |
| 148 | + { |
| 149 | + int installResult = modUpdate.InstallOoklaSpeedtest(); |
| 150 | + if (installResult == -1) |
| 151 | + { |
| 152 | + return -1; |
| 153 | + } |
| 154 | + } |
| 155 | + Process SpeedTestRunner = new Process(); |
| 156 | + SpeedTestRunner.StartInfo.UseShellExecute = false; |
| 157 | + SpeedTestRunner.StartInfo.RedirectStandardOutput = true; |
| 158 | + SpeedTestRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
| 159 | + SpeedTestRunner.StartInfo.FileName = Properties.Settings.Default.Tools_FolderURI + "speedtest.exe"; |
| 160 | + SpeedTestRunner.StartInfo.Arguments = "-f json --accept-license"; |
| 161 | + SpeedTestRunner.Start(); |
| 162 | + SpeedTestRunner.WaitForExit(); |
| 163 | + string speedJson = SpeedTestRunner.StandardOutput.ReadToEnd(); |
| 164 | + modLogging.LogEvent("Speed test results: " + speedJson, EventLogEntryType.Information, 6072); |
| 165 | + using (JsonDocument speedResults = JsonDocument.Parse(speedJson)) |
| 166 | + { |
| 167 | + string timestamp = speedResults.RootElement.GetProperty("timestamp").GetString(); |
| 168 | + double downSpeed = Math.Round(speedResults.RootElement.GetProperty("download").GetProperty("bandwidth").GetInt64() / 125000D, 2); |
| 169 | + double upSpeed = Math.Round(speedResults.RootElement.GetProperty("upload").GetProperty("bandwidth").GetInt64() / 125000D, 2); |
| 170 | + modLogging.LogEvent("Speed test results: " + downSpeed.ToString() + " Mbps Down, " + upSpeed.ToString() + " Mbps Up", EventLogEntryType.Information, 6072); |
| 171 | + |
| 172 | + string SpeedTestResultsJSON = "{\"systemdetails\":["; |
| 173 | + modDatabase.Config ConfigObj; |
| 174 | + |
| 175 | + ConfigObj = new modDatabase.Config { Key = "Speedtest_Download", Value = downSpeed.ToString() }; |
| 176 | + modDatabase.AddOrUpdateConfig(ConfigObj); |
| 177 | + SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + ","; |
| 178 | + |
| 179 | + ConfigObj = new modDatabase.Config { Key = "Speedtest_Upload", Value = upSpeed.ToString() }; |
| 180 | + modDatabase.AddOrUpdateConfig(ConfigObj); |
| 181 | + SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + ","; |
| 182 | + |
| 183 | + ConfigObj = new modDatabase.Config { Key = "Speedtest_LastRun", Value = timestamp }; |
| 184 | + modDatabase.AddOrUpdateConfig(ConfigObj); |
| 185 | + SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + "]}"; |
| 186 | + |
| 187 | + modSync.SendMessage("server", "nodedata", "systemdetails", SpeedTestResultsJSON); |
| 188 | + } |
| 189 | + return SpeedTestRunner.ExitCode; |
| 190 | + } |
| 191 | + catch (Exception err) |
| 192 | + { |
| 193 | + modLogging.LogEvent("Speed test error: " + err.Message, EventLogEntryType.Error, 6071); |
| 194 | + return -1; |
| 195 | + } |
| 196 | + } |
| 197 | + |
138 | 198 | /// <summary> |
139 | 199 | /// Attempts a specified number of pings until it receives a successful response |
140 | 200 | /// </summary> |
|
0 commit comments