ServerConsole.cs 2.8 KB

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