using H.Pipes; using InABox.Wpf.Console; using PRSServices; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Media; using System.Windows.Threading; using Comal.Classes; using H.Formatters; using InABox.Core; namespace PRSLicensing; public class LicensingConsole : InABox.Wpf.Console.Console { private PipeClient? _client; private PRSService? _service; private readonly bool _monitoronly = true; public string ServiceName { get; private set; } private Timer? RefreshTimer; public LicensingConsole(string servicename, string description, bool monitoronly = true): base(description) { ServiceName = servicename; _monitoronly = monitoronly; } protected override void OnLoaded() { base.OnLoaded(); _client = new PipeClient(ServiceName, ".", formatter:new BinaryFormatter()); _client.MessageReceived += (o, args) => { Dispatcher.BeginInvoke(() => { ConsoleControl.LoadLogEntry(args.Message ?? ""); }); }; _client.Connected += (o, args) => { Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = true; }); }; _client.Disconnected += (o, args) => { Dispatcher.BeginInvoke(() => { ConsoleControl.Enabled = false; }); if (RefreshTimer == null) { RefreshTimer = new Timer(1000); RefreshTimer.Elapsed += RefreshTimer_Elapsed; } RefreshTimer.Start(); }; _client.ExceptionOccurred += (o, args) => { }; if (!_client.IsConnecting) { _client.ConnectAsync(); } if (!_monitoronly) { var timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 3) }; timer.Tick += (o, args) => { timer.IsEnabled = false; _service = new PRSLicensingService(ServiceName); _service.Run(ServiceName); }; timer.IsEnabled = true; } } private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e) { if (_client is null) return; if (!_client.IsConnected) { if (!_client.IsConnecting) { _client.ConnectAsync(); } } else { RefreshTimer?.Stop(); } } protected override void OnClosing() { base.OnClosing(); if (_monitoronly) { _client?.DisposeAsync().AsTask().Wait(); RefreshTimer?.Stop(); } else _service?.Halt(); } protected override string GetLogDirectory() { return LicensingEngine.GetPath(ServiceName); } }