From 441af1b1ba6f0bc5e94e678029918f354838e9a5 Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期六, 04 七月 2020 18:05:55 +0800
Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M071

---
 src/Bro.Common.Device/DeviceBase/MotionCardBase.cs |  247 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 241 insertions(+), 6 deletions(-)

diff --git a/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs b/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
index 1b11a50..1cd328f 100644
--- a/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
+++ b/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
@@ -1,14 +1,18 @@
-锘縰sing Bro.Common.Helper;
+锘縰sing Autofac;
+using Bro.Common.Helper;
 using Bro.Common.Interface;
 using Bro.Common.Model;
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.ComponentModel.Design;
 using System.Drawing.Design;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Forms;
+using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.Common.Base
 {
@@ -23,12 +27,14 @@
         public abstract void ResetAlarm();
         public abstract ResponseMessage Run(IOperationConfig config);
 
-        public void ExcuteMonitorInvok(DateTime dateTime, string invokeDevice, IDevice device, IMonitorSet monitorSet)
+        public virtual void ExcuteMonitorInvoke(DateTime dateTime, string invokeDevice, IDevice device, IMonitorSet monitorSet)
         {
-            OnMonitorInvoke?.BeginInvoke(dateTime, invokeDevice, device, monitorSet, null, null);
+            OnMonitorInvoke?.BeginInvoke(dateTime, invokeDevice, device, monitorSet, OnMethodInvoked, null);
         }
 
-        public void ExcuteMonitorAlarm(DateTime dateTime, IDevice device, IWarningSet warningSet)
+        public abstract void OnMethodInvoked(IAsyncResult ar);
+
+        public virtual void ExcuteMonitorAlarm(DateTime dateTime, IDevice device, IWarningSet warningSet)
         {
             OnMonitorAlarm?.BeginInvoke(dateTime, device, warningSet, null, null);
         }
@@ -40,6 +46,10 @@
                 return InitialConfig as MotionCardInitialConfigBase;
             }
         }
+
+        public List<IOItem> MonitorValues { get; set; } = new List<IOItem>();
+
+        public abstract void WriteOutput(short cardNum, short index, IOValue value);
     }
 
     public class MotionCardInitialConfigBase : InitialConfigBase, IMonitorConfig
@@ -116,14 +126,29 @@
         public List<IMonitorSet> GetAllMonitorSet()
         {
             WarningSetCollection.ForEach(m => m.Source = this.Name);
-
             MonitorSetCollection.ForEach(m => m.SourceDevice = this.Id);
             return MonitorSetCollection;
         }
         #endregion
     }
 
+    [Device("MotionCardBase", "鏉垮崱鎿嶄綔閰嶇疆鍩虹被", EnumHelper.DeviceAttributeType.OperationConfig)]
     public class MotionCardOperationConfigBase : OperationConfigBase, IComplexDisplay
