| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System.ComponentModel.DataAnnotations;
- using System.IO.Abstractions;
- using System.Net;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Text;
- using qmonlib.Attributes;
- using Quadarax.Foundation.Core.Logging;
- using Quadarax.Foundation.Core.Object;
- using Quadarax.Foundation.Core.Reflection.Extensions;
- namespace qmonlib
- {
- public class QMonClient : DisposableObject
- {
- #region *** Properties ***
- public bool IsEnabled
- {
- get => _isEnabled;
- set
- {
- SetEnabled(value);
- }
- }
- public string InstanceIdentifier { get; }
- #endregion
- #region *** Fields ***
- private bool _isEnabled;
- private QMonConfiuration _configuration;
- private UdpClient? _udpClientGeneral;
- private UdpClient? _udpClientData;
- private Timer? _timerGeneral;
- private Timer? _timerData;
- 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
- #region *** Constructors ***
- public QMonClient(string instanceIdentifier, QMonConfiuration configuration, ILogger? externalLogger)
- {
- _log = externalLogger?.GetLogger(GetType());
- InstanceIdentifier = instanceIdentifier ?? throw new ArgumentNullException(nameof(instanceIdentifier));
- _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
- SetEnabled(_configuration.Enabled);
- ScanAssemblies();
- }
- private void ScanAssemblies()
- {
- var assemblies = AppDomain.CurrentDomain.GetAssemblies();
- foreach (var assembly in assemblies)
- {
- var types = assembly.GetTypesWithAttribute<MonitoredClassAttribute>(true);
- foreach (var type in types)
- {
- var monItemGeneral = new MonItemGeneral
- {
- Caption = type.GetCustomAttribute<MonitoredClassAttribute>()?.Caption ?? type.Name,
- Description = type.GetCustomAttribute<MonitoredClassAttribute>()?.Description ?? string.Empty,
- ViewType = MonItemGeneral.MonViewType.Item
- };
- var properties = type.GetPropertiesWithAttribute<MonitoredPropertyAttribute>(true);
- var keys = new List<Tuple<string,int>>();
- foreach (var property in properties)
- {
- var itemDescriptor = new MonItemGeneral.ItemDescriptor
- {
- Name = property.Name,
- TypeName = property.PropertyType.Name,
- Label = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Label ?? property.Name,
- Description = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Description ?? string.Empty,
- Order = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Order ?? 0,
- _propertyInfo = property
- };
- 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);
- }
- }
- }
- #endregion
- #region *** Public Operations ***
- #endregion
- #region *** Protected Operations ***
- protected override void OnDisposing()
- {
- Close();
- _generalCache.Clear();
- }
- private void Open()
- {
- if (_udpClientGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
- if (_udpClientData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
- _udpClientGeneral = CreateUdpClient(_configuration.TargetUriGeneral);
- _udpClientData = CreateUdpClient(_configuration.TargetUriData);
- _timerGeneral = new Timer(OnTimerGeneral, this, _configuration.EmitGeneralInterval, _configuration.EmitGeneralInterval);
- _timerData = new Timer(OnTimerData, this, _configuration.EmitDataInterval, _configuration.EmitDataInterval);
- }
- private void OnTimerData(object? state)
- {
-
- }
- private void OnTimerGeneral(object? state)
- {
- 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()
- {
- if (_udpClientGeneral != null)
- {
- _timerGeneral?.Dispose();
- _udpClientGeneral.Close();
- _udpClientGeneral.Dispose();
- _udpClientGeneral = null;
- _timerGeneral = null;
- }
- if (_udpClientData != null)
- {
- _timerData?.Dispose();
- _udpClientData.Close();
- _udpClientData.Dispose();
- _udpClientData = null;
- _timerData = null;
- }
- }
- private UdpClient CreateUdpClient(Uri targetUri)
- {
- var ep = new IPEndPoint(IPAddress.Parse(targetUri.DnsSafeHost), targetUri.Port);
- var udpClient = new UdpClient();
- udpClient.Connect(ep);
- return udpClient;
- }
- private void SetEnabled(bool value)
- {
- if (value == _isEnabled)
- return;
- _isEnabled = value;
- if (_isEnabled)
- Open();
- else
- Close();
- }
- #endregion
- #region *** Nested Types ***
- #endregion
- }
- }
|