PlatformTools.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Autofac;
  2. using InABox.Avalonia.Platform;
  3. using InABox.Core;
  4. namespace InABox.Avalonia.Platform;
  5. public static class PlatformTools
  6. {
  7. private static ContainerBuilder _builder = new ContainerBuilder();
  8. private static IContainer? _container;
  9. private static IDeviceId? _deviceId;
  10. public static IDeviceId DeviceId
  11. {
  12. get
  13. {
  14. _deviceId ??= Resolve<IDeviceId,DefaultDeviceId>();
  15. return _deviceId;
  16. }
  17. }
  18. private static IAppVersion? _appVersion;
  19. public static IAppVersion AppVersion
  20. {
  21. get
  22. {
  23. _appVersion ??= Resolve<IAppVersion, DefaultAppVersion>();
  24. return _appVersion;
  25. }
  26. }
  27. private static IImageTools? _imageTools;
  28. public static IImageTools ImageTools
  29. {
  30. get
  31. {
  32. _imageTools ??= Resolve<IImageTools, DefaultImageTools>();
  33. return _imageTools;
  34. }
  35. }
  36. private static IPdfRenderer? _pdfRenderer;
  37. public static IPdfRenderer PdfRenderer
  38. {
  39. get
  40. {
  41. _pdfRenderer ??= Resolve<IPdfRenderer, DefaultPdfRenderer>();
  42. return _pdfRenderer;
  43. }
  44. }
  45. public static Guid DigitalKeyServiceId = Guid.Parse("ce6c0b18-0000-1000-8000-00805F9B34FB");
  46. public static Guid DigitalKeyConfigId = Guid.Parse("447c1982-77ef-49be-a39a-2920f33c31e5");
  47. public static Guid DigitalKeyControlId = Guid.Parse("5b804487-b73f-406a-8240-649c23ad1590");
  48. public static double ScanTimeoutInSeconds = 15;
  49. private static IBluetooth? _bluetooth;
  50. public static IBluetooth Bluetooth
  51. {
  52. get
  53. {
  54. _bluetooth ??= Resolve<IBluetooth, DefaultBluetooth>();
  55. return _bluetooth;
  56. }
  57. }
  58. private static TInterface Resolve<TInterface, TDefault>() where TInterface : notnull, ILoggable where TDefault : TInterface, new()
  59. {
  60. _container ??= _builder.Build();
  61. var result = _container.IsRegistered<TInterface>()
  62. ?_container.Resolve<TInterface>()
  63. : new TDefault();
  64. result.Logger = Logger.Main;
  65. return result;
  66. }
  67. public static void Register<TInterface, TImplementation>()
  68. where TImplementation : TInterface
  69. where TInterface : notnull
  70. {
  71. _builder.RegisterType<TImplementation>().As<TInterface>();
  72. }
  73. }