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

191 lines
6.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPASmart.Model;
  7. using Microsoft.Toolkit.Mvvm.Input;
  8. using System.Collections.ObjectModel;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using BPA.Helper;
  12. using System.Diagnostics;
  13. using BeDesignerSCADA;
  14. using BeDesignerSCADA.Controls;
  15. using System.Windows;
  16. using System.Reflection;
  17. using System.Collections.Concurrent;
  18. namespace BPASmart.ConfigurationSoftware
  19. {
  20. public class MainWindowViewModel : NoticeBase
  21. {
  22. //public ushort CalcCRC(byte[] bytes)
  23. //{
  24. // ushort uiCrcValue = 0xffff;
  25. // ushort polynomial = 0x8408;
  26. // for (int i = 0; i < bytes.Length; i++)
  27. // {
  28. // uiCrcValue = (ushort)(uiCrcValue ^ bytes[i]);
  29. // for (int m = 0; m < 8; m++)
  30. // {
  31. // if ((uiCrcValue & 0x0001) == 1)
  32. // {
  33. // uiCrcValue = (ushort)((uiCrcValue >> 1) ^ polynomial);
  34. // }
  35. // else
  36. // {
  37. // uiCrcValue = (ushort)(uiCrcValue >> 1);
  38. // }
  39. // }
  40. // }
  41. // return uiCrcValue;
  42. //}
  43. //public byte[] GetCrc(ushort value, bool IsReverse = true)
  44. //{
  45. // byte[] bytes = new byte[2];
  46. // if (IsReverse)
  47. // {
  48. // bytes[0] = (byte)value;
  49. // bytes[1] = (byte)(value >> 8);
  50. // }
  51. // else
  52. // {
  53. // bytes[1] = (byte)value;
  54. // bytes[0] = (byte)(value >> 8);
  55. // }
  56. // return bytes;
  57. //}
  58. public MainWindowViewModel()
  59. {
  60. Init();
  61. ActionManage.GetInstance.Register(new Action<object>((o) =>
  62. {
  63. Pages.Add(o.ToString());
  64. string path = $"{ Json<ProjectModel>.Data.ProjectPath}\\{Global.PageDirectoryName}";
  65. Directory.CreateDirectory(path);
  66. File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode);
  67. Json<ProjectModel>.Data.Pages.TryAdd(o.ToString(), $"{path}\\{o.ToString()}.lay");
  68. }), "AddPage");
  69. NewProjectCommand = new RelayCommand(() =>
  70. {
  71. NewProjectView newProjectView = new NewProjectView();
  72. bool? result = newProjectView.ShowDialog();
  73. if (result != null && result == true)
  74. {
  75. if (newProjectView.Tag != null && newProjectView.Tag is NewDataModel objModel)
  76. {
  77. string path = $"{objModel.ProjectPath}\\{objModel.ProjectName}";
  78. Directory.CreateDirectory(path);
  79. Json<ProjectModel>.Data.ProjectPath = path;
  80. Json<ProjectModel>.Data.ProjectName = objModel.ProjectName;
  81. Head = objModel.ProjectName;
  82. Save();
  83. FileHelper.GetInstance.RegisterOpenFileType();
  84. }
  85. }
  86. });
  87. NewPageCommand = new RelayCommand(() =>
  88. {
  89. NewPageView newPageView = new NewPageView();
  90. newPageView.ShowDialog();
  91. });
  92. OpenVarManagerCommand = new RelayCommand(() =>
  93. {
  94. Json<LocalPar>.Data.ProjectPath = Json<ProjectModel>.Data.ProjectPath;
  95. Json<LocalPar>.Save();
  96. if (File.Exists(Global.VarManagerPath))
  97. {
  98. Process[] pro = Process.GetProcesses();
  99. if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.VariableManager")) == null)
  100. {
  101. Process.Start(Global.VarManagerPath);
  102. }
  103. }
  104. });
  105. SelectedPageCommand = new RelayCommand<object>((o) =>
  106. {
  107. string path = $"{Json<ProjectModel>.Data.ProjectPath}\\{Global.PageDirectoryName}\\{o.ToString()}.lay";
  108. if (mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString()) == null)
  109. {
  110. mainCanvasPanels.Add(new MainCanvasPageModel()
  111. {
  112. MainCanvasPanelModel = new MainCanvasPanel(path),
  113. PageName = o.ToString(),
  114. });
  115. }
  116. var res = mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString());
  117. if (res != null)
  118. {
  119. MainContent = res.MainCanvasPanelModel;
  120. }
  121. });
  122. SaveProjectCommand = new RelayCommand(() =>
  123. {
  124. for (int i = 0; i < mainCanvasPanels.Count; i++)
  125. {
  126. mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.FileSave();
  127. }
  128. Save();
  129. });
  130. }
  131. private void Init()
  132. {
  133. if (Json<ProjectModel>.Data.ProjectName != null && Json<ProjectModel>.Data.ProjectName.Length > 0)
  134. {
  135. Head = Json<ProjectModel>.Data.ProjectName;
  136. }
  137. if (Json<ProjectModel>.Data.Pages?.Count > 0)
  138. {
  139. for (int i = 0; i < Json<ProjectModel>.Data.Pages.Count; i++)
  140. {
  141. Pages.Add(Json<ProjectModel>.Data.Pages.ElementAt(i).Key);
  142. }
  143. }
  144. }
  145. public void Save()
  146. {
  147. string path = Json<ProjectModel>.Data.ProjectPath;
  148. string name = Json<ProjectModel>.Data.ProjectName;
  149. var pages = Json<ProjectModel>.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
  150. if (pages != null) Json<ProjectModel>.Data.Pages = pages;
  151. Json<ProjectModel>.Save($"{path}\\{name}.project");
  152. }
  153. public RelayCommand NewProjectCommand { get; set; }
  154. public RelayCommand OpenVarManagerCommand { get; set; }
  155. public RelayCommand<object> SelectedPageCommand { get; set; }
  156. public RelayCommand SaveProjectCommand { get; set; }
  157. public RelayCommand NewPageCommand { get; set; }
  158. public string Head { get { return _mHead; } set { _mHead = value; OnPropertyChanged(); } }
  159. private string _mHead;
  160. public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
  161. private FrameworkElement _mMainContent;
  162. public List<MainCanvasPageModel> mainCanvasPanels { get; set; } = new List<MainCanvasPageModel>();
  163. public ObservableCollection<string> Pages { get; set; } = new ObservableCollection<string>();
  164. }
  165. }