forked from barnstee/UA-CloudPublisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeriodicPublishing.cs
82 lines (68 loc) · 2.28 KB
/
PeriodicPublishing.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
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.Extensions.Logging;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Threading;
public class PeriodicPublishing : IDisposable
{
private readonly ILogger _logger;
private readonly Timer _timer;
public Session HeartBeatSession { get; }
public NodeId HeartBeatNodeId { get; }
public uint HeartBeatInterval { get; }
public string DisplayName { get; }
public PeriodicPublishing(
uint heartbeatInterval,
Session session,
NodeId nodeId,
string name,
ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger("PeriodicPublishing");
HeartBeatSession = session;
HeartBeatNodeId = nodeId;
HeartBeatInterval = heartbeatInterval;
DisplayName = name;
if (heartbeatInterval > 0)
{
_timer = new Timer(HeartbeatSend, null, heartbeatInterval, heartbeatInterval);
}
}
public void Dispose()
{
_timer.Dispose();
}
public void Stop()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
private void HeartbeatSend(object state)
{
try
{
MessageProcessorModel messageData = new MessageProcessorModel
{
ExpandedNodeId = NodeId.ToExpandedNodeId(HeartBeatNodeId, HeartBeatSession.NamespaceUris).ToString(),
ApplicationUri = HeartBeatSession.Endpoint.Server.ApplicationUri,
MessageContext = HeartBeatSession.MessageContext,
Name = DisplayName
};
DataValue value = HeartBeatSession.ReadValue(HeartBeatNodeId);
if (value != null)
{
messageData.Value = value;
}
// enqueue the message
MessageProcessor.Enqueue(messageData);
}
catch (Exception ex)
{
_logger.LogError($"Message for heartbeat failed with {ex.Message}'.");
}
}
}
}