PlatformTools.cs 2.4 KB

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