Console.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using H.Pipes;
  2. using InABox.Configuration;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.Wpf.Editors;
  6. using InABox.WPF;
  7. using PRSServices;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Runtime.CompilerServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Timers;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. namespace PRSLicensing;
  26. /// <summary>
  27. /// Interaction logic for MainWindow.xaml
  28. /// </summary>
  29. public partial class Console : Window, INotifyPropertyChanged
  30. {
  31. private LicensingConfiguration Settings { get; set; }
  32. private Timer timer;
  33. public event PropertyChangedEventHandler? PropertyChanged;
  34. private bool _isRunning = false;
  35. public bool IsRunning
  36. {
  37. get => _isRunning;
  38. set
  39. {
  40. _isRunning = value;
  41. OnPropertyChanged();
  42. OnPropertyChanged(nameof(IsNotRunning));
  43. }
  44. }
  45. public bool IsNotRunning => !_isRunning;
  46. private bool _isInstalled = false;
  47. public bool IsInstalled
  48. {
  49. get => _isInstalled;
  50. set
  51. {
  52. _isInstalled = value;
  53. OnPropertyChanged();
  54. }
  55. }
  56. private PipeClient<string>? _client;
  57. private Timer? RefreshTimer;
  58. public Console()
  59. {
  60. InitializeComponent();
  61. LoadSettings();
  62. Progress.DisplayImage = PRSLicensing.Resources.splash_small.AsBitmapImage();
  63. timer = new Timer(2000);
  64. timer.Elapsed += Timer_Elapsed;
  65. timer.AutoReset = true;
  66. timer.Start();
  67. }
  68. private void Window_Loaded(object sender, RoutedEventArgs e)
  69. {
  70. RefreshStatus();
  71. }
  72. private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
  73. {
  74. RefreshStatus();
  75. }
  76. protected override void OnClosing(CancelEventArgs e)
  77. {
  78. base.OnClosing(e);
  79. _client?.DisposeAsync().AsTask().Wait();
  80. _client = null;
  81. RefreshTimer?.Stop();
  82. }
  83. private void RefreshStatus()
  84. {
  85. IsRunning = PRSServiceInstaller.IsRunning(Settings.GetServiceName());
  86. IsInstalled = PRSServiceInstaller.IsInstalled(Settings.GetServiceName());
  87. if(_client is null)
  88. {
  89. if (IsRunning)
  90. {
  91. CreateClient();
  92. }
  93. }
  94. else if(_client.PipeName != GetPipeName())
  95. {
  96. _client.DisposeAsync().AsTask().Wait();
  97. _client = null;
  98. CreateClient();
  99. }
  100. }
  101. private bool _creatingClient = false;
  102. private void CreateClient()
  103. {
  104. if (_creatingClient) return;
  105. _creatingClient = true;
  106. var client = new PipeClient<string>(GetPipeName(), ".");
  107. client.MessageReceived += (o, args) =>
  108. {
  109. Dispatcher.BeginInvoke(() =>
  110. {
  111. ConsoleControl.LoadLogEntry(args.Message ?? "");
  112. });
  113. };
  114. client.Connected += (o, args) =>
  115. {
  116. Dispatcher.BeginInvoke(() =>
  117. {
  118. ConsoleControl.Enabled = true;
  119. });
  120. };
  121. client.Disconnected += (o, args) =>
  122. {
  123. Dispatcher.BeginInvoke(() =>
  124. {
  125. ConsoleControl.Enabled = false;
  126. });
  127. if (RefreshTimer == null)
  128. {
  129. RefreshTimer = new Timer(1000);
  130. RefreshTimer.Elapsed += RefreshTimer_Elapsed;
  131. }
  132. RefreshTimer.Start();
  133. };
  134. client.ExceptionOccurred += (o, args) =>
  135. {
  136. };
  137. if (!client.IsConnecting)
  138. {
  139. client.ConnectAsync();
  140. }
  141. _client = client;
  142. _creatingClient = false;
  143. }
  144. private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e)
  145. {
  146. if (_client is null) return;
  147. if (!_client.IsConnected)
  148. {
  149. if (!_client.IsConnecting)
  150. {
  151. _client.ConnectAsync();
  152. }
  153. }
  154. else
  155. {
  156. RefreshTimer?.Stop();
  157. }
  158. }
  159. private string GetPipeName()
  160. {
  161. return Settings.GetServiceName();
  162. }
  163. private void LoadSettings()
  164. {
  165. Settings = PRSLicensingService.GetConfiguration().Load();
  166. ServiceName.Text = Settings.ServiceName;
  167. }
  168. private void SaveSettings()
  169. {
  170. PRSLicensingService.GetConfiguration().Save(Settings);
  171. }
  172. private void Install()
  173. {
  174. var username = GetProperties().Username;
  175. string? password = null;
  176. if (!string.IsNullOrWhiteSpace(username))
  177. {
  178. var passwordEditor = new PasswordDialog(string.Format("Enter password for {0}", username));
  179. if(passwordEditor.ShowDialog() == true)
  180. {
  181. password = passwordEditor.Password;
  182. }
  183. else
  184. {
  185. password = null;
  186. }
  187. }
  188. else
  189. {
  190. username = null;
  191. }
  192. PRSServiceInstaller.InstallService(
  193. Settings.GetServiceName(),
  194. "PRS Licensing Service",
  195. Settings.ServiceName,
  196. username,
  197. password);
  198. }
  199. private void InstallButton_Click(object sender, RoutedEventArgs e)
  200. {
  201. if (PRSServiceInstaller.IsInstalled(Settings.GetServiceName()))
  202. {
  203. Progress.ShowModal("Uninstalling Service", (progress) =>
  204. {
  205. PRSServiceInstaller.UninstallService(Settings.GetServiceName());
  206. });
  207. }
  208. else
  209. {
  210. Progress.ShowModal("Installing Service", (progress) =>
  211. {
  212. Install();
  213. });
  214. }
  215. RefreshStatus();
  216. }
  217. private void StartButton_Click(object sender, RoutedEventArgs e)
  218. {
  219. if (PRSServiceInstaller.IsRunning(Settings.GetServiceName()))
  220. {
  221. Progress.ShowModal("Stopping Service", (progress) =>
  222. {
  223. PRSServiceInstaller.StopService(Settings.GetServiceName());
  224. });
  225. }
  226. else
  227. {
  228. Progress.ShowModal("Starting Service", (progress) =>
  229. {
  230. PRSServiceInstaller.StartService(Settings.GetServiceName());
  231. });
  232. }
  233. RefreshStatus();
  234. }
  235. private LicensingEngineProperties GetProperties()
  236. {
  237. var properties = Serialization.Deserialize<LicensingEngineProperties>(Settings.Properties) ?? new LicensingEngineProperties();
  238. properties.Name = Settings.ServiceName;
  239. return properties;
  240. }
  241. private void EditButton_Click(object sender, RoutedEventArgs e)
  242. {
  243. var grid = new DynamicItemsListGrid<LicensingEngineProperties>();
  244. Settings.CommitChanges();
  245. var properties = GetProperties();
  246. if(grid.EditItems(new LicensingEngineProperties[] { properties }))
  247. {
  248. Settings.Properties = Serialization.Serialize(properties);
  249. Settings.ServiceName = properties.Name;
  250. if (Settings.IsChanged())
  251. {
  252. if(Settings.HasOriginalValue(x => x.ServiceName) || properties.HasOriginalValue(x => x.Username))
  253. {
  254. var oldService = LicensingConfiguration.GetServiceName(Settings.GetOriginalValue(x => x.ServiceName));
  255. if (PRSServiceInstaller.IsInstalled(oldService))
  256. {
  257. Progress.ShowModal("Modifying Service", (progress) =>
  258. {
  259. PRSServiceInstaller.UninstallService(oldService);
  260. Install();
  261. });
  262. }
  263. }
  264. SaveSettings();
  265. ServiceName.Text = Settings.ServiceName;
  266. RefreshStatus();
  267. }
  268. }
  269. }
  270. protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  271. {
  272. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  273. }
  274. }