Skip to content

Commit 34984d0

Browse files
committed
Store and send speedtest results
1 parent 10521e0 commit 34984d0

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.16")]
36-
[assembly: AssemblyFileVersion("1.0.0.16")]
35+
[assembly: AssemblyVersion("1.0.0.17")]
36+
[assembly: AssemblyFileVersion("1.0.0.17")]

XRFAgent.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<UpdatePeriodically>false</UpdatePeriodically>
2626
<UpdateRequired>false</UpdateRequired>
2727
<MapFileExtensions>true</MapFileExtensions>
28-
<ApplicationRevision>16</ApplicationRevision>
28+
<ApplicationRevision>17</ApplicationRevision>
2929
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
3030
<UseApplicationTrust>false</UseApplicationTrust>
3131
<BootstrapperEnabled>true</BootstrapperEnabled>
@@ -119,7 +119,7 @@
119119
<Version>6.1.2</Version>
120120
</PackageReference>
121121
<PackageReference Include="System.Text.Json">
122-
<Version>9.0.5</Version>
122+
<Version>9.0.7</Version>
123123
</PackageReference>
124124
</ItemGroup>
125125
<ItemGroup>

modCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void Handle(string inputCommand, string inputSource, string reques
5151
case "reset" when inputData.Length == 3:
5252
if (inputData[1] == "installed" && inputData[2] == "software") { outputResponse = modSystem.ResetInstalledSoftware(); } break;
5353
case "run" when inputData.Length == 2:
54-
if (inputData[1] == "speedtest") { outputResponse = "Running speed test"; modSystem.RunSpeedTest(); } break;
54+
if (inputData[1] == "speedtest") { outputResponse = "Running speed test"; modNetwork.RunSpeedTest(); } break;
5555
case "shutdown" when inputData.Length == 2:
5656
if (inputData[1] == "host") { outputResponse = modSystem.ShutdownHost(); } break;
5757
case "update" when inputData.Length == 2:

modNetwork.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.NetworkInformation;
88
using System.Net.Sockets;
99
using System.Text;
10+
using System.Text.Json;
1011
using System.Threading;
1112
using System.Timers;
1213

@@ -135,6 +136,65 @@ public static string GetPublicIPAddress()
135136
else return "Not online";
136137
}
137138

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+
138198
/// <summary>
139199
/// Attempts a specified number of pings until it receives a successful response
140200
/// </summary>

modSystem.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -229,47 +229,6 @@ public static int InstallWindowsUpdates()
229229
}
230230
}
231231

232-
/// <summary>
233-
/// Runs Ookla Speedtest, installs Speedtest CLI tool if it is not present
234-
/// </summary>
235-
/// <returns></returns>
236-
public static int RunSpeedTest()
237-
{
238-
try
239-
{
240-
if (File.Exists(Properties.Settings.Default.Tools_FolderURI + "speedtest.exe") == false)
241-
{
242-
int installResult = modUpdate.InstallOoklaSpeedtest();
243-
if (installResult == -1)
244-
{
245-
return -1;
246-
}
247-
}
248-
Process SpeedTestRunner = new Process();
249-
SpeedTestRunner.StartInfo.UseShellExecute = false;
250-
SpeedTestRunner.StartInfo.RedirectStandardOutput = true;
251-
SpeedTestRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
252-
SpeedTestRunner.StartInfo.FileName = Properties.Settings.Default.Tools_FolderURI + "speedtest.exe";
253-
SpeedTestRunner.StartInfo.Arguments = "-f json --accept-license";
254-
SpeedTestRunner.Start();
255-
SpeedTestRunner.WaitForExit();
256-
string speedJson = SpeedTestRunner.StandardOutput.ReadToEnd();
257-
modLogging.LogEvent("Speed test results: " + speedJson, EventLogEntryType.Information, 6072);
258-
using (JsonDocument speedResults = JsonDocument.Parse(speedJson))
259-
{
260-
double downSpeed = Math.Round(speedResults.RootElement.GetProperty("download").GetProperty("bandwidth").GetInt64() / 125000D, 2);
261-
double upSpeed = Math.Round(speedResults.RootElement.GetProperty("upload").GetProperty("bandwidth").GetInt64() / 125000D, 2);
262-
modLogging.LogEvent("Speed test results: " + downSpeed.ToString() + " Mbps Down, " + upSpeed.ToString() + " Mbps Up", EventLogEntryType.Information, 6072);
263-
}
264-
return SpeedTestRunner.ExitCode;
265-
}
266-
catch(Exception err)
267-
{
268-
modLogging.LogEvent("Speed test error: " + err.Message, EventLogEntryType.Error, 6071);
269-
return -1;
270-
}
271-
}
272-
273232
/// <summary>
274233
/// Reboots the host computer
275234
/// </summary>

0 commit comments

Comments
 (0)