+    {
+        [Category("鏉垮崱鎿嶄綔")]
+        [Description("鏉垮崱鎿嶄綔閰嶇疆鍩虹被闆嗗悎锛屼緷娆℃墽琛�")]
+        [TypeConverter(typeof(CollectionCountConvert))]
+        [Editor(typeof(ComplexCollectionEditor<MotionCardOperationSet>), typeof(UITypeEditor))]
+        public List<MotionCardOperationSet> OperationCollection { get; set; } = new List<MotionCardOperationSet>();
+
+        public string GetDisplayText()
+        {
+            return string.Join(" ", OperationCollection.Select(u => u.GetDisplayText()));
+        }
+    }
+
+    [Device("MotionOperationSet", "杩愬姩鎿嶄綔閰嶇疆鍩虹被", EnumHelper.DeviceAttributeType.OperationConfig)]
+    public class MotionCardOperationSet :  IComplexDisplay
     {
         [Category("IO鎿嶄綔閰嶇疆")]
         [DisplayName("IO棰勬鏌ュ畾涔夐泦鍚�")]
@@ -131,6 +156,11 @@
         [TypeConverter(typeof(CollectionCountConvert))]
         [Editor(typeof(IORefrenceItemCollectionEditor), typeof(UITypeEditor))]
         public List<IORefrenceItem> PreCheckIOCollection { get; set; } = new List<IORefrenceItem>();
+
+        [Category("IO鎿嶄綔閰嶇疆")]
+        [Description("瓒呮椂璁剧疆锛屽崟浣嶏細ms")]
+        [DisplayName("IO棰勬鏌ヨ秴鏃�")]
+        public int PreCheckIOTimeout { get; set; } = 0;
 
         [Category("IO鎿嶄綔閰嶇疆")]
         [DisplayName("IO杈撳嚭瀹氫箟闆嗗悎")]
@@ -143,8 +173,73 @@
         [Description("IO纭瀹氫箟闆嗗悎")]
         [TypeConverter(typeof(CollectionCountConvert))]
         public List<IORefrenceItem> IOConfirmCollection { get; set; } = new List<IORefrenceItem>();
+        [Category("IO鎿嶄綔閰嶇疆")]
+        [Description("瓒呮椂璁剧疆锛屽崟浣嶏細ms")]
+        [DisplayName("IO纭瓒呮椂")]
+        public int IOConfirmTimeout { get; set; } = 0;
 
+        //[Category("鏉垮崱杩愬姩閰嶇疆")]
+        //[DisplayName("杩愬姩閰嶇疆闆嗗悎")]
+        //[Description("MovingOps锛氳繍鍔ㄩ厤缃泦鍚�")]
+        //[TypeConverter(typeof(CollectionCountConvert))]
+        //[Editor(typeof(ComplexCollectionEditor<MovingOption>), typeof(UITypeEditor))]
+        //public List<MovingOption> MovingOps { get; set; } = new List<MovingOption>();
         [Category("鏉垮崱杩愬姩閰嶇疆")]
+        [DisplayName("杩愬姩閰嶇疆闆嗗悎")]
+        [Description("MovingOps锛氳繍鍔ㄩ厤缃泦鍚�")]
+        [TypeConverter(typeof(ComplexObjectConvert))]
+        [Editor(typeof(PropertyObjectEditor),typeof(UITypeEditor))]
+        public MotionOperationCollection MotionOperationCollection { get; set; } = new MotionOperationCollection();
+
+
+        public string GetDisplayText()
+        {
+            string preCheck = "";
+            PreCheckIOCollection.ForEach(i =>
+            {
+                preCheck += i.GetDisplayText() + " ";
+            });
+            if (preCheck != "")
+            {
+                preCheck = " 棰勬锛�" + preCheck;
+            }
+
+            string output = "";
+            IOOutputCollection.ForEach(i =>
+            {
+                output += i.GetDisplayText() + " ";
+            });
+            if (output != "")
+            {
+                output = " 杈撳嚭锛�" + output;
+            }
+            string confirm = "";
+            IOConfirmCollection.ForEach(i =>
+            {
+                confirm += i.GetDisplayText() + " ";
+            });
+            if (confirm != "")
+            {
+                confirm = " 纭锛�" + confirm;
+            }
+
+            string moving = "";
+            //MovingOps.ForEach(i =>
+            //{
+            //    moving += i.GetDisplayText() + " ";
+            //});
+            //if (moving != "")
+            //{
+            //    moving = " 杩愬姩锛�" + moving;
+            //}
+
+            return (preCheck + output + confirm + moving).Trim();
+        }
+    }
+
+    public class MotionOperationCollection : OperationConfigBase, IComplexDisplay
+    {
+        [Category("杩愬姩閰嶇疆")]
         [DisplayName("杩愬姩閰嶇疆闆嗗悎")]
         [Description("MovingOps锛氳繍鍔ㄩ厤缃泦鍚�")]
         [TypeConverter(typeof(CollectionCountConvert))]
@@ -153,8 +248,148 @@
 
         public string GetDisplayText()
         {
-            throw new NotImplementedException();
+            return $"{string.Join(" ", MovingOps.Select(u => u.GetDisplayText()))}";
         }
     }
 
