using MiniExcelLibs; using PLCSharp.Core.Common; using PLCSharp.Core.Prism; using PLCSharp.Core.Tools; using PLCSharp.Core.UserControls; using PLCSharp.VVMs.Connects; using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.Homepage; using PLCSharp.VVMs.ModeState; using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.Recipe; using PLCSharp.VVMs.Robots; using PLCSharp.VVMs.Vision; using PLCSharp.VVMs.Workflows; using Prism.Commands; using Prism.Dialogs; using Prism.Events; using Prism.Ioc; using System.Collections.ObjectModel; using System.ComponentModel; using System.Threading; using System.Windows; using System.Windows.Media; namespace PLCSharp.Models { /// /// 全局模型 /// [Model] public partial class GlobalModel : ModelBase { /// /// 全局模型 /// public GlobalModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService) { NatashaManagement //获取链式构造器 .GetInitializer() //使用引用程序集中的命名空间 .WithMemoryUsing() //使用内存中的元数据 .WithMemoryReference() //注册域构造器 .Preheating(); foreach (var item in _DatasContext.Recipes) { Recipes.Add(item.DeepCopy()); } if (Recipes.Count == 0) { var recipe = new Recipe { Name = "First", Comment = "", Prompt = "默认配方", Current = true }; Recipes.Add(recipe); RecipeSelected = recipe; ExecuteRecipeManage("Save"); CurrentRecipe = recipe; } else { CurrentRecipe = Recipes.Where(r => r.Current).FirstOrDefault(); if (CurrentRecipe == null) { CurrentRecipe = Recipes.FirstOrDefault(); CurrentRecipe.Current = true; RecipeSelected = CurrentRecipe; ExecuteRecipeManage("Save"); } RecipeSelected ??= CurrentRecipe; } VariablesModel = container.Resolve(); VariablesModel.Init(this); Connects = container.Resolve(); ControllersModel = container.Resolve(); ControllersModel.Init(this); WorkflowsModel = container.Resolve(); VisionsModel = container.Resolve(); RobotModel = container.Resolve(); VisionsModel.Init(this); RobotModel.Init(this); bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; bkgWorker.DoWork += BackgroundWork; bkgWorker.RunWorkerAsync(); //注册发送给errLog的消息 ea.GetEvent().Subscribe( ErrMessageReceived, ThreadOption.UIThread, false, (filter) => filter.Target.Contains("errLog")); //注册配方变更 ea.GetEvent().Subscribe( RecipeChanged, ThreadOption.UIThread, false, (filter) => filter.Target.Contains("RecipeChanged")); //注册退出 ea.GetEvent().Subscribe( Exit, ThreadOption.UIThread, false, (filter) => filter.Target.Contains("Exit")); } private void Exit(Message message) { if (ModeState.State.Execute) { SendInfoDialog("无法退出,请先停止运行!"); return; } MessageBoxResult result = MessageBox.Show("确认退出吗?请确保安全!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (result == MessageBoxResult.OK) { // 用户点击了“确定”按钮 MessageBoxResult resultSeconnd = MessageBox.Show("确认退出吗?即将退出控制!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (resultSeconnd == MessageBoxResult.OK) { Application.Current.Shutdown(); } } } #region 全局变量 /// /// 获取变量 /// /// 名称 /// 返回结果 public Variable GetVariable(string name) { var variable = VariablesModel.Variables.FirstOrDefault(v => v.Name == name) ?? throw new Exception("全局变量未配置!"); return variable; } #endregion #region 硬件 /// /// 连接模型 /// public ConnectsModel Connects { get; set; } /// /// Controllers模型 /// public ControllersModel ControllersModel { get; set; } /// /// 机器人模型 /// public RobotModel RobotModel { get; set; } /// /// 全局变量 /// public VariablesModel VariablesModel { get; set; } /// /// 获取连接 /// /// 名称 /// 返回结果 public Connect GetConnect(string name) { var connect = Connects.Connects.FirstOrDefault(c => c.Name == name) ?? throw new Exception("网络未配置!"); return connect; } /// /// 获取Robot /// /// 名称 /// 返回结果 public Robot GetRobot(string name) { var robot = RobotModel.Robots.FirstOrDefault(c => c.Name == name) ?? throw new Exception("Robot未配置!"); return robot; } /// /// 获取VisionFunction /// /// 名称 /// 返回结果 public VisionFunction GetVisionFunction(string name) { return VisionsModel.VisionFunctions.FirstOrDefault(v => v.Name == name) ?? throw new Exception("视觉功能未配置!"); } /// /// 获取图像数据 /// /// 名称 /// 返回结果 public ImageData GetImageData(string name) { var imageData = VisionsModel.ImageDatas.FirstOrDefault(v => v.Name == name) ?? throw new Exception("全局图像未配置!"); if (imageData.Mat == null) throw new Exception("全局图像未赋值!"); return imageData; } #endregion #region 任务 /// /// Workflows模型 /// public WorkflowsModel WorkflowsModel { get; set; } /// /// 视觉模型 /// public VisionsModel VisionsModel { get; set; } /// /// 获取Task /// /// 名称 /// 返回结果 public Workflow GetTask(string name) { return WorkflowsModel.Workflows.FirstOrDefault(t => t.Name == name); } #endregion } }