-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (120 loc) · 5.32 KB
/
Program.cs
File metadata and controls
136 lines (120 loc) · 5.32 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.WebSocketMessagingSystem;
using Eneter.Messaging.Nodes.Broker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
// Website Eneter Messaging http://www.eneter.net/ProductDownload.htm
// Must install library Install-Package Eneter.Messaging.Framework -Version 8.0.4 from Nuget
namespace RealTimeChartWeb
{
// Properties for value Chart
public class DataUsage
{
public float values { get; set; }
}
class Program
{
const string connectionString = "Server=DESKTOP-DMR2IJB;Database=testing;User Id=sa;Password = sz123;";
// JavaScript uses JSON serializer so set using JSON.
static ISerializer aSerializer = new DataContractJsonStringSerializer();
// Create broker.
static IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory();
static IDuplexBroker aBroker = aBrokerFactory.CreateBroker();
private static DateTime _refDate = DateTime.Now;
static void Main(string[] args)
{
SqlDependency.Start(connectionString);
// Communicate using WebSockets.
IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory();
IDuplexInputChannel anInputChannel =
aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8000/RealTimeChartWeb/");
anInputChannel.ResponseReceiverConnected += (x, y) =>
{
Console.WriteLine("Connected client: " + y.ResponseReceiverId);
};
anInputChannel.ResponseReceiverDisconnected += (x, y) =>
{
Console.WriteLine("Disconnected client: " + y.ResponseReceiverId);
};
// Attach input channel and start listeing.
aBroker.AttachDuplexInputChannel(anInputChannel);
// Start working thread monitoring the CPU usage.
bool aStopWorkingThreadFlag = false;
Thread aWorkingThread = new Thread(() =>
{
float usage = 0;
while (!aStopWorkingThreadFlag)
{
getData(ref _refDate, aSerializer, aBroker);
Thread.Sleep(100);
}
});
aWorkingThread.Start();
Console.WriteLine("RealTimeChartWeb is running press ENTER to stop.");
Console.ReadLine();
// Wait until the working thread stops.
aStopWorkingThreadFlag = true;
aWorkingThread.Join(3000);
aBroker.DetachDuplexInputChannel();
SqlDependency.Stop(connectionString);
// Detach the input channel and stop listening.
}
private static void getData(ref DateTime _refDate, ISerializer aSerializer, IDuplexBroker aBroker)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
string cmdText = "SELECT id, usage, created_at FROM RealTimeChartWeb WHERE created_at > @created_at";
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.Parameters.Add("@created_at", SqlDbType.DateTime);
command.Parameters["@created_at"].Value = _refDate;
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed) connection.Open();
var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//json = reader[0].ToString();
var date = Convert.ToDateTime(reader["created_at"]);
if (date > _refDate)
{
_refDate = date;
DataUsage aMessage = new DataUsage();
aMessage.values = float.Parse(reader["usage"].ToString());
// Serialize the message.
object aSerializedMessage = aSerializer.Serialize<DataUsage>(aMessage);
// Notify subscribers via the broker.
// Note: The broker will forward the message to subscribed clients.
aBroker.SendMessage("MyUpdateData", aSerializedMessage);
}
}
}
reader.Close();
}
}
}
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
getData(ref _refDate, aSerializer, aBroker);
}
else
{
//Do somthing here
//Console.WriteLine(e.Type);
}
}
}
}