终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

134 строки
4.4 KiB

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