终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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