Korero – Rev 2
?pathlinks?
using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Korero.Communication;
using Korero.Serialization;
using Korero.Utilities;
namespace Korero.Heartbeat
{
public partial class HeartbeatForm : Form
{
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private readonly CancellationToken _cancellationToken;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly MainForm _mainForm;
private readonly MqttCommunication _mqttCommunication;
#endregion
#region Constructors, Destructors and Finalizers
public HeartbeatForm()
{
InitializeComponent();
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
_mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
base.Dispose(disposing);
}
public HeartbeatForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
{
_mainForm = mainForm;
_mqttCommunication = mqttCommunication;
_mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
}
#endregion
#region Event Handlers
private void HeartbeatForm_Load(object sender, EventArgs e)
{
Utilities.WindowState.FormTracker.Track(this);
}
private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
{
switch (e.Notification["notification"])
{
case "heartbeat":
var data = e.Notification["data"];
if (string.IsNullOrEmpty(data))
{
break;
}
var metrics = new CSV(data).ToArray();
for (var i = 0; i < metrics.Length; ++i)
{
var value = metrics.ElementAtOrDefault(i + 1);
switch (metrics[i])
{
case "AverageCPUUsage":
if (int.TryParse(value, out var cpuUsage))
{
aquaGauge1.InvokeIfRequired(aquaGauge => { aquaGauge.Value = cpuUsage; });
}
break;
case "AverageRAMUsage":
if (int.TryParse(value, out var ramUsage))
{
var dataPoint = new DataPoint(DateTime.Now.ToOADate(), ramUsage);
label1.InvokeIfRequired(label => { label.Text = $"{ramUsage / 1024 / 1024}MiB"; });
try
{
if (_cancellationToken.IsCancellationRequested)
{
break;
}
chart1.InvokeIfRequired(chart =>
{
chart.Series["RAM"].Points.Add(dataPoint);
});
}
catch
{
// Suppress all errors.
}
}
break;
}
}
break;
}
}
private void HeartbeatForm_FormClosing(object sender, FormClosingEventArgs e)
{
_cancellationTokenSource.Cancel();
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.