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

397 lines
15 KiB

  1. using BeDesignerSCADA.Controls;
  2. using BeDesignerSCADA.ViewModel;
  3. using BPA.Communication;
  4. using BPA.Helper;
  5. using BPASmart.Model;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. namespace BPASmart.ConfigurationSoftware
  18. {
  19. /// <summary>
  20. /// 服务中心
  21. /// </summary>
  22. public class ServiceCenter
  23. {
  24. private volatile static ServiceCenter _Instance;
  25. public static ServiceCenter GetInstance => _Instance ?? (_Instance = new ServiceCenter());
  26. private ServiceCenter() { }
  27. private PipeServer pipeServer { get; set; } = new PipeServer(PipeTopic.PipeName);
  28. private ConcurrentQueue<string> msg { get; set; } = new ConcurrentQueue<string>();
  29. public ObservableCollection<string> Message { get; set; } = new ObservableCollection<string>();
  30. public List<MainCanvasPageModel> mainCanvasPanels { get; set; } = new List<MainCanvasPageModel>();
  31. public ObservableCollection<string> Pages { get; set; } = new ObservableCollection<string>();
  32. /// <summary>
  33. /// 页面切换委托
  34. /// </summary>
  35. public Action<FrameworkElement> PageChange { get; set; }
  36. /// <summary>
  37. /// 项目名称改变委托
  38. /// </summary>
  39. public Action<string> ProjectNameChange { get; set; }
  40. public void Init()
  41. {
  42. MessageLog.GetInstance.NotifyShow = new Action<string>((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
  43. MessageLog.GetInstance.NotifyShowEx = new Action<string>((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
  44. PipeMsgMonitor();
  45. ProjectStartInit();
  46. PageAdd();
  47. }
  48. /// <summary>
  49. /// 退出通知
  50. /// </summary>
  51. public void ExitNotify()
  52. {
  53. pipeServer.SendCommand(PipeTopic.ExitNotify);
  54. }
  55. /// <summary>
  56. /// 管道消息监听,和服务启动
  57. /// </summary>
  58. private void PipeMsgMonitor()
  59. {
  60. ThreadManage.GetInstance().StartLong(new Action(() =>
  61. {
  62. while (msg.Count > 0)
  63. {
  64. if (msg.TryDequeue(out string s))
  65. {
  66. if (s == PipeTopic.VarSave)
  67. {
  68. DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath);
  69. for (int i = 0; i < mainCanvasPanels.Count; i++)
  70. {
  71. mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.VariableManagerPath(FileConfigModel.VarConfigPath);
  72. }
  73. }
  74. else
  75. {
  76. MessageLog.GetInstance.Show(s);
  77. }
  78. }
  79. }
  80. Thread.Sleep(100);
  81. }), "管道消息控制");
  82. pipeServer.PushCommand = new Action<string>((o) => { msg.Enqueue(o); });
  83. pipeServer.StartPipeStream();
  84. }
  85. /// <summary>
  86. /// 通过项目启动文件加载的数据初始化
  87. /// </summary>
  88. private void ProjectStartInit()
  89. {
  90. if (Json<ProjectModel>.Data.ProjectName != null && Json<ProjectModel>.Data.ProjectName.Length > 0)
  91. {
  92. ProjectNameChange?.Invoke(Json<ProjectModel>.Data.ProjectName);
  93. FileConfigModel.ConstPath = Json<ProjectModel>.Data.ProjectPath;
  94. DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath);
  95. }
  96. if (Json<ProjectModel>.Data.Pages?.Count > 0)
  97. {
  98. for (int i = 0; i < Json<ProjectModel>.Data.Pages.Count; i++)
  99. {
  100. Pages.Add(Json<ProjectModel>.Data.Pages.ElementAt(i).Key);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 保存配置
  106. /// </summary>
  107. private void Save()
  108. {
  109. string path = Json<ProjectModel>.Data.ProjectPath;
  110. string name = Json<ProjectModel>.Data.ProjectName;
  111. var pages = Json<ProjectModel>.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
  112. if (pages != null) Json<ProjectModel>.Data.Pages = pages;
  113. Json<ProjectModel>.Save($"{path}\\{name}.project");
  114. }
  115. /// <summary>
  116. /// 页面添加委托设置
  117. /// </summary>
  118. private void PageAdd()
  119. {
  120. ActionManage.GetInstance.Register(new Action<object>((o) =>
  121. {
  122. if (Pages.Count <= 0)
  123. {
  124. Serialization<BinConfigFile>.Data.StartPageName = o.ToString();
  125. Serialization<BinConfigFile>.Save($"{FileConfigModel.GetBinConfigPath}");
  126. }
  127. Pages.Add(o.ToString());
  128. string path = $"{ Json<ProjectModel>.Data.ProjectPath}\\{FileConfigModel.PageDirName}";
  129. Directory.CreateDirectory(path);
  130. File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode);
  131. Json<ProjectModel>.Data.Pages.TryAdd(o.ToString(), $"{path}\\{o.ToString()}.lay");
  132. }), "AddPage");
  133. }
  134. /// <summary>
  135. /// 打开新建项目弹窗
  136. /// </summary>
  137. public void OpenNewProjectWindow()
  138. {
  139. NewProjectView newProjectView = new NewProjectView();
  140. bool? result = newProjectView.ShowDialog();
  141. if (result != null && result == true)
  142. {
  143. if (newProjectView.Tag != null && newProjectView.Tag is NewProjectModel objModel)
  144. {
  145. string path = $"{objModel.ProjectPath}\\{objModel.ProjectName}";
  146. Directory.CreateDirectory(path);
  147. Json<ProjectModel>.Data.ProjectPath = path;
  148. Json<ProjectModel>.Data.ProjectName = objModel.ProjectName;
  149. ProjectNameChange?.Invoke(objModel.ProjectName);
  150. FileConfigModel.ConstPath = $"{path}";
  151. Save();
  152. FileHelper.GetInstance.RegisterOpenFileType();
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 打开新建页面弹窗
  158. /// </summary>
  159. public void OpenNewPageWindow()
  160. {
  161. NewPageView newPageView = new NewPageView();
  162. newPageView.ShowDialog();
  163. }
  164. /// <summary>
  165. /// 打开变量管理器工具
  166. /// </summary>
  167. public void OpenVariableManagerTool()
  168. {
  169. Json<LocalPar>.Data.ProjectPath = Json<ProjectModel>.Data.ProjectPath;
  170. FileConfigModel.ConstPath = Json<ProjectModel>.Data.ProjectPath;
  171. Json<LocalPar>.Save();
  172. if (File.Exists(FileConfigModel.VarManagerPath))
  173. {
  174. Process[] pro = Process.GetProcesses();
  175. if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.VariableManager")) == null)
  176. {
  177. Process.Start(FileConfigModel.VarManagerPath);
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// 选中页面
  183. /// </summary>
  184. /// <param name="o"></param>
  185. public void SelectedPage(object o)
  186. {
  187. string path = $"{Json<ProjectModel>.Data.ProjectPath}\\{FileConfigModel.PageDirName}\\{o.ToString()}.lay";
  188. if (mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString()) == null)
  189. {
  190. mainCanvasPanels.Add(new MainCanvasPageModel()
  191. {
  192. MainCanvasPanelModel = new MainCanvasPanel(path),
  193. PageName = o.ToString(),
  194. });
  195. }
  196. int index = Array.FindIndex(mainCanvasPanels.ToArray(), p => p.PageName == o.ToString());
  197. if (index >= 0 && index < mainCanvasPanels.Count)
  198. {
  199. mainCanvasPanels.ElementAt(index).MainCanvasPanelModel.VariableManagerPath(FileConfigModel.VarConfigPath);
  200. PageChange?.Invoke(mainCanvasPanels.ElementAt(index).MainCanvasPanelModel);
  201. }
  202. }
  203. /// <summary>
  204. /// 删除指定界面
  205. /// </summary>
  206. /// <param name="o"></param>
  207. public void RemovePageCommand(object o)
  208. {
  209. if (!string.IsNullOrEmpty(o?.ToString()))
  210. {
  211. if (Pages.FirstOrDefault(p => p == o.ToString()) != null)
  212. {
  213. Pages.Remove(o.ToString());
  214. string path = $"{FileConfigModel.ConstPath}\\{FileConfigModel.PageDirName}\\{o.ToString()}.lay";
  215. if (File.Exists(path)) File.Delete(path);
  216. if (!File.Exists(path)) MessageLog.GetInstance.Show($"页面 {o.ToString()} 删除成功");
  217. else MessageLog.GetInstance.Show($"页面 {o.ToString()} 删除失败");
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// 设置界面为启动界面
  223. /// </summary>
  224. /// <param name="o"></param>
  225. public void SetStartPageCommand(object o)
  226. {
  227. if (!string.IsNullOrEmpty(o?.ToString()))
  228. {
  229. Serialization<BinConfigFile>.Data.StartPageName = o.ToString();
  230. Serialization<BinConfigFile>.Save($"{FileConfigModel.GetBinConfigPath}");
  231. }
  232. }
  233. /// <summary>
  234. /// 重命名界面名称
  235. /// </summary>
  236. /// <param name="o"></param>
  237. public void ReNameCommand(object o)
  238. {
  239. if (!string.IsNullOrEmpty(o?.ToString()))
  240. {
  241. }
  242. }
  243. /// <summary>
  244. /// 项目保存
  245. /// </summary>
  246. public void ProjectSave()
  247. {
  248. for (int i = 0; i < mainCanvasPanels.Count; i++)
  249. {
  250. mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.FileSave();
  251. }
  252. Save();
  253. MessageLog.GetInstance.Show("项目保存成功");
  254. }
  255. /// <summary>
  256. /// 应用程序打包程序
  257. /// </summary>
  258. public void ApplicationBale()
  259. {
  260. if (string.IsNullOrEmpty(FileConfigModel.ConstPath))
  261. {
  262. MessageLog.GetInstance.Show("打包路径不合法,打包失败");
  263. return;
  264. }
  265. MessageLog.GetInstance.Show("开始打包");
  266. string path = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}";
  267. Directory.CreateDirectory(path);
  268. MessageLog.GetInstance.Show($"打包路径:{path}");
  269. //打包组件
  270. string dllPath = AppDomain.CurrentDomain.BaseDirectory;
  271. if (Directory.Exists(dllPath)) CopyFolder(dllPath, $"{path}", new string[] { FileConfigModel.ConfigDirName, "Videos" });
  272. //打包配置文件
  273. string ConfigFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.ConfigDirName}";
  274. if (Directory.Exists(ConfigFilePath)) CopyFolder(ConfigFilePath, $"{path}\\{FileConfigModel.ConfigDirName}");
  275. //打包页面文件
  276. string pageFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.PageDirName}";
  277. if (Directory.Exists(pageFilePath)) CopyFolder(pageFilePath, $"{path}\\{FileConfigModel.PageDirName}");
  278. //生成启动快捷方式
  279. //string createPath = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}\\";
  280. //CreateShortcut($"{Json<ProjectModel>.Data.ProjectName}.exe", createPath);
  281. MessageLog.GetInstance.Show("打包完成");
  282. }
  283. /// <summary>
  284. /// 复制文件夹及文件
  285. /// </summary>
  286. /// <param name="sourceFolder">原文件路径</param>
  287. /// <param name="destFolder">目标文件路径</param>
  288. /// <returns></returns>
  289. private int CopyFolder(string sourceFolder, string destFolder, string[] FilterFolders = null)
  290. {
  291. try
  292. {
  293. Directory.CreateDirectory(destFolder);
  294. //得到原文件根目录下的所有文件
  295. string[] files = Directory.GetFiles(sourceFolder);
  296. foreach (string file in files)
  297. {
  298. string name = Path.GetFileName(file);
  299. if (name.Contains("BPASmart.PageLoad.exe"))
  300. {
  301. name = $"{Json<ProjectModel>.Data.ProjectName}.exe";
  302. }
  303. string dest = Path.Combine(destFolder, name);
  304. File.Copy(file, dest);//复制文件
  305. }
  306. //AccessFile,Videos
  307. //得到原文件根目录下的所有文件夹
  308. var folders = Directory.GetDirectories(sourceFolder)?.ToList();
  309. FilterFolders?.ToList()?.ForEach(item =>
  310. {
  311. int index = Array.FindIndex(folders.ToArray(), p => p.Contains(item));
  312. if (index >= 0 && index < folders.Count)
  313. {
  314. folders.RemoveAt(index);
  315. }
  316. });
  317. foreach (string folder in folders)
  318. {
  319. //if (FilterFolders!=null && FilterFolders.ToList()?.FirstOrDefault(p=>p.Contains()))
  320. string name = Path.GetFileName(folder);
  321. string dest = Path.Combine(destFolder, name);
  322. CopyFolder(folder, dest);//构建目标路径,递归复制文件
  323. }
  324. return 1;
  325. }
  326. catch (Exception e)
  327. {
  328. return -1;
  329. }
  330. }
  331. /// <summary>
  332. /// 创建快捷方式
  333. /// </summary>
  334. /// <returns></returns>
  335. private bool CreateShortcut(string name, string path)
  336. {
  337. string deskTop = string.Empty;
  338. try
  339. {
  340. if (System.IO.File.Exists($"{path}{name}.lnk")) return true;
  341. IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
  342. IWshRuntimeLibrary.IWshShortcut shortcut = shell.CreateShortcut($"{path}{name}.lnk") as IWshRuntimeLibrary.IWshShortcut;
  343. shortcut.TargetPath = path; //目标文件
  344. //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
  345. shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
  346. shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
  347. shortcut.Description = name; //描述
  348. //shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标
  349. shortcut.Arguments = "";
  350. shortcut.Save(); //必须调用保存快捷才成创建成功
  351. return true;
  352. }
  353. catch (Exception ex)
  354. {
  355. MessageLog.GetInstance.ShowEx(ex.ToString());
  356. return false;
  357. }
  358. }
  359. }
  360. }