+    //[Device("MotionCardOperationConfigCollection", "鏉垮崱鎿嶄綔閰嶇疆鍩虹被闆嗗悎", EnumHelper.DeviceAttributeType.OperationConfig)]
+    //public class MotionCardOperationConfigCollection : OperationConfigBase
+    //{
+    //    [Category("澶嶄綅鎿嶄綔")]
+    //    [Description("鏉垮崱鎿嶄綔閰嶇疆鍩虹被闆嗗悎锛屼緷娆℃墽琛�")]
+    //    [TypeConverter(typeof(CollectionCountConvert))]
+    //    [Editor(typeof(ComplexCollectionEditor<MotionCardOperationConfigBase>), typeof(UITypeEditor))]
+    //    public List<MotionCardOperationConfigBase> MotionCardOpsCollection { get; set; } = new List<MotionCardOperationConfigBase>();
+    //}
+
+    public class IORefrenceItemCollectionEditor : CollectionEditor
+    {
+        protected override CollectionForm CreateCollectionForm()
+        {
+            var form = base.CreateCollectionForm();
+
+            var prop = form.GetType().GetField("propertyBrowser", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
+
+            if (prop != null)
+            {
+                if (prop.GetValue(form) is PropertyGrid grid)
+                {
+                    grid.HelpVisible = true;
+                    grid.ToolbarVisible = false;
+                }
+            }
+
+            return form;
+        }
+
+        protected override object CreateInstance(Type itemType)
+        {
+            if (itemType == typeof(IORefrenceItem))
+            {
+                IORefrenceItem item = new IORefrenceItem();
+                if ((this.Context.Instance is IOperationConfig opConfig) && !string.IsNullOrWhiteSpace(opConfig.DeviceId))
+                {
+                    using (var scope = GlobalVar.Container.BeginLifetimeScope())
+                    {
+                        List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
+
+                        var device = deviceList.FirstOrDefault(u => u.Id == opConfig.DeviceId);
+
+                        if (device != null && (device is MotionCardBase motionCard))
+                        {
+                            item.IOItemSource = (motionCard.InitialConfig as MotionCardInitialConfigBase).IODefinitionCollection;
+                        }
+                    }
+                }
+
+                return item;
+            }
+
+            return base.CreateInstance(itemType);
+        }
+
+        //protected override object[] GetItems(object editValue)
+        //{
+        //    return base.GetItems(editValue);
+        //}
+
+        //protected override object SetItems(object editValue, object[] value)
+        //{
+        //    return base.SetItems(editValue, value);
+        //}
+
+        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
+        {
+            if (context.Instance is IOperationConfig opConfig)
+            {
+                if (string.IsNullOrWhiteSpace(opConfig.DeviceId))
+                {
+                    return base.EditValue(context, provider, value);
+                }
+
+                using (var scope = GlobalVar.Container.BeginLifetimeScope())
+                {
+                    List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
+
+                    var device = deviceList.FirstOrDefault(u => u.Id == opConfig.DeviceId);
+
+                    if (device == null)
+                    {
+                        return base.EditValue(context, provider, value);
+                    }
+
+                    if (device is MotionCardBase motionCard)
+                    {
+                        if (value is List<IORefrenceItem> items)
+                        {
+                            items.ForEach(item =>
+                            {
+                                item.IOItemSource = (motionCard.InitialConfig as MotionCardInitialConfigBase).IODefinitionCollection;
+                            });
+                        }
+                    }
+                }
+            }
+
+            return base.EditValue(context, provider, value);
+        }
+
+        public IORefrenceItemCollectionEditor(Type type) : base(type)
+        {
+        }
+
+        /// <summary> 
+        /// 闄愬埗涓�娆¢�変竴涓疄渚� 
+        /// </summary> 
+        /// <returns></returns> 
+        protected override bool CanSelectMultipleInstances()
+        {
+            return false;
+        }
+
+        /// <summary> 
+        /// 鎸囧畾鍒涘缓鐨勫璞$被鍨� 
+        /// </summary> 
+        /// <returns></returns> 
+        protected override Type CreateCollectionItemType()
+        {
+            return typeof(IORefrenceItem);
+        }
+
+        protected override string GetDisplayText(object value)
+        {
+            if (value is IComplexDisplay)
+            {
+                return (value as IComplexDisplay).GetDisplayText();
+            }
+
+            return base.GetDisplayText(value);
+        }
+
+        //protected override void DestroyInstance(object instance)
+        //{
+        //    base.DestroyInstance(instance);//閲嶈锛佽嚜鍔ㄥ垹闄ょ粍浠剁殑璁捐鏃朵唬鐮侊紒 
+        //}
+    }
+
 }

--
Gitblit v1.8.0