DigitalFormDocumentFactory.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using InABox.Clients;
  7. namespace InABox.Core
  8. {
  9. public interface IDigitalFormDocumentHandler
  10. {
  11. void LoadDocument(Guid id, Action<byte[]> callback);
  12. byte[] LoadDocument(Guid id);
  13. Guid SaveDocument(byte[] data);
  14. void Run();
  15. void Stop();
  16. }
  17. public abstract class DigitalFormDocumentHandler : IDigitalFormDocumentHandler
  18. {
  19. private static String FileName(Guid id) => $"{id}.formdocument";
  20. protected abstract String CachePath { get; }
  21. protected abstract bool IsConnected { get; }
  22. private static Action<bool>? _status;
  23. public byte[] LoadDocument(Guid id)
  24. {
  25. var fullpath = Path.Combine(CachePath, FileName(id));
  26. if (File.Exists(fullpath))
  27. return File.ReadAllBytes(fullpath);
  28. var result = new Client<Document>().Query(
  29. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  30. null,
  31. null
  32. ).Rows.FirstOrDefault()?.Get<Document,byte[]>(c=>c.Data);
  33. return result;
  34. }
  35. public void LoadDocument(Guid id, Action<byte[]> callback)
  36. {
  37. var fullpath = Path.Combine(CachePath, FileName(id));
  38. if (File.Exists(fullpath))
  39. callback(File.ReadAllBytes(fullpath));
  40. else
  41. {
  42. new Client<Document>().Query(
  43. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  44. null,
  45. null,
  46. (o, e) =>
  47. {
  48. var row = o?.Rows.FirstOrDefault();
  49. if (row != null)
  50. callback(row.Get<Document, byte[]>(c => c.Data));
  51. }
  52. );
  53. }
  54. }
  55. public Guid SaveDocument(byte[] data)
  56. {
  57. Guid result = Guid.NewGuid();
  58. var filename = Path.Combine(CachePath, FileName(result));
  59. File.WriteAllBytes(filename,data);
  60. return result;
  61. }
  62. private CancellationTokenSource? _cancel;
  63. public void Stop()
  64. {
  65. if (_cancel != null)
  66. {
  67. _cancel.Cancel();
  68. _cancel = null;
  69. }
  70. }
  71. protected DigitalFormDocumentHandler(Action<bool> status)
  72. {
  73. _status = status;
  74. }
  75. public void Run()
  76. {
  77. Stop();
  78. _cancel = new CancellationTokenSource();
  79. Task.Run(
  80. () =>
  81. {
  82. bool? previouslyActive = null;
  83. while (_cancel?.IsCancellationRequested != true)
  84. {
  85. var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
  86. .FirstOrDefault();
  87. var isActive = !String.IsNullOrWhiteSpace(file);
  88. if (isActive != previouslyActive)
  89. {
  90. previouslyActive = isActive;
  91. _status?.Invoke(isActive);
  92. }
  93. if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
  94. {
  95. var data = File.ReadAllBytes(file);
  96. var document = new Document()
  97. {
  98. FileName = Path.GetFileName(file),
  99. Data = data,
  100. CRC = CoreUtils.CalculateCRC(data),
  101. TimeStamp = DateTime.Now,
  102. Created = DateTime.Now,
  103. CreatedBy = ClientFactory.UserID
  104. };
  105. new Client<Document>().Save(document, "Uploaded from Mobile Device");
  106. File.Delete(file);
  107. }
  108. Thread.Sleep(1000);
  109. }
  110. },
  111. _cancel.Token
  112. );
  113. }
  114. }
  115. public static class DigitalFormDocumentFactory
  116. {
  117. private static IDigitalFormDocumentHandler? _handler;
  118. public static void Init<THandler>(THandler handler) where THandler : IDigitalFormDocumentHandler
  119. {
  120. _handler = handler;
  121. }
  122. public static void Run() => _handler?.Run();
  123. public static void Stop() => _handler?.Stop();
  124. public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler?.LoadDocument(id, callback);
  125. public static byte[]? LoadDocument(Guid id) => _handler?.LoadDocument(id);
  126. public static Guid SaveDocument(byte[] data) => _handler?.SaveDocument(data) ?? Guid.Empty;
  127. }
  128. }