Korero – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Linq;
3 using System.Threading;
4 using System.Windows.Forms;
5 using System.Windows.Forms.DataVisualization.Charting;
6 using Korero.Communication;
7 using Korero.Serialization;
8 using Korero.Utilities;
9  
10 namespace Korero.Heartbeat
11 {
12 public partial class HeartbeatForm : Form
13 {
14 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
15  
16 private readonly CancellationToken _cancellationToken;
17  
18 private readonly CancellationTokenSource _cancellationTokenSource;
19  
20 private readonly MainForm _mainForm;
21  
22 private readonly MqttCommunication _mqttCommunication;
23  
24 #endregion
25  
26 #region Constructors, Destructors and Finalizers
27  
28 public HeartbeatForm()
29 {
30 InitializeComponent();
31  
32 _cancellationTokenSource = new CancellationTokenSource();
33 _cancellationToken = _cancellationTokenSource.Token;
34 }
35  
36 /// <summary>
37 /// Clean up any resources being used.
38 /// </summary>
39 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
40 protected override void Dispose(bool disposing)
41 {
42 if (disposing && components != null)
43 {
44 components.Dispose();
45 }
46  
47 _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
48 base.Dispose(disposing);
49 }
50  
51 public HeartbeatForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
52 {
53 _mainForm = mainForm;
54 _mqttCommunication = mqttCommunication;
55  
56 _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
57 }
58  
59 #endregion
60  
61 #region Event Handlers
2 office 62 private void HeartbeatForm_Load(object sender, EventArgs e)
63 {
64 Utilities.WindowState.FormTracker.Track(this);
65 }
1 office 66  
67 private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
68 {
69 switch (e.Notification["notification"])
70 {
71 case "heartbeat":
72 var data = e.Notification["data"];
73 if (string.IsNullOrEmpty(data))
74 {
75 break;
76 }
77  
78 var metrics = new CSV(data).ToArray();
79 for (var i = 0; i < metrics.Length; ++i)
80 {
81 var value = metrics.ElementAtOrDefault(i + 1);
82 switch (metrics[i])
83 {
84 case "AverageCPUUsage":
85 if (int.TryParse(value, out var cpuUsage))
86 {
87 aquaGauge1.InvokeIfRequired(aquaGauge => { aquaGauge.Value = cpuUsage; });
88 }
89  
90 break;
91 case "AverageRAMUsage":
92 if (int.TryParse(value, out var ramUsage))
93 {
94 var dataPoint = new DataPoint(DateTime.Now.ToOADate(), ramUsage);
95  
96 label1.InvokeIfRequired(label => { label.Text = $"{ramUsage / 1024 / 1024}MiB"; });
97  
98 try
99 {
100 if (_cancellationToken.IsCancellationRequested)
101 {
102 break;
103 }
104  
105 chart1.InvokeIfRequired(chart =>
106 {
107 chart.Series["RAM"].Points.Add(dataPoint);
108 });
109 }
110 catch
111 {
112 // Suppress all errors.
113 }
114 }
115  
116 break;
117 }
118 }
119  
120 break;
121 }
122 }
123  
124 private void HeartbeatForm_FormClosing(object sender, FormClosingEventArgs e)
125 {
126 _cancellationTokenSource.Cancel();
127 }
128  
129 #endregion
2 office 130  
131  
1 office 132 }
133 }