using BeDesignerSCADA.Controls; using BeDesignerSCADA.ViewModel; using BPA.Communication; using BPA.Helper; using BPASmart.Model; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace BPASmart.ConfigurationSoftware { /// /// 服务中心 /// public class ServiceCenter { private volatile static ServiceCenter _Instance; public static ServiceCenter GetInstance => _Instance ?? (_Instance = new ServiceCenter()); private ServiceCenter() { } private PipeServer pipeServer { get; set; } = new PipeServer(PipeTopic.PipeName); private ConcurrentQueue msg { get; set; } = new ConcurrentQueue(); public ObservableCollection Message { get; set; } = new ObservableCollection(); public List mainCanvasPanels { get; set; } = new List(); public ObservableCollection Pages { get; set; } = new ObservableCollection(); /// /// 页面切换委托 /// public Action PageChange { get; set; } /// /// 项目名称改变委托 /// public Action ProjectNameChange { get; set; } public void Init() { MessageLog.GetInstance.NotifyShow = new Action((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); }); MessageLog.GetInstance.NotifyShowEx = new Action((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); }); PipeMsgMonitor(); ProjectStartInit(); PageAdd(); } /// /// 退出通知 /// public void ExitNotify() { pipeServer.SendCommand(PipeTopic.ExitNotify); } /// /// 管道消息监听,和服务启动 /// private void PipeMsgMonitor() { ThreadManage.GetInstance().StartLong(new Action(() => { while (msg.Count > 0) { if (msg.TryDequeue(out string s)) { if (s == PipeTopic.VarSave) { DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath); for (int i = 0; i < mainCanvasPanels.Count; i++) { mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.VariableManagerPath(FileConfigModel.VarConfigPath); } } else { MessageLog.GetInstance.Show(s); } } } Thread.Sleep(100); }), "管道消息控制"); pipeServer.PushCommand = new Action((o) => { msg.Enqueue(o); }); pipeServer.StartPipeStream(); } /// /// 通过项目启动文件加载的数据初始化 /// private void ProjectStartInit() { if (Json.Data.ProjectName != null && Json.Data.ProjectName.Length > 0) { ProjectNameChange?.Invoke(Json.Data.ProjectName); FileConfigModel.ConstPath = Json.Data.ProjectPath; DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath); } if (Json.Data.Pages?.Count > 0) { for (int i = 0; i < Json.Data.Pages.Count; i++) { Pages.Add(Json.Data.Pages.ElementAt(i).Key); } } } /// /// 保存配置 /// private void Save() { string path = Json.Data.ProjectPath; string name = Json.Data.ProjectName; var pages = Json.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value); if (pages != null) Json.Data.Pages = pages; Json.Save($"{path}\\{name}.project"); } /// /// 页面添加委托设置 /// private void PageAdd() { ActionManage.GetInstance.Register(new Action((o) => { if (Pages.Count <= 0) { Serialization.Data.StartPageName = o.ToString(); Serialization.Save($"{FileConfigModel.GetBinConfigPath}"); } Pages.Add(o.ToString()); string path = $"{ Json.Data.ProjectPath}\\{FileConfigModel.PageDirName}"; Directory.CreateDirectory(path); File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode); Json.Data.Pages.TryAdd(o.ToString(), $"{path}\\{o.ToString()}.lay"); }), "AddPage"); } /// /// 打开新建项目弹窗 /// public void OpenNewProjectWindow() { NewProjectView newProjectView = new NewProjectView(); bool? result = newProjectView.ShowDialog(); if (result != null && result == true) { if (newProjectView.Tag != null && newProjectView.Tag is NewProjectModel objModel) { string path = $"{objModel.ProjectPath}\\{objModel.ProjectName}"; Directory.CreateDirectory(path); Json.Data.ProjectPath = path; Json.Data.ProjectName = objModel.ProjectName; ProjectNameChange?.Invoke(objModel.ProjectName); FileConfigModel.ConstPath = $"{path}"; Save(); //FileHelper.GetInstance.RegisterOpenFileType(); RegisterOpenFileType(); } } } /// /// 注册使用默认应用程序打开自定义文件 /// private void RegisterOpenFileType() { try { string icoFile = System.Windows.Forms.Application.StartupPath + $"\\Images\\fyf.ico"; string DirectoryPath = $"{Json.Data.ProjectPath}\\Images"; Directory.CreateDirectory(DirectoryPath); File.Copy(icoFile, $"{DirectoryPath}\\fyf.ico"); SystemHelper.GetInstance.RegisterOpenFileType("project", "HBL", System.Windows.Forms.Application.ExecutablePath, $"{DirectoryPath}\\fyf.ico"); } catch (Exception ex) { } } /// /// 打开新建页面弹窗 /// public void OpenNewPageWindow() { NewPageView newPageView = new NewPageView(); newPageView.ShowDialog(); } /// /// 打开变量管理器工具 /// public void OpenVariableManagerTool() { Json.Data.ProjectPath = Json.Data.ProjectPath; FileConfigModel.ConstPath = Json.Data.ProjectPath; Json.Save(); if (File.Exists(FileConfigModel.VarManagerPath)) { Process[] pro = Process.GetProcesses(); if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.VariableManager")) == null) { Process.Start(FileConfigModel.VarManagerPath); } } } /// /// 选中页面 /// /// public void SelectedPage(object o) { string path = $"{Json.Data.ProjectPath}\\{FileConfigModel.PageDirName}\\{o.ToString()}.lay"; if (mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString()) == null) { mainCanvasPanels.Add(new MainCanvasPageModel() { MainCanvasPanelModel = new MainCanvasPanel(path), PageName = o.ToString(), }); } int index = Array.FindIndex(mainCanvasPanels.ToArray(), p => p.PageName == o.ToString()); if (index >= 0 && index < mainCanvasPanels.Count) { mainCanvasPanels.ElementAt(index).MainCanvasPanelModel.VariableManagerPath(FileConfigModel.VarConfigPath); PageChange?.Invoke(mainCanvasPanels.ElementAt(index).MainCanvasPanelModel); } } /// /// 删除指定界面 /// /// public void RemovePageCommand(object o) { MessageLog.GetInstance.Show($"准备删除页面:{o.ToString()}"); if (!string.IsNullOrEmpty(o?.ToString())) { if (Pages.FirstOrDefault(p => p == o.ToString()) != null) { Pages.Remove(o.ToString()); if (Json.Data.Pages.ContainsKey(o.ToString())) Json.Data.Pages.Remove(o.ToString()); string path = $"{FileConfigModel.ConstPath}\\{FileConfigModel.PageDirName}\\{o.ToString()}.lay"; if (File.Exists(path)) File.Delete(path); if (!File.Exists(path)) MessageLog.GetInstance.Show($"页面 {o.ToString()} 删除成功"); else MessageLog.GetInstance.Show($"页面 {o.ToString()} 删除失败"); } } } /// /// 设置界面为启动界面 /// /// public void SetStartPageCommand(object o) { if (!string.IsNullOrEmpty(o?.ToString())) { Serialization.Data.StartPageName = o.ToString(); Serialization.Save($"{FileConfigModel.GetBinConfigPath}"); } MessageLog.GetInstance.Show($"当前启动页面:{Serialization.Data.StartPageName}"); } /// /// 重命名界面名称 /// /// public void ReNameCommand(object o) { if (!string.IsNullOrEmpty(o?.ToString())) { MessageLog.GetInstance.Show($"页面 {o.ToString()}重命名"); } } /// /// 项目保存 /// public void ProjectSave() { for (int i = 0; i < mainCanvasPanels.Count; i++) { mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.FileSave(); } Save(); MessageLog.GetInstance.Show("项目保存成功"); } /// /// 运行项目 /// /// public async Task RunProjectAsync() { MessageLog.GetInstance.Show("启动中...."); await ApplicationBaleAsync(); string publishPath = $"{FileConfigModel.ConstPath}\\Publish\\{Json.Data.ProjectName}\\"; string name = $" {publishPath}{ Json.Data.ProjectName}.exe"; if (File.Exists(name)) { Process[] pro = Process.GetProcesses(); if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.PageLoad")) == null) { Process.Start(name); } } MessageLog.GetInstance.Show("启动完成"); } /// /// 打开项目 /// public void OpenProject() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "项目|*.project;"; if (ofd.ShowDialog() == DialogResult.OK) { Json.Read(ofd.FileName); var pages = Json.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value); ProjectStartInit(); } } /// /// 应用程序打包程序 /// public async Task ApplicationBaleAsync() { ProjectSave(); await Task.Factory.StartNew(new Action(() => { if (string.IsNullOrEmpty(FileConfigModel.ConstPath)) { MessageLog.GetInstance.Show("打包路径不合法,打包失败"); return; } string publishPath = $"{FileConfigModel.ConstPath}\\Publish"; if (Directory.Exists(publishPath)) { MessageLog.GetInstance.Show("清理旧数据"); Directory.Delete(publishPath, true); } MessageLog.GetInstance.Show("开始打包"); string path = $"{FileConfigModel.ConstPath}\\Publish\\{Json.Data.ProjectName}"; Directory.CreateDirectory(path); MessageLog.GetInstance.Show($"打包路径:{path}"); //打包组件 string dllPath = AppDomain.CurrentDomain.BaseDirectory; if (Directory.Exists(dllPath)) CopyFolder(dllPath, $"{path}", new string[] { FileConfigModel.ConfigDirName, "Videos" }); //打包配置文件 string ConfigFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.ConfigDirName}"; if (Directory.Exists(ConfigFilePath)) CopyFolder(ConfigFilePath, $"{path}\\{FileConfigModel.ConfigDirName}"); //打包页面文件 string pageFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.PageDirName}"; if (Directory.Exists(pageFilePath)) CopyFolder(pageFilePath, $"{path}\\{FileConfigModel.PageDirName}"); //打包图片文件资源 string imageFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.ImageDirName}"; if (Directory.Exists(imageFilePath)) CopyFolder(imageFilePath, $"{path}\\{FileConfigModel.ImageDirName}"); MessageLog.GetInstance.Show("打包完成"); })); } /// /// 复制文件夹及文件 /// /// 原文件路径 /// 目标文件路径 /// private int CopyFolder(string sourceFolder, string destFolder, string[] FilterFolders = null) { try { Directory.CreateDirectory(destFolder); //得到原文件根目录下的所有文件 string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); if (name.Contains("BPASmart.PageLoad.exe")) { name = $"{Json.Data.ProjectName}.exe"; } string dest = Path.Combine(destFolder, name); File.Copy(file, dest);//复制文件 } //AccessFile,Videos //得到原文件根目录下的所有文件夹 var folders = Directory.GetDirectories(sourceFolder)?.ToList(); FilterFolders?.ToList()?.ForEach(item => { int index = Array.FindIndex(folders.ToArray(), p => p.Contains(item)); if (index >= 0 && index < folders.Count) { folders.RemoveAt(index); } }); foreach (string folder in folders) { //if (FilterFolders!=null && FilterFolders.ToList()?.FirstOrDefault(p=>p.Contains())) string name = Path.GetFileName(folder); string dest = Path.Combine(destFolder, name); CopyFolder(folder, dest);//构建目标路径,递归复制文件 } return 1; } catch (Exception e) { return -1; } } /// /// 创建快捷方式 /// /// private bool CreateShortcut(string name, string path) { string deskTop = string.Empty; try { if (System.IO.File.Exists($"{path}{name}.lnk")) return true; IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = shell.CreateShortcut($"{path}{name}.lnk") as IWshRuntimeLibrary.IWshShortcut; shortcut.TargetPath = path; //目标文件 //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。 shortcut.WorkingDirectory = System.Environment.CurrentDirectory; shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】 shortcut.Description = name; //描述 //shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标 shortcut.Arguments = ""; shortcut.Save(); //必须调用保存快捷才成创建成功 return true; } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); return false; } } } }