App.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Logging.Abstractions;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using System.Linq;
  8. using Wayne.FDCPOSLibrary;
  9. using System.IO;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Newtonsoft.Json;
  12. using Microsoft.AspNetCore.Http;
  13. using System.Net.Http;
  14. using System.Net.Http.Headers;
  15. using Edge.Core.Processor;
  16. using Edge.Core;
  17. using Edge.Core.UniversalApi;
  18. using Edge.Core.Processor.Dispatcher.Attributes;
  19. using Edge.Core.IndustryStandardInterface.Pump;
  20. using Edge.Core.Processor.Dispatcher;
  21. namespace Application.LicensingApp
  22. {
  23. [MetaPartsDescriptor(
  24. "lang-zh-cn:验证码lang-en-us:LicensingApp",
  25. "lang-zh-cn:用于验证设备是否有权限访问服务lang-en-us:Used to check if the device has right to access the services",
  26. new[] { "lang-zh-cn:验证码lang-en-us:LicensingApp" })]
  27. public class App : IAppProcessor
  28. {
  29. public IServiceProvider Services { get; }
  30. public string MetaConfigName { get; set; }
  31. public string SerialNumber { get; set; }
  32. public ILogger Logger { get; } = NullLogger.Instance;
  33. private IEnumerable<IProcessor> _processors;
  34. public App(IServiceProvider services, int id)
  35. {
  36. this.Services = services;
  37. if (services != null)
  38. {
  39. var loggerFactory = services.GetRequiredService<ILoggerFactory>();
  40. Logger = loggerFactory.CreateLogger("Application");
  41. }
  42. }
  43. public void Init(IEnumerable<IProcessor> processors)
  44. {
  45. _processors = processors;
  46. }
  47. public Task<bool> Start()
  48. {
  49. if (CheckLicense().Result == false)
  50. {
  51. var macAddress = Licensing.Instance().GetMacAddress(Logger);
  52. LogInfo($"License not found! Please enter license code from web page, physical address {macAddress}");
  53. Task.Delay(15 * 1000).ContinueWith(_ =>
  54. {
  55. _processors?.OfType<DefaultDispatcher>().First()
  56. .StopProcessorsAsync(
  57. _processors.WithHandlerOrApp<IFdcPumpController>().Concat(
  58. _processors.WithHandlerOrApp<IEnumerable<IFdcPumpController>>()), "Licensing check failed");
  59. });
  60. }
  61. else
  62. {
  63. LogInfo(@"License OK!");
  64. }
  65. return Task.FromResult(true);
  66. }
  67. [UniversalApi]
  68. public async Task<bool> CheckLicense(string license = "")
  69. {
  70. return await Licensing.Instance().CheckLicense();
  71. }
  72. [UniversalApi]
  73. public async Task<bool> VerifyLicense(string license)
  74. {
  75. var result = await Licensing.Instance().VerifyLicense(license);
  76. if (result == true)
  77. {
  78. LogInfo("License verification OK, please restart LiteFccCore to apply the license.");
  79. }
  80. else
  81. {
  82. LogInfo("License verification failed, please try again.");
  83. }
  84. return result;
  85. }
  86. public Task<bool> Stop()
  87. {
  88. return Task.FromResult(true);
  89. }
  90. private void LogInfo(string msg)
  91. {
  92. Console.WriteLine(" " + msg);
  93. Logger.LogError(msg);
  94. }
  95. }
  96. }