-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30717.126 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pinger", "Pinger\Pinger.csproj", "{D316826F-4850-4869-9071-CCEF16AD94C3}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{D316826F-4850-4869-9071-CCEF16AD94C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{D316826F-4850-4869-9071-CCEF16AD94C3}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{D316826F-4850-4869-9071-CCEF16AD94C3}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{D316826F-4850-4869-9071-CCEF16AD94C3}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {5D73A650-C364-4DDD-B93B-55D0003D4048} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using CsvHelper; | ||
using CsvHelper.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
|
||
namespace Pinger | ||
{ | ||
public class Dataset | ||
{ | ||
public static readonly string FileName = Environment.CurrentDirectory + @"\pings.csv"; | ||
|
||
public static IEnumerable<Record> Read() | ||
{ | ||
using (var reader = new StreamReader(FileName)) | ||
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) | ||
{ | ||
return csv.GetRecords<Record>(); | ||
} | ||
} | ||
|
||
public static void Write(Record record) | ||
{ | ||
var config = new CsvConfiguration(CultureInfo.InvariantCulture) | ||
{ | ||
HasHeaderRecord = false, | ||
}; | ||
using (var stream = File.Open(FileName, FileMode.Append)) | ||
using (var writer = new StreamWriter(stream)) | ||
using (var csv = new CsvWriter(writer, config)) | ||
{ | ||
csv.WriteRecord(record); | ||
} | ||
} | ||
|
||
public static void Init() | ||
{ | ||
if (File.Exists(FileName)) | ||
return; | ||
|
||
using (var writer = new StreamWriter(FileName)) | ||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) | ||
{ | ||
csv.WriteHeader<Record>(); | ||
csv.NextRecord(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<UseWindowsForms>true</UseWindowsForms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CsvHelper" Version="22.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="icon.ico"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="icon.png"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Windows.Forms; | ||
|
||
namespace Pinger | ||
{ | ||
public class PingerAppContext : ApplicationContext | ||
{ | ||
private NotifyIcon trayIcon; | ||
private Container components = new Container(); | ||
|
||
public PingerAppContext() | ||
{ | ||
var contextMenu = new ContextMenuStrip(); | ||
|
||
var menuItemExit = new ToolStripMenuItem("Exit", null, (sender, e) => | ||
{ | ||
trayIcon.Visible = false; | ||
Application.Exit(); | ||
}); | ||
|
||
var menuItemSettings = new ToolStripMenuItem("Settings", null, (sender, e) => | ||
{ | ||
Process.Start(Settings.FileName); | ||
}); | ||
|
||
var menuItemViewData = new ToolStripMenuItem("View data", null, (sender, e) => | ||
{ | ||
Process.Start(Dataset.FileName); | ||
}); | ||
|
||
contextMenu.Items.AddRange(new[] { menuItemViewData, menuItemSettings, menuItemExit }); | ||
|
||
trayIcon = new NotifyIcon(components) | ||
{ | ||
ContextMenuStrip = contextMenu, | ||
Icon = new System.Drawing.Icon("icon.ico"), | ||
Text = "Pinger is collecting data!", | ||
Visible = true | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.NetworkInformation; | ||
using System.Threading; | ||
|
||
namespace Pinger | ||
{ | ||
class PingerCore | ||
{ | ||
public void Collect() | ||
{ | ||
Dataset.Init(); | ||
Settings.Init(); | ||
|
||
var settings = Settings.Read(); | ||
|
||
var pinger = new Ping(); | ||
Record record; | ||
|
||
while (true) | ||
{ | ||
record = new Record | ||
{ | ||
RoundtripTime = pinger.Send(settings.host).RoundtripTime, | ||
Timestamp = DateTime.Now | ||
}; | ||
|
||
Dataset.Write(record); | ||
|
||
Thread.Sleep(settings.frequencyInSec * 1000); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace Pinger | ||
{ | ||
static class Program | ||
{ | ||
/// <summary> | ||
/// The main entry point for the application. | ||
/// </summary> | ||
[STAThread] | ||
static void Main() | ||
{ | ||
Task.Run(() => new PingerCore().Collect()); | ||
|
||
Application.SetHighDpiMode(HighDpiMode.SystemAware); | ||
Application.EnableVisualStyles(); | ||
Application.SetCompatibleTextRenderingDefault(false); | ||
Application.Run(new PingerAppContext()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace Pinger | ||
{ | ||
public class Record | ||
{ | ||
public long RoundtripTime { get; set; } | ||
public DateTime Timestamp { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
|
||
namespace Pinger | ||
{ | ||
public class Settings | ||
{ | ||
public static readonly string FileName = Environment.CurrentDirectory + @"\settings.json"; | ||
|
||
public string host { get; set; } = "www.google.ca"; | ||
public int frequencyInSec { get; set; } = 3; | ||
|
||
public static Settings Read() | ||
{ | ||
return JsonSerializer.Deserialize<Settings>(File.ReadAllText(FileName)); | ||
} | ||
|
||
public static void Write(Settings settings) | ||
{ | ||
File.WriteAllText(FileName, JsonSerializer.Serialize(settings)); | ||
} | ||
|
||
public static void Init() | ||
{ | ||
if (File.Exists(FileName)) | ||
return; | ||
|
||
Write(new Settings()); | ||
} | ||
} | ||
} |
Binary file not shown.