using H.Pipes; using InABox.Configuration; using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf.Editors; using InABox.WPF; using PRSServices; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PRSLicensing; /// /// Interaction logic for MainWindow.xaml /// public partial class Console : Window, INotifyPropertyChanged { private LicensingConfiguration Settings { get; set; } private Timer timer; public event PropertyChangedEventHandler? PropertyChanged; private bool _isRunning = false; public bool IsRunning { get => _isRunning; set { _isRunning = value; OnPropertyChanged(); OnPropertyChanged(nameof(IsNotRunning)); } } public bool IsNotRunning => !_isRunning; private bool _isInstalled = false; public bool IsInstalled { get => _isInstalled; set { _isInstalled = value; OnPropertyChanged(); } } private PipeClient? _client; private Timer? RefreshTimer; public Console() { InitializeComponent(); LoadSettings(); Progress.DisplayImage = PRSLicensing.Resources.splash_small.AsBitmapImage(); timer = new Timer(2000); timer.Elapsed += Timer_Elapsed; timer.AutoReset = true; timer.Start(); } private void Window_Loaded(object sender, RoutedEventArgs e) { RefreshStatus(); } private void Timer_Elapsed(object? sender, ElapsedEventArgs e) { RefreshStatus(); } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); _client?.DisposeAsync().AsTask().Wait(); _client = null; RefreshTimer?.Stop(); } private void RefreshStatus() { IsRunning = PRSServiceInstaller.IsRunning(Settings.GetServiceName()); IsInstalled = PRSServiceInstaller.IsInstalled(Settings.GetServiceName()); if(_client is null) { if (IsRunning) { CreateClient(); } } else if(_client.PipeName != GetPipeName()) { _client.DisposeAsync().AsTask().Wait(); _client = null; CreateClient(); } } private bool _creatingClient = false; private void CreateClient() { if (_creatingClient) return; _creatingClient = true; var client = new PipeClient(GetPipeName(), "."); 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(); } _client = client; _creatingClient = false; } private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e) { if (_client is null) return; if (!_client.IsConnected) { if (!_client.IsConnecting) { _client.ConnectAsync(); } } else { RefreshTimer?.Stop(); } } private string GetPipeName() { return Settings.GetServiceName(); } private void LoadSettings() { Settings = PRSLicensingService.GetConfiguration().Load(); ServiceName.Text = Settings.ServiceName; } private void SaveSettings() { PRSLicensingService.GetConfiguration().Save(Settings); } private void Install() { var username = GetProperties().Username; string? password = null; if (!string.IsNullOrWhiteSpace(username)) { var passwordEditor = new PasswordDialog(string.Format("Enter password for {0}", username)); if(passwordEditor.ShowDialog() == true) { password = passwordEditor.Password; } else { password = null; } } else { username = null; } PRSServiceInstaller.InstallService( Settings.GetServiceName(), "PRS Licensing Service", Settings.ServiceName, username, password); } private void InstallButton_Click(object sender, RoutedEventArgs e) { if (PRSServiceInstaller.IsInstalled(Settings.GetServiceName())) { Progress.ShowModal("Uninstalling Service", (progress) => { PRSServiceInstaller.UninstallService(Settings.GetServiceName()); }); } else { Progress.ShowModal("Installing Service", (progress) => { Install(); }); } RefreshStatus(); } private void StartButton_Click(object sender, RoutedEventArgs e) { if (PRSServiceInstaller.IsRunning(Settings.GetServiceName())) { Progress.ShowModal("Stopping Service", (progress) => { PRSServiceInstaller.StopService(Settings.GetServiceName()); }); } else { Progress.ShowModal("Starting Service", (progress) => { PRSServiceInstaller.StartService(Settings.GetServiceName()); }); } RefreshStatus(); } private LicensingEngineProperties GetProperties() { var properties = Serialization.Deserialize(Settings.Properties) ?? new LicensingEngineProperties(); properties.Name = Settings.ServiceName; return properties; } private void EditButton_Click(object sender, RoutedEventArgs e) { var grid = new DynamicItemsListGrid(); Settings.CommitChanges(); var properties = GetProperties(); if(grid.EditItems(new LicensingEngineProperties[] { properties })) { Settings.Properties = Serialization.Serialize(properties); Settings.ServiceName = properties.Name; if (Settings.IsChanged()) { if(Settings.HasOriginalValue(x => x.ServiceName) || properties.HasOriginalValue(x => x.Username)) { var oldService = LicensingConfiguration.GetServiceName(Settings.GetOriginalValue(x => x.ServiceName)); if (PRSServiceInstaller.IsInstalled(oldService)) { Progress.ShowModal("Modifying Service", (progress) => { PRSServiceInstaller.UninstallService(oldService); Install(); }); } } SaveSettings(); ServiceName.Text = Settings.ServiceName; RefreshStatus(); } } } protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }