IQueuedUpdater.cs 4.1 KB

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