|
- 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;
-
- namespace BPASmart.ConfigurationSoftware
- {
- /// <summary>
- /// 服务中心
- /// </summary>
- 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<string> msg { get; set; } = new ConcurrentQueue<string>();
-
- public ObservableCollection<string> Message { get; set; } = new ObservableCollection<string>();
-
- public List<MainCanvasPageModel> mainCanvasPanels { get; set; } = new List<MainCanvasPageModel>();
-
- public ObservableCollection<string> Pages { get; set; } = new ObservableCollection<string>();
-
- /// <summary>
- /// 页面切换委托
- /// </summary>
- public Action<FrameworkElement> PageChange { get; set; }
-
- /// <summary>
- /// 项目名称改变委托
- /// </summary>
- public Action<string> ProjectNameChange { get; set; }
-
- public void Init()
- {
- MessageLog.GetInstance.NotifyShow = new Action<string>((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
- MessageLog.GetInstance.NotifyShowEx = new Action<string>((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
- PipeMsgMonitor();
- ProjectStartInit();
- PageAdd();
- }
-
- /// <summary>
- /// 退出通知
- /// </summary>
- public void ExitNotify()
- {
- pipeServer.SendCommand(PipeTopic.ExitNotify);
- }
-
- /// <summary>
- /// 管道消息监听,和服务启动
- /// </summary>
- 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<string>((o) => { msg.Enqueue(o); });
- pipeServer.StartPipeStream();
- }
-
- /// <summary>
- /// 通过项目启动文件加载的数据初始化
- /// </summary>
- private void ProjectStartInit()
- {
- if (Json<ProjectModel>.Data.ProjectName != null && Json<ProjectModel>.Data.ProjectName.Length > 0)
- {
- ProjectNameChange?.Invoke(Json<ProjectModel>.Data.ProjectName);
- FileConfigModel.ConstPath = Json<ProjectModel>.Data.ProjectPath;
- DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath);
- }
-
- if (Json<ProjectModel>.Data.Pages?.Count > 0)
- {
- for (int i = 0; i < Json<ProjectModel>.Data.Pages.Count; i++)
- {
- Pages.Add(Json<ProjectModel>.Data.Pages.ElementAt(i).Key);
- }
- }
- }
-
- /// <summary>
- /// 保存配置
- /// </summary>
- private void Save()
- {
- string path = Json<ProjectModel>.Data.ProjectPath;
- string name = Json<ProjectModel>.Data.ProjectName;
- var pages = Json<ProjectModel>.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
- if (pages != null) Json<ProjectModel>.Data.Pages = pages;
- Json<ProjectModel>.Save($"{path}\\{name}.project");
- }
-
- /// <summary>
- /// 页面添加委托设置
- /// </summary>
- private void PageAdd()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (Pages.Count <= 0)
- {
- Serialization<BinConfigFile>.Data.StartPageName = o.ToString();
- Serialization<BinConfigFile>.Save($"{FileConfigModel.GetBinConfigPath}");
- }
- Pages.Add(o.ToString());
- string path = $"{ Json<ProjectModel>.Data.ProjectPath}\\{FileConfigModel.PageDirName}";
- Directory.CreateDirectory(path);
- File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode);
- Json<ProjectModel>.Data.Pages.TryAdd(o.ToString(), $"{path}\\{o.ToString()}.lay");
- }), "AddPage");
- }
-
-
- /// <summary>
- /// 打开新建项目弹窗
- /// </summary>
- 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<ProjectModel>.Data.ProjectPath = path;
- Json<ProjectModel>.Data.ProjectName = objModel.ProjectName;
- ProjectNameChange?.Invoke(objModel.ProjectName);
- FileConfigModel.ConstPath = $"{path}";
- Save();
- FileHelper.GetInstance.RegisterOpenFileType();
- }
- }
- }
-
- /// <summary>
- /// 打开新建页面弹窗
- /// </summary>
- public void OpenNewPageWindow()
- {
- NewPageView newPageView = new NewPageView();
- newPageView.ShowDialog();
- }
-
- /// <summary>
- /// 打开变量管理器工具
- /// </summary>
- public void OpenVariableManagerTool()
- {
- Json<LocalPar>.Data.ProjectPath = Json<ProjectModel>.Data.ProjectPath;
- FileConfigModel.ConstPath = Json<ProjectModel>.Data.ProjectPath;
- Json<LocalPar>.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);
- }
- }
- }
-
- /// <summary>
- /// 选中页面
- /// </summary>
- /// <param name="o"></param>
- public void SelectedPage(object o)
- {
- string path = $"{Json<ProjectModel>.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);
- }
- }
-
- /// <summary>
- /// 删除指定界面
- /// </summary>
- /// <param name="o"></param>
- public void RemovePageCommand(object o)
- {
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
- if (Pages.FirstOrDefault(p => p == o.ToString()) != null)
- {
- 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()} 删除失败");
- }
- }
- }
-
- /// <summary>
- /// 设置界面为启动界面
- /// </summary>
- /// <param name="o"></param>
- public void SetStartPageCommand(object o)
- {
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
- Serialization<BinConfigFile>.Data.StartPageName = o.ToString();
- Serialization<BinConfigFile>.Save($"{FileConfigModel.GetBinConfigPath}");
- }
- }
-
- /// <summary>
- /// 重命名界面名称
- /// </summary>
- /// <param name="o"></param>
- public void ReNameCommand(object o)
- {
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
-
- }
- }
-
- /// <summary>
- /// 项目保存
- /// </summary>
- public void ProjectSave()
- {
- for (int i = 0; i < mainCanvasPanels.Count; i++)
- {
- mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.FileSave();
- }
- Save();
- MessageLog.GetInstance.Show("项目保存成功");
- }
-
- /// <summary>
- /// 应用程序打包程序
- /// </summary>
- public void ApplicationBale()
- {
- if (string.IsNullOrEmpty(FileConfigModel.ConstPath))
- {
- MessageLog.GetInstance.Show("打包路径不合法,打包失败");
- return;
- }
- MessageLog.GetInstance.Show("开始打包");
- string path = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.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 createPath = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}\\";
- //CreateShortcut($"{Json<ProjectModel>.Data.ProjectName}.exe", createPath);
- MessageLog.GetInstance.Show("打包完成");
- }
-
- /// <summary>
- /// 复制文件夹及文件
- /// </summary>
- /// <param name="sourceFolder">原文件路径</param>
- /// <param name="destFolder">目标文件路径</param>
- /// <returns></returns>
- 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<ProjectModel>.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;
- }
- }
-
-
- /// <summary>
- /// 创建快捷方式
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
-
- }
- }
|