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

233 regels
8.9 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.Security.Cryptography.Xml;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using BPASmartClient.CustomResource.Pages.Model;
  12. using BPA.Helper;
  13. using BPASmartClient.Model;
  14. using BPA.Helper;
  15. namespace BPASmartClient.CustomResource.Pages.ViewModel
  16. {
  17. public class MainViewModel : NotifyBase
  18. {
  19. public MainViewModel()
  20. {
  21. NavChangedCommand = new BPARelayCommand<object>(DoNavChanged);
  22. MessageLog.GetInstance.NotifyShow = new Action<string>((o) =>
  23. {
  24. Application.Current?.Dispatcher?.Invoke(() =>
  25. {
  26. //ff20aefe
  27. var temp = new MessageModel()
  28. {
  29. LogInfo = o,
  30. Forground = new SolidColorBrush(Color.FromArgb(255, 32, 174, 254))
  31. };
  32. if (DebugLogViewModel.MessageModels.Count > 0)
  33. DebugLogViewModel.MessageModels.Insert(0, temp);
  34. else
  35. DebugLogViewModel.MessageModels.Add(temp);
  36. });
  37. });
  38. MessageLog.GetInstance.NotifyShowEx = new Action<string>((o) =>
  39. {
  40. Application.Current?.Dispatcher?.Invoke(() =>
  41. {
  42. //FFF53F62
  43. var temp = new MessageModel()
  44. {
  45. LogInfo = o,
  46. Forground = new SolidColorBrush(Color.FromArgb(255, 245, 63, 98))
  47. };
  48. if (DebugLogViewModel.MessageModels.Count > 0)
  49. DebugLogViewModel.MessageModels.Insert(0, temp);
  50. else
  51. DebugLogViewModel.MessageModels.Add(temp);
  52. });
  53. });
  54. BPA.Helper.MessageLog.GetInstance.NotifyShow = new Action<string>((o) =>
  55. {
  56. try
  57. {
  58. Application.Current?.Dispatcher?.Invoke(() =>
  59. {
  60. //ff20aefe
  61. var temp = new MessageModel()
  62. {
  63. LogInfo = o,
  64. Forground = new SolidColorBrush(Color.FromArgb(255, 32, 174, 254))
  65. };
  66. if (DebugLogViewModel.MessageModels.Count > 0)
  67. DebugLogViewModel.MessageModels.Insert(0, temp);
  68. else
  69. DebugLogViewModel.MessageModels.Add(temp);
  70. });
  71. }
  72. catch (Exception)
  73. {
  74. //throw;
  75. }
  76. });
  77. BPA.Helper.MessageLog.GetInstance.NotifyShowEx = new Action<string>((o) =>
  78. {
  79. Application.Current?.Dispatcher?.Invoke(() =>
  80. {
  81. //FFF53F62
  82. var temp = new MessageModel()
  83. {
  84. LogInfo = o,
  85. Forground = new SolidColorBrush(Color.FromArgb(255, 245, 63, 98))
  86. };
  87. if (DebugLogViewModel.MessageModels.Count > 0)
  88. DebugLogViewModel.MessageModels.Insert(0, temp);
  89. else
  90. DebugLogViewModel.MessageModels.Add(temp);
  91. });
  92. });
  93. menuModels = MenuManage.GetInstance.menuModels;
  94. PermissionChange();
  95. ActionManage.GetInstance.Register(new Action(() =>
  96. {
  97. PermissionChange();
  98. }), "PermissionChange");
  99. ActionManage.GetInstance.Register(new Action<object>((c) =>
  100. {
  101. if (c != null)
  102. IsAlarm = Convert.ToBoolean(c);
  103. }), "HeartBeatCheck");
  104. LogInit();
  105. }
  106. private void PermissionChange()
  107. {
  108. int MainIndex = Array.FindIndex(menuModels.ToArray(), P => P.MainMenuVisibility == Visibility.Visible);
  109. if (MainIndex >= 0 && MainIndex < menuModels.Count)
  110. {
  111. int SubIndex; SubIndex = Array.FindIndex(menuModels.ElementAt(MainIndex).subMenumodels.ToArray(), p => p.SubMenuVisibility == Visibility.Visible);
  112. if (SubIndex >= 0 && SubIndex < menuModels.ElementAt(MainIndex).subMenumodels.Count)
  113. {
  114. DoNavChanged(menuModels.ElementAt(MainIndex).subMenumodels.ElementAt(SubIndex));
  115. }
  116. }
  117. }
  118. private void LogInit()
  119. {
  120. LogHelper.GetInstance.Init();
  121. LogHelper.GetInstance.ShowAlarmLog = s =>
  122. {
  123. //if (AlarmViewModel.AlarmInfos == null) AlarmViewModel.AlarmInfos = new ObservableCollection<Alarm>();
  124. Alarm alarm = new Alarm()
  125. {
  126. Date = s.Date,
  127. Grade = s.Grade,
  128. NumId = AlarmViewModel.AlarmInfos.Count,
  129. Info = s.MsgInfo,
  130. Time = s.Time,
  131. Value = s.Value
  132. };
  133. Application.Current?.Dispatcher.Invoke(() =>
  134. {
  135. if (AlarmViewModel.AlarmInfos.Count <= 0) AlarmViewModel.AlarmInfos.Add(alarm);
  136. else if (AlarmViewModel.AlarmInfos.Count > 0) AlarmViewModel.AlarmInfos.Insert(0, alarm);
  137. });
  138. for (int i = 0; i < AlarmViewModel.AlarmInfos.Count; i++)
  139. {
  140. AlarmViewModel.AlarmInfos[i].NumId = i + 1;
  141. }
  142. };
  143. }
  144. public ObservableCollection<MenuModel> menuModels { get; set; }
  145. private void DoNavChanged(object obj)
  146. {
  147. if (obj != null && obj is SubMenumodel menumodel)
  148. {
  149. DisplayName = menumodel.SubMenuName;
  150. var end = menumodel.AssemblyName.Substring(menumodel.AssemblyName.Length - 1);
  151. var start = menumodel.ToggleWindowPath.Substring(0, 1);
  152. bool isAddPoint = end != "." && start != ".";
  153. string point = isAddPoint ? "." : "";
  154. Type type = Assembly.Load(menumodel.AssemblyName)?.GetType($"{menumodel.AssemblyName}{point}{menumodel.ToggleWindowPath}");
  155. ConstructorInfo cti = type?.GetConstructor(System.Type.EmptyTypes);
  156. if (type?.BaseType.Name == "Window") ((Window)cti?.Invoke(null)).ShowDialog();
  157. else if (type?.BaseType.Name == "UserControl")
  158. {
  159. MainContent = null;
  160. MainContent = (FrameworkElement)cti?.Invoke(null);
  161. MainContent?.GetType()?.GetProperty("Name").SetValue(MainContent, menumodel.SubMenuName);
  162. if (MainContent is null)
  163. {
  164. var instance = type?.GetProperty("GetInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance);
  165. MainContent = null;
  166. MainContent = (FrameworkElement)instance?.GetValue(null);
  167. MainContent?.GetType()?.GetProperty("Name").SetValue(MainContent, menumodel.SubMenuName);
  168. }
  169. }
  170. }
  171. }
  172. private bool _status;
  173. /// <summary>
  174. /// 设备初始化状态
  175. /// </summary>
  176. public bool Status
  177. {
  178. get { return _status; }
  179. set
  180. {
  181. if (value)
  182. {
  183. ActionManage.GetInstance.Send("StartPlcInite");
  184. }
  185. else
  186. {
  187. ActionManage.GetInstance.Send("EndPlcInite");
  188. }
  189. _status = value;
  190. OnPropertyChanged();
  191. }
  192. }
  193. private bool _isAlarm = true;
  194. /// <summary>
  195. /// 心跳检测报警
  196. /// </summary>
  197. public bool IsAlarm
  198. {
  199. get { return _isAlarm; }
  200. set { _isAlarm = value; OnPropertyChanged(); }
  201. }
  202. /// <summary>
  203. /// 开机自启
  204. /// </summary>
  205. public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
  206. public BPARelayCommand<object> NavChangedCommand { get; set; }
  207. public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
  208. private FrameworkElement _mMainContent;
  209. public string DisplayName { get { return _mDisplayName; } set { _mDisplayName = value; OnPropertyChanged(); } }
  210. private string _mDisplayName;
  211. }
  212. }