Skip to content

Commit

Permalink
[#11] : Corriger le changement du réseau
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver254 committed May 12, 2019
1 parent 655d55b commit cb839bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/MachineMonitor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ public class MainViewModel : ViewModelBase
private readonly ConfigurationStore _configuration;
private readonly PerformanceCounter _cpuCounter;
private readonly PerformanceCounter _memoryCounter;
private readonly PerformanceCounter _networkCounter;
private readonly DispatcherTimer _timer;
private readonly PerformanceCounter _totalRamCounter;
private double _cpu;
private double _disk;
private PerformanceCounter _diskCounter;
private double _memoryTotal;
private double _network;
private PerformanceCounter _networkCounter;
private double _networkMax;
private double _ram;
private MainWindow _view;
private double _memoryTotal;
#endregion

#region Constructeurs
Expand All @@ -38,15 +37,10 @@ public class MainViewModel : ViewModelBase
/// </summary>
public MainViewModel(ConfigurationStore configuration)
{
_configuration = configuration;
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
_memoryCounter = new PerformanceCounter("Memory", "Available MBytes");

if (configuration != null && !string.IsNullOrEmpty(configuration.Network))
{
_networkCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", configuration.Network);
}

_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(500);
_timer.Tick += Timer_Tick;
Expand All @@ -55,6 +49,7 @@ public MainViewModel(ConfigurationStore configuration)
Messenger.Default.Register<UpdatedConfigurationMessage>(this, HandleUpdatedConfiguration);

OnDiskChange();
OnNetworkChange();
}
#endregion

Expand Down Expand Up @@ -118,21 +113,20 @@ public void Initialize(MainWindow view)

}

private static double ToDouble(ManagementBaseObject mo, string name, double divider)
{
var value = mo[name];
return value == null
? 0
: (double.TryParse(value.ToString(), out double result) ? result / divider : 0);
}

/// <summary>
/// Starts the counters.
/// </summary>
public void Start()
{
_timer.Start();
}
private static double ToDouble(ManagementBaseObject mo, string name, double divider)
{
var value = mo[name];
return value == null
? 0
: (double.TryParse(value.ToString(), out double result) ? result / divider : 0);
}
private double CalculatePercent()
{
double free = _memoryCounter.NextValue();
Expand Down Expand Up @@ -163,6 +157,11 @@ private void HandleUpdatedConfiguration(UpdatedConfigurationMessage updatedMessa
OnDiskChange();
break;
}
case ChangedType.Network:
{
OnNetworkChange();
break;
}
case ChangedType.Transparent:
{
OnTransparencyChange(_configuration.Transparent);
Expand All @@ -181,6 +180,16 @@ private void OnDiskChange()
{
_diskCounter = new PerformanceCounter("PhysicalDisk", "% Disk Time",_configuration.Disk);
}

private void OnNetworkChange()
{
if(string.IsNullOrEmpty(_configuration.Network))
{
_networkCounter = null;
return;
}
_networkCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", _configuration.Network);
}
private void OnTransparencyChange(bool transparent)
{
if (transparent)
Expand Down
1 change: 1 addition & 0 deletions src/MachineMonitor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Height="100"
Loaded="Window_Loaded"
ResizeMode="NoResize"
ShowInTaskbar="False"
Title="Machine Monitor"
Topmost="True"
Width="400"
Expand Down

0 comments on commit cb839bc

Please sign in to comment.