终端一体化运控平台
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.
 
 
 

118 lines
4.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using BPASmartClient.CustomResource.Pages.Model;
  10. using BPASmartClient.Helper;
  11. using BPASmartClient.Model;
  12. using Microsoft.Toolkit.Mvvm.ComponentModel;
  13. using Microsoft.Toolkit.Mvvm.Input;
  14. namespace BPASmartClient.CustomResource.Pages.ViewModel
  15. {
  16. public class MainViewModel : ObservableObject
  17. {
  18. public MainViewModel()
  19. {
  20. NavChangedCommand = new RelayCommand<object>(DoNavChanged);
  21. menuModels = MenuManage.GetInstance.menuModels;
  22. PermissionChange();
  23. ActionManage.GetInstance.Register(new Action(() =>
  24. {
  25. PermissionChange();
  26. }), "PermissionChange");
  27. //测试用
  28. Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "营养强化剂", DeviceIp = "192.168.1.111", RawMaterialSource = 1 });
  29. Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "甜味剂", DeviceIp = "192.168.1.112", RawMaterialSource = 1 });
  30. Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "抗氧化剂", DeviceIp = "192.168.1.113", RawMaterialSource = 1 });
  31. Global.DeviceRawMaterials.Add(new RawMaterialModel { RawMaterialName = "食用香料", DeviceIp = "192.168.1.114", RawMaterialSource = 1 });
  32. }
  33. private void PermissionChange()
  34. {
  35. int MainIndex = Array.FindIndex(menuModels.ToArray(), P => P.MainMenuVisibility == Visibility.Visible);
  36. if (MainIndex >= 0 && MainIndex < menuModels.Count)
  37. {
  38. int SubIndex; SubIndex = Array.FindIndex(menuModels.ElementAt(MainIndex).subMenumodels.ToArray(), p => p.SubMenuVisibility == Visibility.Visible);
  39. if (SubIndex >= 0 && SubIndex < menuModels.ElementAt(MainIndex).subMenumodels.Count)
  40. {
  41. DoNavChanged(menuModels.ElementAt(MainIndex).subMenumodels.ElementAt(SubIndex).ToggleWindowPath);
  42. }
  43. }
  44. }
  45. public ObservableCollection<MenuModel> menuModels { get; set; }
  46. private void DoNavChanged(object obj)
  47. {
  48. ActionManage.GetInstance.Send("RecipeIsChange");
  49. for (int i = 0; i < menuModels.Count; i++)
  50. {
  51. var res = menuModels.ElementAt(i).subMenumodels.FirstOrDefault(p => p.ToggleWindowPath == obj.ToString());
  52. if (res != null)
  53. {
  54. DisplayName = res.SubMenuName;
  55. var end = res.AssemblyName.Substring(res.AssemblyName.Length - 1);
  56. var start = res.ToggleWindowPath.Substring(0, 1);
  57. bool isAddPoint = end != "." && start != ".";
  58. string point = isAddPoint ? "." : "";
  59. Type type = Assembly.Load(res.AssemblyName)?.GetType($"{res.AssemblyName }{point}{ res.ToggleWindowPath}");
  60. ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes);
  61. if (type?.BaseType.Name == "Window")
  62. {
  63. ((Window)cti?.Invoke(null)).ShowDialog();
  64. break;
  65. }
  66. else if (type?.BaseType.Name == "UserControl")
  67. {
  68. //ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes);
  69. MainContent = (FrameworkElement)cti?.Invoke(null);
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. private bool _status;
  76. /// <summary>
  77. /// 设备初始化状态
  78. /// </summary>
  79. public bool Status
  80. {
  81. get { return _status; }
  82. set
  83. {
  84. if (value)
  85. {
  86. ActionManage.GetInstance.Send("StartPlcInite");
  87. }
  88. else
  89. {
  90. ActionManage.GetInstance.Send("EndPlcInite");
  91. }
  92. _status = value;
  93. OnPropertyChanged();
  94. }
  95. }
  96. /// <summary>
  97. /// 开机自启
  98. /// </summary>
  99. public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
  100. public RelayCommand<object> NavChangedCommand { get; set; }
  101. public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
  102. private FrameworkElement _mMainContent;
  103. public string DisplayName { get { return _mDisplayName; } set { _mDisplayName = value; OnPropertyChanged(); } }
  104. private string _mDisplayName;
  105. }
  106. }