|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- 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
- {
- /// <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();
- RegisterOpenFileType();
- }
- }
- }
-
- /// <summary>
- /// 注册使用默认应用程序打开自定义文件
- /// </summary>
- private void RegisterOpenFileType()
- {
- try
- {
- string icoFile = System.Windows.Forms.Application.StartupPath + $"\\Images\\fyf.ico";
- string DirectoryPath = $"{Json<ProjectModel>.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)
- {
-
- }
-
- }
-
- /// <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)
- {
- MessageLog.GetInstance.Show($"准备删除页面:{o.ToString()}");
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
- if (Pages.FirstOrDefault(p => p == o.ToString()) != null)
- {
- Pages.Remove(o.ToString());
- if (Json<ProjectModel>.Data.Pages.ContainsKey(o.ToString()))
- Json<ProjectModel>.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()} 删除失败");
- }
- }
- }
-
- /// <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}");
- }
- MessageLog.GetInstance.Show($"当前启动页面:{Serialization<BinConfigFile>.Data.StartPageName}");
- }
-
- /// <summary>
- /// 重命名界面名称
- /// </summary>
- /// <param name="o"></param>
- public void ReNameCommand(object o)
- {
- if (!string.IsNullOrEmpty(o?.ToString()))
- {
- MessageLog.GetInstance.Show($"页面 {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>
- /// <returns></returns>
- public async Task RunProjectAsync()
- {
- MessageLog.GetInstance.Show("启动中....");
- await ApplicationBaleAsync();
- string publishPath = $"{FileConfigModel.ConstPath}\\Publish\\{Json<ProjectModel>.Data.ProjectName}\\";
- string name = $" {publishPath}{ Json<ProjectModel>.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("启动完成");
-
- }
-
- /// <summary>
- /// 打开项目
- /// </summary>
- public void OpenProject()
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "项目|*.project;";
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- Json<ProjectModel>.Read(ofd.FileName);
- var pages = Json<ProjectModel>.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
- ProjectStartInit();
- }
- }
-
- /// <summary>
- /// 应用程序打包程序
- /// </summary>
- 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<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 imageFilePath = $"{FileConfigModel.ConstPath}\\{FileConfigModel.ImageDirName}";
- if (Directory.Exists(imageFilePath)) CopyFolder(imageFilePath, $"{path}\\{FileConfigModel.ImageDirName}");
-
- 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;
- }
- }
-
- }
- }
|