ServerConsole.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using H.Pipes;
  2. using InABox.Wpf.Console;
  3. using PRSServices;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Windows;
  11. using System.Windows.Media;
  12. using System.Windows.Threading;
  13. using Comal.Classes;
  14. using InABox.Core;
  15. namespace PRSLicensing;
  16. public class LicensingConsole : InABox.Wpf.Console.Console
  17. {
  18. private PipeClient<string>? _client;
  19. private PRSService? _service;
  20. private readonly bool _monitoronly = true;
  21. public string ServiceName { get; private set; }
  22. private Timer? RefreshTimer;
  23. public LicensingConsole(string servicename, string description, bool monitoronly = true): base(description)
  24. {
  25. ServiceName = servicename;
  26. _monitoronly = monitoronly;
  27. }
  28. protected override void OnLoaded()
  29. {
  30. base.OnLoaded();
  31. _client = new PipeClient<string>(ServiceName, ".");
  32. _client.MessageReceived += (o, args) =>
  33. {
  34. Dispatcher.BeginInvoke(() =>
  35. {
  36. ConsoleControl.LoadLogEntry(args.Message ?? "");
  37. });
  38. };
  39. _client.Connected += (o, args) =>
  40. {
  41. Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = true; });
  42. };
  43. _client.Disconnected += (o, args) =>
  44. {
  45. Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = false; });
  46. if (RefreshTimer == null)
  47. {
  48. RefreshTimer = new Timer(1000);
  49. RefreshTimer.Elapsed += RefreshTimer_Elapsed;
  50. }
  51. RefreshTimer.Start();
  52. };
  53. _client.ExceptionOccurred += (o, args) =>
  54. {
  55. };
  56. if (!_client.IsConnecting)
  57. {
  58. _client.ConnectAsync();
  59. }
  60. if (!_monitoronly)
  61. {
  62. var timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 3) };
  63. timer.Tick += (o, args) =>
  64. {
  65. timer.IsEnabled = false;
  66. _service = new PRSLicensingService(ServiceName);
  67. _service.Run(ServiceName);
  68. };
  69. timer.IsEnabled = true;
  70. }
  71. }
  72. private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e)
  73. {
  74. if (_client is null) return;
  75. if (!_client.IsConnected)
  76. {
  77. if (!_client.IsConnecting)
  78. {
  79. _client.ConnectAsync();
  80. }
  81. }
  82. else
  83. {
  84. RefreshTimer?.Stop();
  85. }
  86. }
  87. protected override void OnClosing()
  88. {
  89. base.OnClosing();
  90. if (_monitoronly)
  91. {
  92. _client?.DisposeAsync().AsTask().Wait();
  93. RefreshTimer?.Stop();
  94. }
  95. else
  96. _service?.Halt();
  97. }
  98. protected override string GetLogDirectory()
  99. {
  100. return LicensingEngine.GetPath(ServiceName);
  101. }
  102. }