QMonClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Net;
  2. using System.Net.Sockets;
  3. using System.Reflection;
  4. using qmonlib.Attributes;
  5. using Quadarax.Foundation.Core.Object;
  6. using Quadarax.Foundation.Core.Reflection.Extensions;
  7. namespace qmonlib
  8. {
  9. public class QMonClient : DisposableObject
  10. {
  11. #region *** Properties ***
  12. public bool IsEnabled
  13. {
  14. get => _isEnabled;
  15. set
  16. {
  17. SetEnabled(value);
  18. }
  19. }
  20. public string InstanceIdentifier { get; }
  21. #endregion
  22. #region *** Fields ***
  23. private bool _isEnabled;
  24. private QMonConfiuration _configuration;
  25. private UdpClient? _udpClientGeneral;
  26. private UdpClient? _udpClientData;
  27. private Timer? _timerGeneral;
  28. private Timer? _timerData;
  29. private IDictionary<string, MonItemGeneral> _generalCache = new Dictionary<string, MonItemGeneral>();
  30. #endregion
  31. #region *** Constructors ***
  32. public QMonClient(string instanceIdentifier, QMonConfiuration configuration)
  33. {
  34. InstanceIdentifier = instanceIdentifier ?? throw new ArgumentNullException(nameof(instanceIdentifier));
  35. _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  36. SetEnabled(_configuration.Enabled);
  37. ScanAssemblies();
  38. }
  39. private void ScanAssemblies()
  40. {
  41. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  42. foreach (var assembly in assemblies)
  43. {
  44. var types = assembly.GetTypesWithAttribute<MonitoredClassAttribute>(true);
  45. foreach (var type in types)
  46. {
  47. var monItemGeneral = new MonItemGeneral
  48. {
  49. InstanceIdentifier = InstanceIdentifier,
  50. Caption = type.GetCustomAttribute<MonitoredClassAttribute>()?.Caption ?? type.Name,
  51. Description = type.GetCustomAttribute<MonitoredClassAttribute>()?.Description ?? string.Empty,
  52. ViewType = MonItemGeneral.MonViewType.Item
  53. };
  54. var properties = type.GetPropertiesWithAttribute<MonitoredPropertyAttribute>(true);
  55. foreach (var property in properties)
  56. {
  57. var itemDescriptor = new MonItemGeneral.ItemDescriptor
  58. {
  59. Name = property.Name,
  60. TypeName = property.PropertyType.Name,
  61. Label = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Label ?? property.Name,
  62. Description = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Description ?? string.Empty,
  63. Order = property.GetCustomAttribute<MonitoredPropertyAttribute>()?.Order ?? 0,
  64. _propertyInfo = property
  65. };
  66. monItemGeneral.Items.Add(itemDescriptor);
  67. }
  68. _generalCache.Add(type.FullName!, monItemGeneral);
  69. }
  70. }
  71. }
  72. #endregion
  73. #region *** Public Operations ***
  74. #endregion
  75. #region *** Protected Operations ***
  76. protected override void OnDisposing()
  77. {
  78. Close();
  79. _generalCache.Clear();
  80. }
  81. private void Open()
  82. {
  83. if (_udpClientGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
  84. if (_udpClientData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
  85. _udpClientGeneral = CreateUdpClient(_configuration.TargetUriGeneral);
  86. _udpClientData = CreateUdpClient(_configuration.TargetUriData);
  87. _timerGeneral = new Timer(OnTimerGeneral, this, _configuration.EmitGeneralInterval, _configuration.EmitGeneralInterval);
  88. _timerData = new Timer(OnTimerData, this, _configuration.EmitDataInterval, _configuration.EmitDataInterval);
  89. }
  90. private void OnTimerData(object? state)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. private void OnTimerGeneral(object? state)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. private void Close()
  99. {
  100. if (_udpClientGeneral != null)
  101. {
  102. _timerGeneral?.Dispose();
  103. _udpClientGeneral.Close();
  104. _udpClientGeneral.Dispose();
  105. _udpClientGeneral = null;
  106. _timerGeneral = null;
  107. }
  108. if (_udpClientData != null)
  109. {
  110. _timerData?.Dispose();
  111. _udpClientData.Close();
  112. _udpClientData.Dispose();
  113. _udpClientData = null;
  114. _timerData = null;
  115. }
  116. }
  117. private UdpClient CreateUdpClient(Uri targetUri)
  118. {
  119. var udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(targetUri.DnsSafeHost), targetUri.Port));
  120. return udpClient;
  121. }
  122. private void SetEnabled(bool value)
  123. {
  124. if (value == _isEnabled)
  125. return;
  126. _isEnabled = value;
  127. if (_isEnabled)
  128. Open();
  129. else
  130. Close();
  131. }
  132. #endregion
  133. #region *** Nested Types ***
  134. #endregion
  135. }
  136. }