123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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<string>? _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<string>(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);
- }
- }
|