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