-
Notifications
You must be signed in to change notification settings - Fork 2
/
StatusHub.cs
84 lines (68 loc) · 2.81 KB
/
StatusHub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace Opc.Ua.Cloud.Dashboard
{
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Threading.Tasks;
public class StatusHub : Hub
{
// this is our SignalR Status Hub
}
public class StatusHubClient
{
public Dictionary<string, Tuple<string, string>> TableEntries { get; set; } = new Dictionary<string, Tuple<string, string>>();
public Dictionary<string, string[]> ChartEntries { get; set; } = new Dictionary<string, string[]>();
private readonly IHubContext<StatusHub> _hubContext;
public StatusHubClient(IHubContext<StatusHub> hubContext)
{
_hubContext = hubContext;
_ = Task.Run(() => SendMessageViaSignalR());
}
private async Task SendMessageViaSignalR()
{
while (true)
{
await Task.Delay(3000).ConfigureAwait(false);
lock (TableEntries)
{
foreach (string displayName in TableEntries.Keys)
{
_hubContext.Clients.All.SendAsync("addDatasetToChart", displayName).GetAwaiter().GetResult();
}
foreach (KeyValuePair<string, string[]> entry in ChartEntries)
{
_hubContext.Clients.All.SendAsync("addDataToChart", entry.Key, entry.Value).GetAwaiter().GetResult();
}
ChartEntries.Clear();
CreateAndSendTelemetryTable();
}
}
}
private void CreateAndSendTelemetryTable()
{
// create HTML table
StringBuilder sb = new StringBuilder();
sb.Append("<table width='1000px' cellpadding='3' cellspacing='3'>");
// header
sb.Append("<tr>");
sb.Append("<th><b>Name</b></th>");
sb.Append("<th><b>Latest Value</b></th>");
sb.Append("<th><b>Time Stamp</b></th>");
sb.Append("</tr>");
// rows
foreach (KeyValuePair<string, Tuple<string, string>> item in TableEntries.ToImmutableSortedDictionary())
{
sb.Append("<tr>");
sb.Append("<td style='width:400px'>" + item.Key + "</td>");
sb.Append("<td style='width:400px'>" + item.Value.Item1 + "</td>");
sb.Append("<td style='width:200px'>" + item.Value.Item2 + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
_hubContext.Clients.All.SendAsync("addTable", sb.ToString()).GetAwaiter().GetResult();
}
}
}