终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

423 rader
16 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. public async Task RunProjectAsync()
  256. {
  257. MessageLog.GetInstance.Show("启动中....");
  258. await ApplicationBaleAsync();
  259. string publishPath = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}\\";
  260. string name = $" {publishPath}{ Json<ProjectModel>.Data.ProjectName}.exe";
  261. if (File.Exists(name))
  262. {
  263. Process[] pro = Process.GetProcesses();
  264. if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.PageLoad")) == null)
  265. {
  266. Process.Start(name);
  267. }
  268. }
  269. MessageLog.GetInstance.Show("启动完成");
  270. }
  271. /// <summary>
  272. /// 应用程序打包程序
  273. /// </summary>
  274. public async Task ApplicationBaleAsync()
  275. {
  276. await Task.Factory.StartNew(new Action(() =>
  277. {
  278. if (string.IsNullOrEmpty(FileConfigModel.ConstPath))
  279. {
  280. MessageLog.GetInstance.Show("打包路径不合法,打包失败");
  281. return;
  282. }
  283. string publishPath = $"{FileConfigModel.ConstPath}\\Publish";
  284. if (Directory.Exists(publishPath))
  285. {
  286. MessageLog.GetInstance.Show("清理旧数据");
  287. Directory.Delete(publishPath, true);
  288. }
  289. MessageLog.GetInstance.Show("开始打包");
  290. string path = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}";
  291. Directory.CreateDirectory(path);
  292. MessageLog.GetInstance.Show($"打包路径:{path}");
  293. //打包组件
  294. string dllPath = AppDomain.CurrentDomain.BaseDirectory;
  295. if (Directory.Exists(dllPath)) CopyFolder(dllPath, $"{path}", new string[] { FileConfigModel.ConfigDirName, "Videos" });
  296. //打包配置文件
  297. string ConfigFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.ConfigDirName}";
  298. if (Directory.Exists(ConfigFilePath)) CopyFolder(ConfigFilePath, $"{path}\\{FileConfigModel.ConfigDirName}");
  299. //打包页面文件
  300. string pageFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.PageDirName}";
  301. if (Directory.Exists(pageFilePath)) CopyFolder(pageFilePath, $"{path}\\{FileConfigModel.PageDirName}");
  302. MessageLog.GetInstance.Show("打包完成");
  303. }));
  304. }
  305. /// <summary>
  306. /// 复制文件夹及文件
  307. /// </summary>
  308. /// <param name="sourceFolder">原文件路径</param>
  309. /// <param name="destFolder">目标文件路径</param>
  310. /// <returns></returns>
  311. private int CopyFolder(string sourceFolder, string destFolder, string[] FilterFolders = null)
  312. {
  313. try
  314. {
  315. Directory.CreateDirectory(destFolder);
  316. //得到原文件根目录下的所有文件
  317. string[] files = Directory.GetFiles(sourceFolder);
  318. foreach (string file in files)
  319. {
  320. string name = Path.GetFileName(file);
  321. if (name.Contains("BPASmart.PageLoad.exe"))
  322. {
  323. name = $"{Json<ProjectModel>.Data.ProjectName}.exe";
  324. }
  325. string dest = Path.Combine(destFolder, name);
  326. File.Copy(file, dest);//复制文件
  327. }
  328. //AccessFile,Videos
  329. //得到原文件根目录下的所有文件夹
  330. var folders = Directory.GetDirectories(sourceFolder)?.ToList();
  331. FilterFolders?.ToList()?.ForEach(item =>
  332. {
  333. int index = Array.FindIndex(folders.ToArray(), p => p.Contains(item));
  334. if (index >= 0 && index < folders.Count)
  335. {
  336. folders.RemoveAt(index);
  337. }
  338. });
  339. foreach (string folder in folders)
  340. {
  341. //if (FilterFolders!=null && FilterFolders.ToList()?.FirstOrDefault(p=>p.Contains()))
  342. string name = Path.GetFileName(folder);
  343. string dest = Path.Combine(destFolder, name);
  344. CopyFolder(folder, dest);//构建目标路径,递归复制文件
  345. }
  346. return 1;
  347. }
  348. catch (Exception e)
  349. {
  350. return -1;
  351. }
  352. }
  353. /// <summary>
  354. /// 创建快捷方式
  355. /// </summary>
  356. /// <returns></returns>
  357. private bool CreateShortcut(string name, string path)
  358. {
  359. string deskTop = string.Empty;
  360. try
  361. {
  362. if (System.IO.File.Exists($"{path}{name}.lnk")) return true;
  363. IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
  364. IWshRuntimeLibrary.IWshShortcut shortcut = shell.CreateShortcut($"{path}{name}.lnk") as IWshRuntimeLibrary.IWshShortcut;
  365. shortcut.TargetPath = path; //目标文件
  366. //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
  367. shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
  368. shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
  369. shortcut.Description = name; //描述
  370. //shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标
  371. shortcut.Arguments = "";
  372. shortcut.Save(); //必须调用保存快捷才成创建成功
  373. return true;
  374. }
  375. catch (Exception ex)
  376. {
  377. MessageLog.GetInstance.ShowEx(ex.ToString());
  378. return false;
  379. }
  380. }
  381. }
  382. }