-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cs
68 lines (58 loc) · 1.74 KB
/
Source.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
using System.Collections.Concurrent;
using MTConnect.Clients;
using Timer = System.Timers.Timer;
namespace MTCAgentTest;
public class Source
{
private Disruptor.Dsl.Disruptor<Message> _disruptor;
private ConcurrentBag<KeyValuePair<string, object>> _buffer;
private MTConnectHttpClient _source;
private object _lock = new object();
private Timer _timer;
private bool _isExecuting;
public Source(Disruptor.Dsl.Disruptor<Message> disruptor)
{
_disruptor = disruptor;
_buffer = new ConcurrentBag<KeyValuePair<string, object>>();
_source = new MTConnectHttpClient("mtconnect.mazakcorp.com", 5719);
_timer = new Timer();
_timer.Elapsed += (_, _) => { Execute(); };
_timer.Interval = 1000;
_timer.Enabled = true;
}
public void Start()
{
_source.ObservationReceived += (s, observation) =>
{
lock(_lock)
{
_buffer.Add(new KeyValuePair<string, object>(observation.DataItemId, observation.Values.ToList()));
}
};
_source.Start();
}
public void Stop()
{
_source.Stop();
}
private void Execute()
{
if (_isExecuting) return;
_isExecuting = true;
lock (_lock)
{
foreach (var observation in _buffer)
{
using (var scope = _disruptor.RingBuffer.PublishEvent())
{
var data = scope.Event();
data.Key = observation.Key;
data.Value = observation.Value;
data.Timestamp = DateTime.Now;
}
}
_buffer.Clear();
}
_isExecuting = false;
}
}