Console.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using H.Pipes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.Wpf.Editors;
  5. using InABox.WPF;
  6. using PRSServices;
  7. using System;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. using System.Windows;
  14. using InABox.Clients;
  15. using InABox.Rpc;
  16. using PRSServer;
  17. using Comal.Classes;
  18. using InABox.Wpf;
  19. using PRS.Shared;
  20. namespace PRSLicensing;
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class Console : Window, INotifyPropertyChanged
  25. {
  26. private LicensingConfiguration Settings { get; set; }
  27. private Timer timer;
  28. public event PropertyChangedEventHandler? PropertyChanged;
  29. private bool _isRunning = false;
  30. public bool IsRunning
  31. {
  32. get => _isRunning;
  33. set
  34. {
  35. _isRunning = value;
  36. OnPropertyChanged();
  37. OnPropertyChanged(nameof(IsNotRunning));
  38. }
  39. }
  40. public bool IsNotRunning => !_isRunning;
  41. private bool _isInstalled = false;
  42. public bool IsInstalled
  43. {
  44. get => _isInstalled;
  45. set
  46. {
  47. _isInstalled = value;
  48. OnPropertyChanged();
  49. }
  50. }
  51. private PipeClient<string>? _client;
  52. private Timer? RefreshTimer;
  53. public Console()
  54. {
  55. InitializeComponent();
  56. LoadSettings();
  57. Progress.DisplayImage = PRSLicensing.Resources.splash_small.AsBitmapImage();
  58. timer = new Timer(2000);
  59. timer.Elapsed += Timer_Elapsed;
  60. timer.AutoReset = true;
  61. timer.Start();
  62. }
  63. private string GetUpdateLocation() => "https://prsdigital.com.au/updates/prs";
  64. private string GetLatestVersion(string location) => Update.GetRemoteFile($"{location}/PreRelease/version.txt").Content;
  65. private string GetReleaseNotes(string location)=> Update.GetRemoteFile($"{location}/Release Notes.txt").Content;
  66. private void Window_Loaded(object sender, RoutedEventArgs e)
  67. {
  68. var isUpdateAvailable = Update.CheckForUpdates(
  69. GetUpdateLocation, GetLatestVersion, GetReleaseNotes, null, null, false, "");
  70. RefreshStatus();
  71. Progress.ShowModal("Registering Classes", progress =>
  72. {
  73. var tasks = new Task[]
  74. {
  75. Task.Run(() => CoreUtils.RegisterClasses()),
  76. Task.Run(() => ComalUtils.RegisterClasses()),
  77. Task.Run(() => DynamicGridUtils.RegisterClasses()),
  78. };
  79. Task.WaitAll(tasks.ToArray());
  80. });
  81. }
  82. private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
  83. {
  84. RefreshStatus();
  85. }
  86. protected override void OnClosing(CancelEventArgs e)
  87. {
  88. base.OnClosing(e);
  89. _client?.DisposeAsync().AsTask().Wait();
  90. _client = null;
  91. RefreshTimer?.Stop();
  92. }
  93. private void RefreshStatus()
  94. {
  95. IsRunning = PRSServiceInstaller.IsRunning(Settings.GetServiceName());
  96. IsInstalled = PRSServiceInstaller.IsInstalled(Settings.GetServiceName());
  97. if(_client is null)
  98. {
  99. if (IsRunning)
  100. {
  101. CreateClient();
  102. }
  103. }
  104. else if(_client.PipeName != GetPipeName())
  105. {
  106. _client.DisposeAsync().AsTask().Wait();
  107. _client = null;
  108. CreateClient();
  109. }
  110. }
  111. private bool _creatingClient = false;
  112. private void CreateClient()
  113. {
  114. if (_creatingClient) return;
  115. _creatingClient = true;
  116. var client = new PipeClient<string>(GetPipeName(), ".");
  117. client.MessageReceived += (o, args) =>
  118. {
  119. Dispatcher.BeginInvoke(() =>
  120. {
  121. ConsoleControl.LoadLogEntry(args.Message ?? "");
  122. });
  123. };
  124. client.Connected += (o, args) =>
  125. {
  126. Dispatcher.BeginInvoke(() =>
  127. {
  128. ConsoleControl.Enabled = true;
  129. });
  130. };
  131. client.Disconnected += (o, args) =>
  132. {
  133. Dispatcher.BeginInvoke(() =>
  134. {
  135. ConsoleControl.Enabled = false;
  136. });
  137. if (RefreshTimer == null)
  138. {
  139. RefreshTimer = new Timer(1000);
  140. RefreshTimer.Elapsed += RefreshTimer_Elapsed;
  141. }
  142. RefreshTimer.Start();
  143. };
  144. client.ExceptionOccurred += (o, args) =>
  145. {
  146. };
  147. if (!client.IsConnecting)
  148. {
  149. client.ConnectAsync();
  150. }
  151. _client = client;
  152. _creatingClient = false;
  153. }
  154. private void RefreshTimer_Elapsed(object? sender, ElapsedEventArgs e)
  155. {
  156. if (_client is null) return;
  157. if (!_client.IsConnected)
  158. {
  159. if (!_client.IsConnecting)
  160. {
  161. _client.ConnectAsync();
  162. }
  163. }
  164. else
  165. {
  166. RefreshTimer?.Stop();
  167. }
  168. }
  169. private string GetPipeName()
  170. {
  171. return Settings.GetServiceName();
  172. }
  173. private void LoadSettings()
  174. {
  175. Settings = PRSLicensingService.GetConfiguration().Load();
  176. ServiceName.Content = Settings.ServiceName;
  177. }
  178. private void SaveSettings()
  179. {
  180. PRSLicensingService.GetConfiguration().Save(Settings);
  181. }
  182. private void Install()
  183. {
  184. var username = GetProperties().Username;
  185. string? password = null;
  186. if (!string.IsNullOrWhiteSpace(username))
  187. {
  188. var passwordEditor = new PasswordDialog(string.Format("Enter password for {0}", username));
  189. if(passwordEditor.ShowDialog() == true)
  190. {
  191. password = passwordEditor.Password;
  192. }
  193. else
  194. {
  195. password = null;
  196. }
  197. }
  198. else
  199. {
  200. username = null;
  201. }
  202. PRSServiceInstaller.InstallService(
  203. Settings.GetServiceName(),
  204. "PRS Licensing Service",
  205. Settings.ServiceName,
  206. username,
  207. password);
  208. }
  209. private void InstallButton_Click(object sender, RoutedEventArgs e)
  210. {
  211. if (PRSServiceInstaller.IsInstalled(Settings.GetServiceName()))
  212. {
  213. Progress.ShowModal("Uninstalling Service", (progress) =>
  214. {
  215. PRSServiceInstaller.UninstallService(Settings.GetServiceName());
  216. });
  217. }
  218. else
  219. {
  220. Progress.ShowModal("Installing Service", (progress) =>
  221. {
  222. Install();
  223. });
  224. }
  225. RefreshStatus();
  226. }
  227. private void StartButton_Click(object sender, RoutedEventArgs e)
  228. {
  229. if (PRSServiceInstaller.IsRunning(Settings.GetServiceName()))
  230. {
  231. Progress.ShowModal("Stopping Service", (progress) =>
  232. {
  233. PRSServiceInstaller.StopService(Settings.GetServiceName());
  234. });
  235. }
  236. else
  237. {
  238. Progress.ShowModal("Starting Service", (progress) =>
  239. {
  240. PRSServiceInstaller.StartService(Settings.GetServiceName());
  241. });
  242. }
  243. RefreshStatus();
  244. }
  245. private LicensingEngineProperties GetProperties()
  246. {
  247. var properties = Serialization.Deserialize<LicensingEngineProperties>(Settings.Properties) ?? new LicensingEngineProperties();
  248. properties.Name = Settings.ServiceName;
  249. var tokens = CoreUtils.TypeList(x =>x.IsSubclassOf(typeof(LicenseToken)))
  250. .OrderBy(x => x.EntityName().Split('.').Last())
  251. .ToArray();
  252. foreach (var token in tokens)
  253. {
  254. if (!properties.Mappings.Any(x => String.Equals(x.License, token.EntityName())))
  255. properties.Mappings.Add(new LicenseProductMapping() { License = token.EntityName() });
  256. }
  257. return properties;
  258. }
  259. private void CheckConnection()
  260. {
  261. Client.Ping();
  262. ClientFactory.SetBypass();
  263. }
  264. private void EditButton_Click(object sender, RoutedEventArgs e)
  265. {
  266. var grid = new DynamicItemsListGrid<LicensingEngineProperties>();
  267. grid.OnEditorLoaded += (editor, items) =>
  268. {
  269. var control = editor.FindEditor(nameof(LicensingEngineProperties.Mappings)) as ButtonEditorControl;
  270. if (control != null)
  271. {
  272. control.OnClick += (o, args) =>
  273. {
  274. DynamicItemsListGrid<LicenseProductMapping> mappinggrid =
  275. new DynamicItemsListGrid<LicenseProductMapping>();
  276. mappinggrid.Items = items.First().Mappings;
  277. DynamicGridUtils.CreateGridWindow("License Mappings", mappinggrid).ShowDialog();
  278. };
  279. }
  280. };
  281. Settings.CommitChanges();
  282. var properties = GetProperties();
  283. if (!String.IsNullOrWhiteSpace(properties.Server))
  284. {
  285. ClientFactory.SetClientType(
  286. typeof(RpcClient<>),
  287. Platform.LicensingEngine,
  288. CoreUtils.GetVersion(),
  289. new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(properties.Server, true))
  290. );
  291. CheckConnection();
  292. }
  293. if(grid.EditItems(new LicensingEngineProperties[] { properties }))
  294. {
  295. Settings.Properties = Serialization.Serialize(properties);
  296. Settings.ServiceName = properties.Name;
  297. if (Settings.IsChanged())
  298. {
  299. if(Settings.HasOriginalValue(x => x.ServiceName) || properties.HasOriginalValue(x => x.Username))
  300. {
  301. var oldService = LicensingConfiguration.GetServiceName(Settings.GetOriginalValue(x => x.ServiceName));
  302. if (PRSServiceInstaller.IsInstalled(oldService))
  303. {
  304. Progress.ShowModal("Modifying Service", (progress) =>
  305. {
  306. PRSServiceInstaller.UninstallService(oldService);
  307. Install();
  308. });
  309. }
  310. }
  311. SaveSettings();
  312. ServiceName.Content = Settings.ServiceName;
  313. RefreshStatus();
  314. }
  315. }
  316. }
  317. protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  318. {
  319. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  320. }
  321. }