using Autofac; using Bro.Common.Factory; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model.Forms; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing.Design; using System.Linq; using System.Windows.Forms; using System.Windows.Forms.Design; using static Bro.Common.Helper.EnumHelper; namespace Bro.Common.Model { public class MonitorSetBase : IMonitorSet, IComplexDisplay { [Browsable(false)] public string Id { get; set; } = Guid.NewGuid().ToString(); [Category("调用配置")] [Description("调用设备")] [DisplayName("调用设备")] [TypeConverter(typeof(DeviceSelectorConverter))] public string InvokeDevice { get; set; } = ""; [Browsable(false)] public string SourceDevice { get; set; } = ""; [Category("调用配置")] [Description("调用方法")] [DisplayName("调用方法")] [TypeConverter(typeof(ProcessMethodSelectorConverter))] public string MethodCode { get; set; } [Category("调用配置")] [Description("调用方法描述")] [DisplayName("调用方法描述")] [ReadOnly(true)] public string MethodDesc { get { using (var scope = GlobalVar.Container.BeginLifetimeScope()) { List methodList = scope.Resolve>(); return methodList.FirstOrDefault(u => u.MethodCode == MethodCode)?.MethodDesc; } } } [Category("调用配置")] [Description("调用操作配置")] [DisplayName("调用操作配置")] [TypeConverter(typeof(ComplexObjectConvert))] [Editor(typeof(IOperationConfigEditor), typeof(UITypeEditor))] public IOperationConfig OpConfig { get; set; } [Category("个性化设置")] [Description("监听调用描述")] [DisplayName("监听名称")] public string Name { get; set; } = ""; [Browsable(false)] [JsonIgnore] public ProcessResponse Response { get; set; } = new ProcessResponse(); [Browsable(false)] public string DisplayText { get { return GetDisplayText(); } } public MonitorSetBase() { } public virtual string GetDisplayText() { using (var scope = GlobalVar.Container.BeginLifetimeScope()) { List methodList = scope.Resolve>(); List deviceList = scope.Resolve>(); string displayText = string.IsNullOrWhiteSpace(Name) ? "" : Name + "_"; var device = deviceList.FirstOrDefault(u => u.Id == InvokeDevice); if (device != null) { displayText += ($"{device.Name}"); } else { displayText += "无设备"; } displayText += " "; var method = methodList.FirstOrDefault(u => u.MethodCode == MethodCode); if (method != null) { displayText += ($"{method.MethodCode}-{method.MethodDesc}"); } else { displayText += "无调用"; } var sourceDevice = deviceList.FirstOrDefault(u => u.Id == SourceDevice); if (sourceDevice != null) { displayText = sourceDevice.Name + " " + displayText; } return displayText; } } } /// /// PLC监听设置 /// public class PLCMonitorSet : MonitorSetBase { /// /// 监听地址索引 按照监听地址从0开始的索引 /// [Category("监听设置")] [Description("监听地址索引 按照监听地址从0开始的索引")] [DisplayName("触发索引")] public int TriggerIndex { get; set; } = -1; /// /// 触发值 /// [Category("监听设置")] [Description("触发值,设置为-999时变化即触发")] [DisplayName("触发值")] public int TriggerValue { get; set; } = -1; /// /// 传入数据地址的索引 按照监听地址从0开始的索引集合 /// [Category("监听设置")] [Description("传入数据地址的索引 按照监听地址从0开始的索引")] [DisplayName("传入数据索引")] [TypeConverter(typeof(SimpleCollectionConvert))] public List InputDataIndex { get; set; } = new List(); /// /// 数据地址 实际寄存器的地址,例如 40012 /// [Category("回传设置")] [Description("回传数据地址 实际PLC寄存器的地址,10进制,例如 40012")] [DisplayName("回传数据地址")] public int ReplyDataAddress { get; set; } = -1; /// /// 通知地址 实际寄存器的地址,例如 40012 /// [Category("回传设置")] [Description("通知地址 实际寄存器的地址,10进制,例如 40012")] [DisplayName("通知地址")] public int NoticeAddress { get; set; } = -1; public PLCMonitorSet() { } public override string GetDisplayText() { string desc = base.GetDisplayText(); desc += $"_{TriggerIndex}:{TriggerValue}"; return desc; } } /// /// 运动板卡IO监听配置对象 /// public class MotionCardMonitorSet : MonitorSetBase { /// /// 监听类型 /// [Category("监听设置")] [DisplayName("监听类型")] [Description("监听运动板卡 IO 类型(IN OUT)")] public IOType MonitorIOModel { get; set; } /// /// 监听地址索引 /// [Category("监听设置")] [Description("监听地址索引")] [DisplayName("触发索引")] public int TriggerIndex { get; set; } = -1; /// /// 触发值 /// [Category("监听设置")] [Description("触发值,设置为-999时变化即触发")] [DisplayName("触发值")] public int TriggerValue { get; set; } = -1; /// /// 监听回传 /// [Category("回传设置")] [DisplayName("监听回传")] [Description("监听运动板卡,并往指定的IO写入数据")] [TypeConverter(typeof(CollectionCountConvert))] [Editor(typeof(IOItem), typeof(UITypeEditor))] public List ReplyIODatas { get; set; } = new List(); } public class IOperationConfigEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (edSvc != null) { IMonitorSet ms = context.Instance as IMonitorSet; if (value == null) { string methodCode = ms.MethodCode; using (var scope = GlobalVar.Container.BeginLifetimeScope()) { List methodList = scope.Resolve>(); var method = methodList.FirstOrDefault(u => u.MethodCode == methodCode); if (method != null) { value = ConfigFactory.GetOperationConfig(method.DeviceType); } } } FrmOpConfigEdit frm = new FrmOpConfigEdit(ms.MethodCode, ms.Id, value as IOperationConfig) { StartPosition = FormStartPosition.CenterScreen }; if (frm.ShowDialog() == DialogResult.OK) { return frm.OpConfig; } else { return frm.BackupConfig; } } return base.EditValue(context, provider, value); } } }