123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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;
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- 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<string>? _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<string>(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<LicensingEngineProperties>(Settings.Properties) ?? new LicensingEngineProperties();
- properties.Name = Settings.ServiceName;
- return properties;
- }
- private void EditButton_Click(object sender, RoutedEventArgs e)
- {
- var grid = new DynamicItemsListGrid<LicensingEngineProperties>();
- 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));
- }
- }
|