PlatformTools.cs 2.7 KB

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