Skip to content

Commit

Permalink
Inital implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlep committed Feb 3, 2021
1 parent 19798dd commit 86115a1
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Pinger.sln
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
50 changes: 50 additions & 0 deletions Pinger/Dataset.cs
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();
}
}
}
}
22 changes: 22 additions & 0 deletions Pinger/Pinger.csproj
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>
43 changes: 43 additions & 0 deletions Pinger/PingerAppContext.cs
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
};
}
}
}
35 changes: 35 additions & 0 deletions Pinger/PingerCore.cs
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);
}
}
}
}
23 changes: 23 additions & 0 deletions Pinger/Program.cs
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());
}
}
}
10 changes: 10 additions & 0 deletions Pinger/Record.cs
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; }
}
}
32 changes: 32 additions & 0 deletions Pinger/Settings.cs
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 added Pinger/icon.ico
Binary file not shown.

0 comments on commit 86115a1

Please sign in to comment.