终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

134 řádky
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using System.Collections.ObjectModel;
  8. using BPA.Helper;
  9. using System.IO;
  10. using Newtonsoft.Json;
  11. using BPASmartClient.Model;
  12. using System.Reflection;
  13. using System.Windows;
  14. using BPASmartClient.Business;
  15. namespace BPASmartClient.ViewModel
  16. {
  17. public class DeviceMonitorViewModel : NotifyBase
  18. {
  19. public DeviceMonitorViewModel()
  20. {
  21. DeviceWindowSwitch = new BPARelayCommand<object>((o) =>
  22. {
  23. if (o != null && o is string nameSpace)
  24. {
  25. foreach (var item in Devices)
  26. {
  27. var fe = item.DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace);
  28. if (fe != null)
  29. {
  30. MainContent = fe.fe;
  31. break;
  32. }
  33. }
  34. }
  35. });
  36. GetData();
  37. }
  38. private void GetData()
  39. {
  40. var result = Plugin.GetInstance.GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
  41. if (result != null)
  42. {
  43. foreach (var shop in result)//店铺集合
  44. {
  45. foreach (var device in shop.deviceModels)//设备集合
  46. {
  47. Devices.Add(new ControlDevice() { Name = device.DeviceName, Namespace = device.DeviceNamespace });
  48. }
  49. }
  50. }
  51. if (Devices.Count > 0)
  52. {
  53. Devices.ElementAt(0).IsChecked = true;
  54. for (int i = 0; i < Devices.Count; i++)
  55. {
  56. Devices.ElementAt(i).DeviceMonitors.Clear();
  57. Assembly.Load(Devices.ElementAt(i).Namespace.Substring(0, Devices.ElementAt(i).Namespace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) =>
  58. {
  59. if (type?.BaseType?.Name == "UserControl")
  60. {
  61. var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement;
  62. Devices.ElementAt(i).DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe });
  63. }
  64. });
  65. }
  66. if (Devices.ElementAt(0).DeviceMonitors.Count > 0)
  67. {
  68. Devices.ElementAt(0).DeviceMonitors.ElementAt(0).IsChecked = true;
  69. if (Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe != null) MainContent = Devices.ElementAt(0).DeviceMonitors.ElementAt(0).fe;
  70. }
  71. }
  72. }
  73. public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
  74. private FrameworkElement _mMainContent;
  75. public BPARelayCommand<object> DeviceSwitch { get; set; }
  76. public BPARelayCommand<object> DeviceWindowSwitch { get; set; }
  77. public ObservableCollection<ControlDevice> Devices { get; set; } = new ObservableCollection<ControlDevice>();
  78. }
  79. public class ControlDevice : NotifyBase
  80. {
  81. public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
  82. private string _mName;
  83. public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } }
  84. private string _mNamespace;
  85. public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } }
  86. private bool _mIsChecked;
  87. public ObservableCollection<Device> DeviceMonitors { get; set; } = new ObservableCollection<Device>();
  88. }
  89. public class Device : NotifyBase
  90. {
  91. public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
  92. private string _mName;
  93. public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } }
  94. private string _mNamespace;
  95. public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } }
  96. private bool _mIsChecked;
  97. public FrameworkElement fe { get; set; }
  98. }
  99. }