-
Notifications
You must be signed in to change notification settings - Fork 10
/
StatusHub.cs
116 lines (97 loc) · 3.89 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class StatusHub : Hub
{
// this is our SignalR Status Hub
}
public class StatusHubClient
{
private readonly IHubContext<StatusHub> _hubContext;
private Dictionary<string, Tuple<string, bool>> TableEntries { get; set; } = new Dictionary<string, Tuple<string, bool>>();
private object _tableEntriesLock = new object();
private Dictionary<string, string[]> ChartEntries { get; set; } = new Dictionary<string, string[]>();
public StatusHubClient(IHubContext<StatusHub> hubContext)
{
_hubContext = hubContext;
_ = Task.Run(() => UpdateClientTableAndChartTaskAsync());
}
public void AddOrUpdateTableEntry(string key, string value, bool addToChart = false)
{
lock (_tableEntriesLock)
{
if (TableEntries.ContainsKey(key))
{
TableEntries[key] = new Tuple<string, bool>(value, addToChart);
}
else
{
TableEntries.Add(key, new Tuple<string, bool>(value, addToChart));
}
}
}
public void AddChartEntry(string timeAxisEntry, string[] valueAxisEntries)
{
ChartEntries.Add(timeAxisEntry, valueAxisEntries);
}
public async Task UpdateClientProgressAsync(int percentage)
{
await _hubContext.Clients.All.SendAsync("updateProgress", percentage).ConfigureAwait(false);
}
private async Task UpdateClientTableAndChartTaskAsync()
{
while (true)
{
await Task.Delay(1000).ConfigureAwait(false);
lock (_tableEntriesLock)
{
if (TableEntries.Count > 0)
{
foreach (KeyValuePair<string, Tuple<string, bool>> entry in TableEntries)
{
if (entry.Value.Item2 == true)
{
_hubContext.Clients.All.SendAsync("addDatasetToChart", entry.Key).GetAwaiter().GetResult();
}
}
foreach (KeyValuePair<string, string[]> entry in ChartEntries)
{
_hubContext.Clients.All.SendAsync("addDataToChart", entry.Key, entry.Value).GetAwaiter().GetResult();
}
ChartEntries.Clear();
CreateTable();
}
}
}
}
private void CreateTable()
{
// create HTML table
StringBuilder sb = new StringBuilder();
sb.Append("<table width='800px' 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("</tr>");
// rows
lock (_tableEntriesLock)
{
foreach (KeyValuePair<string, Tuple<string, bool>> item in TableEntries)
{
sb.Append("<tr>");
sb.Append("<td style='width:400px'>" + item.Key + "</td>");
sb.Append("<td style='width:200px'>" + item.Value.Item1 + "</td>");
sb.Append("</tr>");
}
}
sb.Append("</table>");
_hubContext.Clients.All.SendAsync("addTable", sb.ToString()).GetAwaiter().GetResult();
}
}
}