IQueuedUpdater.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using Newtonsoft.Json.Linq;
  11. namespace InABox.Mobile
  12. {
  13. public interface IQueueUpdater
  14. {
  15. void ProcessQueue();
  16. }
  17. public interface IQueueUpdater<TEntity> : IQueueUpdater
  18. where TEntity : Entity, IPersistent, IRemotable, new()
  19. {
  20. void Add(TEntity entity);
  21. TEntity[] Items { get; }
  22. }
  23. public class QueuedUpdaterState
  24. {
  25. public Guid ID { get; set; }
  26. public string OriginalState { get; set; }
  27. public Dictionary<string,object> Changes { get; set; } = new();
  28. public bool DisableUpdates { get; set; }
  29. }
  30. public class QueueUpdater<TEntity> : IDisposable, IQueueUpdater<TEntity>
  31. where TEntity : Entity, IPersistent, IRemotable, new()
  32. {
  33. private static String StateFile(Guid id) => $"{id}.{typeof(TEntity).Name.Split('.').Last()}";
  34. private Dictionary<TEntity, QueuedUpdaterState> _cache = new();
  35. private string _path;
  36. private IModelHost _host;
  37. public QueueUpdater(IModelHost host, string path)
  38. {
  39. _host = host;
  40. _path = path;
  41. LoadCache();
  42. _host.AddUpdateQueue(this);
  43. }
  44. public void Add(TEntity entity)
  45. {
  46. var _state = new QueuedUpdaterState();
  47. _state.ID = Guid.NewGuid();
  48. _state.OriginalState = Serialization.Serialize(entity);
  49. Save(_state);
  50. _cache[entity] = _state;
  51. entity.PropertyChanged += (sender, args) =>
  52. {
  53. var state = _cache[entity];
  54. state.Changes[args.PropertyName] = CoreUtils.GetPropertyValue(entity,args.PropertyName);
  55. Save(state);
  56. };
  57. }
  58. public void Update(TEntity entity, Action<TEntity> action)
  59. {
  60. var state = _cache[entity];
  61. state.DisableUpdates = true;
  62. action(entity);
  63. Save(state);
  64. state.DisableUpdates = false;
  65. }
  66. private void Save(QueuedUpdaterState state)
  67. {
  68. var _text = Serialization.Serialize(state);
  69. File.WriteAllText(StateFile(state.ID), _text);
  70. }
  71. public TEntity[] Items => _cache.Keys.ToArray();
  72. private void LoadCache()
  73. {
  74. _cache.Clear();
  75. var files = Directory.GetFiles(_path, $"*.{typeof(TEntity).Name.Split('.').Last()}");
  76. foreach (var file in files)
  77. {
  78. if (!Guid.TryParse(Path.GetFileNameWithoutExtension(file), out Guid id))
  79. continue;
  80. var _text = File.ReadAllText(file);
  81. if (string.IsNullOrWhiteSpace(_text))
  82. continue;
  83. var _state = Serialization.Deserialize<QueuedUpdaterState>(_text);
  84. if (_state == null)
  85. continue;
  86. var _entity = Serialization.Deserialize<TEntity>(_state.OriginalState);
  87. if (_entity == null)
  88. continue;
  89. foreach (var change in _state.Changes)
  90. CoreUtils.SetPropertyValue(_entity,change.Key,change.Value);
  91. _cache[_entity] = _state;
  92. }
  93. }
  94. public void ProcessQueue()
  95. {
  96. var item = Items.FirstOrDefault();
  97. if (item != null)
  98. {
  99. if (item.IsChanged() && _host.IsConnected())
  100. {
  101. new Client<TEntity>().Save(item, "Updated from Mobile Device");
  102. var state = Serialization.Serialize(item);
  103. File.WriteAllText(StateFile(_cache[item].ID),state);
  104. }
  105. }
  106. }
  107. public void Dispose()
  108. {
  109. _host.RemoveUpdateQueue(this);
  110. }
  111. }
  112. }