DigitalFormDocumentFactory.cs 4.3 KB

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