|
@@ -1,7 +1,11 @@
|
|
|
-using System.Net;
|
|
|
|
|
|
|
+using System.ComponentModel.DataAnnotations;
|
|
|
|
|
+using System.IO.Abstractions;
|
|
|
|
|
+using System.Net;
|
|
|
using System.Net.Sockets;
|
|
using System.Net.Sockets;
|
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
|
|
|
+using System.Text;
|
|
|
using qmonlib.Attributes;
|
|
using qmonlib.Attributes;
|
|
|
|
|
+using Quadarax.Foundation.Core.Logging;
|
|
|
using Quadarax.Foundation.Core.Object;
|
|
using Quadarax.Foundation.Core.Object;
|
|
|
using Quadarax.Foundation.Core.Reflection.Extensions;
|
|
using Quadarax.Foundation.Core.Reflection.Extensions;
|
|
|
|
|
|
|
@@ -31,11 +35,15 @@ namespace qmonlib
|
|
|
private Timer? _timerData;
|
|
private Timer? _timerData;
|
|
|
|
|
|
|
|
private IDictionary<string, MonItemGeneral> _generalCache = new Dictionary<string, MonItemGeneral>();
|
|
private IDictionary<string, MonItemGeneral> _generalCache = new Dictionary<string, MonItemGeneral>();
|
|
|
|
|
+ private ILog? _log;
|
|
|
|
|
+ private Quadarax.Foundation.Core.Json.Binder _binder = new Quadarax.Foundation.Core.Json.Binder(new FileSystem(),false,true,100);
|
|
|
|
|
+
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
#region *** Constructors ***
|
|
#region *** Constructors ***
|
|
|
- public QMonClient(string instanceIdentifier, QMonConfiuration configuration)
|
|
|
|
|
|
|
+ public QMonClient(string instanceIdentifier, QMonConfiuration configuration, ILogger? externalLogger)
|
|
|
{
|
|
{
|
|
|
|
|
+ _log = externalLogger?.GetLogger(GetType());
|
|
|
InstanceIdentifier = instanceIdentifier ?? throw new ArgumentNullException(nameof(instanceIdentifier));
|
|
InstanceIdentifier = instanceIdentifier ?? throw new ArgumentNullException(nameof(instanceIdentifier));
|
|
|
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
|
SetEnabled(_configuration.Enabled);
|
|
SetEnabled(_configuration.Enabled);
|
|
@@ -52,13 +60,14 @@ namespace qmonlib
|
|
|
{
|
|
{
|
|
|
var monItemGeneral = new MonItemGeneral
|
|
var monItemGeneral = new MonItemGeneral
|
|
|
{
|
|
{
|
|
|
- InstanceIdentifier = InstanceIdentifier,
|
|
|
|
|
Caption = type.GetCustomAttribute<MonitoredClassAttribute>()?.Caption ?? type.Name,
|
|
Caption = type.GetCustomAttribute<MonitoredClassAttribute>()?.Caption ?? type.Name,
|
|
|
Description = type.GetCustomAttribute<MonitoredClassAttribute>()?.Description ?? string.Empty,
|
|
Description = type.GetCustomAttribute<MonitoredClassAttribute>()?.Description ?? string.Empty,
|
|
|
ViewType = MonItemGeneral.MonViewType.Item
|
|
ViewType = MonItemGeneral.MonViewType.Item
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
var properties = type.GetPropertiesWithAttribute<MonitoredPropertyAttribute>(true);
|
|
var properties = type.GetPropertiesWithAttribute<MonitoredPropertyAttribute>(true);
|
|
|
|
|
+ var keys = new List<Tuple<string,int>>();
|
|
|
|
|
+
|
|
|
foreach (var property in properties)
|
|
foreach (var property in properties)
|
|
|
{
|
|
{
|
|
|
var itemDescriptor = new MonItemGeneral.ItemDescriptor
|
|
var itemDescriptor = new MonItemGeneral.ItemDescriptor
|
|
@@ -71,6 +80,17 @@ namespace qmonlib
|
|
|
_propertyInfo = property
|
|
_propertyInfo = property
|
|
|
};
|
|
};
|
|
|
monItemGeneral.Items.Add(itemDescriptor);
|
|
monItemGeneral.Items.Add(itemDescriptor);
|
|
|
|
|
+ if (property.GetCustomAttribute<MonitoredPropertyKeyAttribute>() != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ keys.Add(new Tuple<string, int>(property.Name, property.GetCustomAttribute<MonitoredPropertyKeyAttribute>()!.KeyOrder));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ foreach (var key in keys.OrderBy(x => x.Item2))
|
|
|
|
|
+ {
|
|
|
|
|
+ var ord = monItemGeneral.Items.FindIndex(x => x.Name == key.Item1);
|
|
|
|
|
+ if (ord>=0)
|
|
|
|
|
+ monItemGeneral.KeyOrdinals.Add(ord);
|
|
|
}
|
|
}
|
|
|
_generalCache.Add(type.FullName!, monItemGeneral);
|
|
_generalCache.Add(type.FullName!, monItemGeneral);
|
|
|
}
|
|
}
|
|
@@ -103,12 +123,38 @@ namespace qmonlib
|
|
|
|
|
|
|
|
private void OnTimerData(object? state)
|
|
private void OnTimerData(object? state)
|
|
|
{
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void OnTimerGeneral(object? state)
|
|
private void OnTimerGeneral(object? state)
|
|
|
{
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
|
|
|
|
+ if (state == null) throw new ArgumentNullException(nameof(state));
|
|
|
|
|
+ var owner = (QMonClient)state;
|
|
|
|
|
+ if (owner._udpClientGeneral == null) throw new InvalidOperationException("UDP client channel General not open.");
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ var data = new MonGeneral()
|
|
|
|
|
+ {
|
|
|
|
|
+ InstanceIdentifier = owner.InstanceIdentifier,
|
|
|
|
|
+ Items = new List<MonItemGeneral>()
|
|
|
|
|
+ };
|
|
|
|
|
+ data.Items.AddRange(owner._generalCache.Values);
|
|
|
|
|
+ var dataOut = Serialize(data);
|
|
|
|
|
+ _udpClientGeneral?.Send(dataOut);
|
|
|
|
|
+ _log?.Log(LogSeverityEnum.Trace, $"[{InstanceIdentifier}] Sent General packet size = {dataOut.Length} bytes");
|
|
|
|
|
+ }
|
|
|
|
|
+ catch(Exception ex)
|
|
|
|
|
+ {
|
|
|
|
|
+ _log?.Log(LogSeverityEnum.Error, $"[{InstanceIdentifier}] Error sending general data", ex);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private byte[] Serialize(MonGeneral data)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (data == null) throw new ArgumentNullException(nameof(data));
|
|
|
|
|
+ var json = _binder.SaveToString(data);
|
|
|
|
|
+ return Encoding.UTF8.GetBytes(json);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void Close()
|
|
private void Close()
|
|
@@ -134,7 +180,9 @@ namespace qmonlib
|
|
|
|
|
|
|
|
private UdpClient CreateUdpClient(Uri targetUri)
|
|
private UdpClient CreateUdpClient(Uri targetUri)
|
|
|
{
|
|
{
|
|
|
- var udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(targetUri.DnsSafeHost), targetUri.Port));
|
|
|
|
|
|
|
+ var ep = new IPEndPoint(IPAddress.Parse(targetUri.DnsSafeHost), targetUri.Port);
|
|
|
|
|
+ var udpClient = new UdpClient();
|
|
|
|
|
+ udpClient.Connect(ep);
|
|
|
return udpClient;
|
|
return udpClient;
|
|
|
}
|
|
}
|
|
|
private void SetEnabled(bool value)
|
|
private void SetEnabled(bool value)
|