UIProvider.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Device;
  2. using Mapster;
  3. using ServiceBase;
  4. namespace EEMSMain.Service;
  5. internal class UIProvider(DeviceCollection deviceCollection) : IUIProvider
  6. {
  7. Task<bool> IUIProvider.InsertNewDevice(DeviceInfo deviceInfo)
  8. {
  9. DeviceInfo_VM device = deviceInfo.Adapt<DeviceInfo_VM>();
  10. device.IsConnected = true;
  11. if (device.Guid is null)
  12. return Task.FromResult(true);
  13. App.Current.Dispatcher.Invoke(() =>
  14. {
  15. deviceCollection.NewDeviceList[device.Guid.Value] = device;
  16. });
  17. return Task.FromResult(true);
  18. }
  19. Task<bool> IUIProvider.UpdateDevice(DeviceInfo deviceInfo)
  20. {
  21. DeviceInfo_VM device = deviceInfo.Adapt<DeviceInfo_VM>();
  22. if (device.Guid is null)
  23. return Task.FromResult(true);
  24. DeviceData_VM deviceData = new()
  25. {
  26. DeviceId = device.Guid.Value,
  27. DeviceModel = device.DeviceModel,
  28. OtherInfo = [],
  29. Alarms = []
  30. };
  31. App.Current.Dispatcher.Invoke(() =>
  32. {
  33. deviceCollection.DeviceDataList[device.Guid.Value] = deviceData;
  34. deviceCollection.DeviceList[device.Guid.Value] = device;
  35. });
  36. return Task.FromResult(true);
  37. }
  38. Task<bool> IUIProvider.UpdateDeviceList(IEnumerable<DeviceInfo> device)
  39. {
  40. return Task.FromResult(true);
  41. }
  42. Task<bool> IUIProvider.UpdateRealtimeData(Guid guid, Dictionary<string, object> data)
  43. {
  44. if (!deviceCollection.DeviceDataList.TryGetValue(guid, out DeviceData_VM? deviceData) || deviceData is null)
  45. return Task.FromResult(false);
  46. App.Current.Dispatcher?.Invoke(() =>
  47. {
  48. foreach (var item in data)
  49. deviceData.OtherInfo[item.Key] = item.Value;
  50. });
  51. return Task.FromResult(true);
  52. }
  53. Task<bool> IUIProvider.UpdateRecipeInfo(Recipe recipeInfo)
  54. {
  55. if (!deviceCollection.DeviceDataList.TryGetValue(recipeInfo.DeviceId, out DeviceData_VM? deviceData) || deviceData is null)
  56. return Task.FromResult(false);
  57. App.Current.Dispatcher?.Invoke(() =>
  58. {
  59. deviceData.RecipeInfo = recipeInfo.Adapt<RecipeInfo_VM>();
  60. });
  61. return Task.FromResult(true);
  62. }
  63. Task<bool> IUIProvider.UpdateAlarm(IEnumerable<Alarm> alarm)
  64. {
  65. if (alarm is null || alarm.FirstOrDefault() is not Alarm first)
  66. return Task.FromResult(true);
  67. if (!deviceCollection.DeviceDataList.TryGetValue(first.DeviceID, out DeviceData_VM? deviceData) || deviceData is null)
  68. return Task.FromResult(false);
  69. App.Current.Dispatcher?.Invoke(() =>
  70. {
  71. deviceData.Alarms ??= [];
  72. deviceData.Alarms.Clear();
  73. foreach (var item in alarm)
  74. deviceData.Alarms.Add(item.Adapt<Alarm_VM>());
  75. });
  76. return Task.FromResult(true);
  77. }
  78. }