LicensingEngine.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Comal.Classes;
  2. using Comal.Stores;
  3. using GenHTTP.Api.Protocol;
  4. using GenHTTP.Modules.IO;
  5. using InABox.Clients;
  6. using InABox.Configuration;
  7. using InABox.Core;
  8. using InABox.Rpc;
  9. using InABox.Wpf.Reports;
  10. using PRS.Shared;
  11. using PRSServer;
  12. using PRSServices;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using RequestMethod = GenHTTP.Api.Protocol.RequestMethod;
  20. namespace PRSLicensing;
  21. public class LicensingHandler : Handler<LicensingHandlerProperties>
  22. {
  23. public override void Init(LicensingHandlerProperties properties)
  24. {
  25. }
  26. private IResponseBuilder LicenseSummary(IRequest request)
  27. {
  28. var summary = new LicenseSummary {
  29. LicenseFees = new() {
  30. { typeof(CoreLicense).EntityName(), 7.99 },
  31. { typeof(DigitalFormsLicense).EntityName(), 3.99 },
  32. { typeof(SchedulingControlLicense).EntityName(), 1.99 },
  33. { typeof(TimeManagementLicense).EntityName(), 2.99 },
  34. { typeof(AccountsPayableLicense).EntityName(), 1.99 },
  35. { typeof(GPSTrackerLicense).EntityName(), 2.99 },
  36. { typeof(LogisticsLicense).EntityName(), 4.99 },
  37. { typeof(ScheduleEngineLicense).EntityName(), 2.99 },
  38. { typeof(QuotesManagementLicense).EntityName(), 4.99 },
  39. { typeof(LeaveManagementLicense).EntityName(), 2.99 },
  40. { typeof(TaskManagementLicense).EntityName(), 1.99 },
  41. { typeof(WarehouseLicense).EntityName(), 5.99 },
  42. { typeof(ProjectManagementLicense).EntityName(), 4.99 },
  43. { typeof(ManufacturingLicense).EntityName(), 4.99 },
  44. { typeof(ProductManagementLicense).EntityName(), 2.99 },
  45. { typeof(EquipmentLicense).EntityName(), 2.99 },
  46. { typeof(HumanResourcesLicense).EntityName(), 2.99 },
  47. { typeof(AccountsReceivableLicense).EntityName(), 1.99 }
  48. },
  49. TimeDiscounts = new() {
  50. { 1, 0.00 },
  51. { 3, 5.00 },
  52. { 6, 5.00 },
  53. { 12, 10.00 }
  54. },
  55. UserDiscounts = new() {
  56. { 1, 00.31 },
  57. { 6, 08.63 },
  58. { 11, 16.94 },
  59. { 21, 25.25 },
  60. { 51, 33.57 }
  61. }
  62. };
  63. return request.Respond().Status(ResponseStatus.OK).Content(Serialization.Serialize(summary));
  64. }
  65. private IResponseBuilder RenewLicense(IRequest request)
  66. {
  67. return request.Respond().Status(ResponseStatus.NotFound);
  68. }
  69. private IResponseBuilder Ping(IRequest request)
  70. {
  71. return request.Respond().Status(ResponseStatus.OK);
  72. }
  73. private IResponseBuilder HandleGET(IRequest request)
  74. {
  75. var endpoint = request.Target.Current?.Value.ToLower() ?? "";
  76. if(endpoint == nameof(LicenseSummary).ToLower())
  77. {
  78. return LicenseSummary(request);
  79. }
  80. else if(endpoint == nameof(Ping).ToLower())
  81. {
  82. return Ping(request);
  83. }
  84. return request.Respond().Status(ResponseStatus.NotFound);
  85. }
  86. private IResponseBuilder HandlePOST(IRequest request)
  87. {
  88. var endpoint = string.Join('/', request.Target.GetRemaining());
  89. if(endpoint == nameof(RenewLicense).ToLower())
  90. {
  91. return RenewLicense(request);
  92. }
  93. return request.Respond().Status(ResponseStatus.NotFound);
  94. }
  95. public override ValueTask<IResponse?> HandleAsync(IRequest request)
  96. {
  97. try
  98. {
  99. switch (request.Method.KnownMethod)
  100. {
  101. case RequestMethod.GET:
  102. return new ValueTask<IResponse?>(HandleGET(request).Build());
  103. case RequestMethod.POST:
  104. return new ValueTask<IResponse?>(HandlePOST(request).Build());
  105. default:
  106. Logger.Send(LogType.Error, ClientFactory.UserID, $"Request method {request.Method.RawMethod} unknown");
  107. return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.MethodNotAllowed).Build());
  108. }
  109. }
  110. catch (Exception eListen)
  111. {
  112. Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
  113. return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.InternalServerError).Build());
  114. }
  115. }
  116. }
  117. public class LicensingHandlerProperties
  118. {
  119. }
  120. public class LicensingEngine : Engine<LicensingEngineProperties>
  121. {
  122. private Listener<LicensingHandler, LicensingHandlerProperties> listener;
  123. public override void Run()
  124. {
  125. Logger.Send(LogType.Information, "", "Starting..");
  126. if (string.IsNullOrWhiteSpace(Properties.Server))
  127. {
  128. Logger.Send(LogType.Error, "", "Server is blank!");
  129. return;
  130. }
  131. var transport = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(Properties.Server, true));
  132. ClientFactory.SetClientType(typeof(RpcClient<>), Platform.WebEngine, Version, transport);
  133. CheckConnection();
  134. Logger.Send(LogType.Information, "", "Registering Classes");
  135. StoreUtils.RegisterClasses();
  136. CoreUtils.RegisterClasses();
  137. ComalUtils.RegisterClasses();
  138. PRSSharedUtils.RegisterClasses();
  139. ReportUtils.RegisterClasses();
  140. ConfigurationUtils.RegisterClasses();
  141. Logger.Send(LogType.Information, "", "Starting Listener on port " + Properties.ListenPort);
  142. try
  143. {
  144. listener = new Listener<LicensingHandler, LicensingHandlerProperties>(new LicensingHandlerProperties());
  145. listener.InitHTTPS((ushort)Properties.ListenPort, CertificateFileName());
  146. Logger.Send(LogType.Information, "", "Starting Web Listener on port " + Properties.ListenPort);
  147. listener.Start();
  148. }
  149. catch (Exception eListen)
  150. {
  151. Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
  152. }
  153. }
  154. private string CertificateFileName() => Properties.CertificateFile;
  155. private void CheckConnection()
  156. {
  157. // Wait for server connection
  158. while (!Client.Ping())
  159. {
  160. Logger.Send(LogType.Error, "", "Database server unavailable. Trying again in 30 seconds...");
  161. Task.Delay(30_000).Wait();
  162. Logger.Send(LogType.Information, "", "Retrying connection...");
  163. }
  164. ClientFactory.SetBypass();
  165. }
  166. public override void Stop()
  167. {
  168. Logger.Send(LogType.Information, "", "Stopping");
  169. listener?.Stop();
  170. }
  171. }
  172. public class LicensingEngineProperties : ServerProperties
  173. {
  174. [ComboLookupEditor(typeof(LicensingDatabaseServerLookupGenerator))]
  175. [EditorSequence(1)]
  176. public string Server { get; set; }
  177. [IntegerEditor]
  178. [EditorSequence(2)]
  179. public int ListenPort { get; set; }
  180. [EditorSequence(3)]
  181. [FileNameEditor("Certificate Files (*.pfx)|*.pfx")]
  182. public string CertificateFile { get; set; }
  183. public override ServerType Type()
  184. {
  185. return ServerType.Other;
  186. }
  187. }
  188. public class LicensingDatabaseServerLookupGenerator : LookupGenerator<LicensingEngineProperties>
  189. {
  190. public LicensingDatabaseServerLookupGenerator(LicensingEngineProperties[] items) : base(items)
  191. {
  192. }
  193. protected override void DoGenerateLookups()
  194. {
  195. var config = new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData("PRSServer"), "");
  196. var servers = config.LoadAll();
  197. foreach (var server in servers.Select(x => x.Value.CreateServer(x.Key)))
  198. {
  199. if (server.Type == ServerType.Database)
  200. {
  201. AddValue(server.Key, server.Name);
  202. }
  203. }
  204. }
  205. }