终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
5.0 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. DeviceSwitch = new RelayCommand<object>((o) =>
  23. {
  24. if (o is string nameSpace)
  25. {
  26. DeviceMonitors.Clear();
  27. Assembly.Load(nameSpace.Substring(0, nameSpace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) =>
  28. {
  29. if (type?.BaseType?.Name == "UserControl")
  30. {
  31. var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement;
  32. DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe });
  33. }
  34. });
  35. }
  36. });
  37. DeviceWindowSwitch = new RelayCommand<object>((o) =>
  38. {
  39. if (o != null && o is string nameSpace)
  40. {
  41. var fe = DeviceMonitors.FirstOrDefault(p => p.Namespace == nameSpace);
  42. if (fe != null) MainContent = fe.fe;
  43. }
  44. });
  45. GetData();
  46. }
  47. private void GetData()
  48. {
  49. //var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
  50. //string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
  51. //if (File.Exists(path))
  52. //{
  53. // string JsonString = File.ReadAllText(path);
  54. // var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  55. // if (result != null)
  56. // {
  57. // foreach (var shop in result)//店铺集合
  58. // {
  59. // foreach (var device in shop.deviceModels)//设备集合
  60. // {
  61. // Devices.Add(new Device() { Name = device.DeviceName, Namespace = device.DeviceNamespace });
  62. // }
  63. // }
  64. // }
  65. //}
  66. var result = Plugin.GetInstance().GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
  67. if (result != null)
  68. {
  69. foreach (var shop in result)//店铺集合
  70. {
  71. foreach (var device in shop.deviceModels)//设备集合
  72. {
  73. Devices.Add(new Device() { Name = device.DeviceName, Namespace = device.DeviceNamespace });
  74. }
  75. }
  76. }
  77. if (Devices.Count > 0)
  78. {
  79. Devices.ElementAt(0).IsChecked = true;
  80. DeviceMonitors.Clear();
  81. Assembly.Load(Devices.ElementAt(0).Namespace.Substring(0, Devices.ElementAt(0).Namespace.LastIndexOf("."))).GetTypes().ToList().ForEach((type) =>
  82. {
  83. if (type?.BaseType?.Name == "UserControl")
  84. {
  85. var fe = type.GetConstructor(Type.EmptyTypes).Invoke(null) as FrameworkElement;
  86. DeviceMonitors.Add(new Device() { Name = fe.Name, Namespace = type?.FullName, fe = fe });
  87. }
  88. });
  89. if (DeviceMonitors.Count > 0)
  90. {
  91. DeviceMonitors.ElementAt(0).IsChecked = true;
  92. if (DeviceMonitors.ElementAt(0).fe != null) MainContent = DeviceMonitors.ElementAt(0).fe;
  93. }
  94. }
  95. }
  96. public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
  97. private FrameworkElement _mMainContent;
  98. public RelayCommand<object> DeviceSwitch { get; set; }
  99. public RelayCommand<object> DeviceWindowSwitch { get; set; }
  100. public ObservableCollection<Device> Devices { get; set; } = new ObservableCollection<Device>();
  101. public ObservableCollection<Device> DeviceMonitors { get; set; } = new ObservableCollection<Device>();
  102. }
  103. public class Device : ObservableObject
  104. {
  105. public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
  106. private string _mName;
  107. public string Namespace { get { return _mNamespace; } set { _mNamespace = value; OnPropertyChanged(); } }
  108. private string _mNamespace;
  109. public bool IsChecked { get { return _mIsChecked; } set { _mIsChecked = value; OnPropertyChanged(); } }
  110. private bool _mIsChecked;
  111. public FrameworkElement fe { get; set; }
  112. }
  113. }