DigitalFormDocumentFactory.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using InABox.Core;
  8. namespace PRS.Mobile
  9. {
  10. public interface IDigitalFormDocumentHandler
  11. {
  12. void LoadDocument(Guid id, Action<byte[]> callback);
  13. Guid SaveDocument(byte[] data);
  14. void Run(Action<bool> status);
  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. public void LoadDocument(Guid id, Action<byte[]> callback)
  23. {
  24. var fullpath = Path.Combine(CachePath, FileName(id));
  25. if (File.Exists(fullpath))
  26. callback(File.ReadAllBytes(fullpath));
  27. else
  28. {
  29. new Client<Document>().Query(
  30. new Filter<Document>(x => x.FileName).IsEqualTo(FileName(id)),
  31. null,
  32. null,
  33. (o, e) =>
  34. {
  35. var row = o?.Rows.FirstOrDefault();
  36. if (row != null)
  37. callback(row.Get<Document, byte[]>(c => c.Data));
  38. }
  39. );
  40. }
  41. }
  42. public Guid SaveDocument(byte[] data)
  43. {
  44. Guid result = Guid.NewGuid();
  45. var filename = Path.Combine(CachePath, FileName(result));
  46. File.WriteAllBytes(filename,data);
  47. return result;
  48. }
  49. private CancellationTokenSource? _cancel;
  50. public void Stop()
  51. {
  52. if (_cancel != null)
  53. {
  54. _cancel.Cancel();
  55. _cancel = null;
  56. }
  57. }
  58. public void Run(Action<bool> status)
  59. {
  60. Stop();
  61. _cancel = new CancellationTokenSource();
  62. Task.Run(
  63. () =>
  64. {
  65. bool? previouslyActive = null;
  66. while (true && !_cancel.IsCancellationRequested)
  67. {
  68. var file = Directory.EnumerateFiles(CachePath, "*.formdocument")
  69. .FirstOrDefault();
  70. var isActive = !String.IsNullOrWhiteSpace(file);
  71. if (isActive != previouslyActive)
  72. {
  73. previouslyActive = isActive;
  74. status(isActive);
  75. }
  76. if (!String.IsNullOrWhiteSpace(file) && File.Exists(file) && IsConnected)
  77. {
  78. var data = File.ReadAllBytes(file);
  79. var document = new Document()
  80. {
  81. FileName = Path.GetFileName(file),
  82. Data = data,
  83. CRC = CoreUtils.CalculateCRC(data),
  84. TimeStamp = DateTime.Now,
  85. Created = DateTime.Now,
  86. CreatedBy = ClientFactory.UserID
  87. };
  88. new Client<Document>().Save(document, "Uploaded from Mobile Device");
  89. File.Delete(file);
  90. }
  91. Thread.Sleep(1000);
  92. }
  93. },
  94. _cancel.Token
  95. );
  96. }
  97. }
  98. public static class DigitalFormDocumentFactory
  99. {
  100. private static IDigitalFormDocumentHandler? _handler;
  101. public static void Run<THandler>(THandler handler, Action<bool> status) where THandler : IDigitalFormDocumentHandler
  102. {
  103. _handler = handler;
  104. _handler.Run(status);
  105. }
  106. public static void LoadDocument(Guid id, Action<byte[]> callback) => _handler?.LoadDocument(id, callback);
  107. public static Guid SaveDocument(byte[] data) => _handler?.SaveDocument(data) ?? Guid.Empty;
  108. }
  109. }