From c866d11e0054a7299076fa53830b96610286ac2c Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期四, 02 七月 2020 20:14:40 +0800
Subject: [PATCH] 板卡操作配置添加IO定义

---
 src/Bro.Common.Model/Helper/PropertyConvertHelper.cs  |    7 +
 src/Bro.Common.Device/DeviceBase/MotionCardBase.cs    |   77 +++++++++++++++++-
 src/Bro.Common.Model/Model/IOItem.cs                  |  132 ++++++++++++++++++++++++++++++--
 src/Bro.Common.Model/Forms/FrmDeviceOpConfigEditor.cs |    2 
 src/Bro.Common.Model/Helper/EnumHelper.cs             |    4 
 5 files changed, 201 insertions(+), 21 deletions(-)

diff --git a/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs b/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
index 271562e..dce4dcd 100644
--- a/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
+++ b/src/Bro.Common.Device/DeviceBase/MotionCardBase.cs
@@ -156,7 +156,46 @@
 
         public string GetDisplayText()
         {
-            throw new NotImplementedException();
+            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();
         }
     }
 
@@ -180,10 +219,31 @@
             return form;
         }
 
-        //protected override object CreateInstance(Type itemType)
-        //{
-        //    return base.CreateInstance(itemType);
-        //}
+        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)
         //{
@@ -217,9 +277,12 @@
 
                     if (device is MotionCardBase motionCard)
                     {
-                        if (value is IORefrenceItem item)
+                        if (value is List<IORefrenceItem> items)
                         {
-                            item.IOItemSource = (motionCard.InitialConfig as MotionCardInitialConfigBase).IODefinitionCollection;
+                            items.ForEach(item =>
+                            {
+                                item.IOItemSource = (motionCard.InitialConfig as MotionCardInitialConfigBase).IODefinitionCollection;
+                            });
                         }
                     }
                 }
diff --git a/src/Bro.Common.Model/Forms/FrmDeviceOpConfigEditor.cs b/src/Bro.Common.Model/Forms/FrmDeviceOpConfigEditor.cs
index 26b8303..bb4b9a7 100644
--- a/src/Bro.Common.Model/Forms/FrmDeviceOpConfigEditor.cs
+++ b/src/Bro.Common.Model/Forms/FrmDeviceOpConfigEditor.cs
@@ -91,7 +91,7 @@
                 var attr = device.GetType().GetCustomAttribute<DeviceAttribute>();
                 if (attr != null)
                 {
-                    var opConfig= ConfigFactory.GetOperationConfig(attr.TypeCode);
+                    var opConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
                     opConfig.DeviceId = device.Id;
                     propGrid.SelectedObject = opConfig;
                 }
diff --git a/src/Bro.Common.Model/Helper/EnumHelper.cs b/src/Bro.Common.Model/Helper/EnumHelper.cs
index 07c890b..15d0610 100644
--- a/src/Bro.Common.Model/Helper/EnumHelper.cs
+++ b/src/Bro.Common.Model/Helper/EnumHelper.cs
@@ -589,9 +589,9 @@
         /// </summary>
         public enum IOType
         {
-            [Description("In杈撳叆")]
+            [Description("INPUT")]
             In = 0,
-            [Description("Out杈撳嚭")]
+            [Description("OUTPUT")]
             Out = 1
         }
     }
diff --git a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
index dd48235..3117d1e 100644
--- a/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
+++ b/src/Bro.Common.Model/Helper/PropertyConvertHelper.cs
@@ -184,6 +184,13 @@
                         }
                         return "";
                     }
+
+                    //foreach (DictionaryEntry myDE in Hash)
+                    //{
+                    //    if (myDE.Key.Equals(v))
+                    //        return myDE.Value;
+                    //}
+                    return null;
                 }
             }
             catch (Exception)
diff --git a/src/Bro.Common.Model/Model/IOItem.cs b/src/Bro.Common.Model/Model/IOItem.cs
index 486d0ff..4b59a7c 100644
--- a/src/Bro.Common.Model/Model/IOItem.cs
+++ b/src/Bro.Common.Model/Model/IOItem.cs
@@ -4,11 +4,12 @@
 using System.Collections;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Globalization;
 using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.Common.Model
 {
-    public class IOItem : IComplexDisplay
+    public class IOItem : IComplexDisplay, IEqualityComparer
     {
         /// <summary>
         /// IO鐐圭紪鍙�
@@ -31,9 +32,24 @@
         [Description("IO绫诲瀷")]
         public virtual IOType IOType { get; set; }
 
+        public new bool Equals(object x, object y)
+        {
+            if (x is IOItem a && y is IOItem b)
+            {
+                return a.IOType == b.IOType && a.IONum == b.IONum;
+            }
+
+            return false;
+        }
+
         public string GetDisplayText()
         {
-            return $"{IOType.GetEnumDescription()},缂栧彿{IONum}锛孖O鐐圭殑鍊納Value}";
+            return $"{IOType.GetEnumDescription()}-{IONum}-{Value}";
+        }
+
+        public int GetHashCode(object obj)
+        {
+            return obj.GetHashCode();
         }
     }
 
@@ -57,37 +73,131 @@
         }
     }
 
-    public class IORefrenceItem
+    public class IORefrenceItem : IComplexDisplay
     {
         [Category("IO鎿嶄綔閰嶇疆")]
         [Description("闇�瑕佹搷浣滅殑IO")]
         [TypeConverter(typeof(IORefrenceItemSourceConverter))]
-        public IOItem IOItem { get; set; } = new IOItem();
+        public IODefinition IOItem { get; set; } = new IODefinition();
 
         [Category("IO鎿嶄綔閰嶇疆")]
         [Description("闇�瑕佹搷浣滅殑IO鐨勬暟鍊�")]
-        public int CheckValue { get => IOItem.Value; set => IOItem.Value = value; }
+        public int CheckValue
+        {
+            get => IOItem.Value;
+            set => IOItem.Value = value;
+        }
 
         [Browsable(false)]
         [JsonIgnore]
         public List<IODefinition> IOItemSource { get; set; } = new List<IODefinition>();
+
+        public string GetDisplayText()
+        {
+            return IOItem.IODesc + "-" + CheckValue;
+        }
     }
 
-    public class IORefrenceItemSourceConverter : ComboBoxItemTypeConvert
+    public class IORefrenceItemSourceConverter : TypeConverter
     {
-        public override Hashtable GetConvertHash(ITypeDescriptorContext context)
+        Dictionary<IODefinition, string> itemDict = new Dictionary<IODefinition, string>();
+        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
         {
-            Hashtable table = new Hashtable();
-
             if (context.Instance is IORefrenceItem item)
             {
                 item.IOItemSource.ForEach(i =>
                 {
-                    table[i.IODesc + i.IOType.GetEnumDescription() + i.IONum] = i as IOItem;
+                    itemDict[i] = i.IODesc + "-" + i.IOType.GetEnumDescription() + "-" + i.IONum;
                 });
+
+                return new StandardValuesCollection(itemDict.Keys);
             }
 
-            return table;
+            return base.GetStandardValues(context);
+        }
+        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
+        {
+            return true;
+        }
+
+        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
+        {
+            return true;
+        }
+
+        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
+        {
+            if (sourceType == typeof(String))
+                return true;
+
+            return base.CanConvertFrom(context, sourceType);
+        }
+
+        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
+        {
+            return base.CanConvertTo(context, destinationType);
+        }
+
+        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
+        {
+            return base.CreateInstance(context, propertyValues);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="context"></param>
+        /// <param name="culture"></param>
+        /// <param name="value">string</param>
+        /// <returns></returns>
+        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
+        {
+            var s = value.ToString().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
+
+            if (s.Length == 3)
+            {
+                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
+                {
+                    if (pair.Value == value.ToString())
+                    {
+                        return pair.Key;
+                    }
+                }
+
+                IODefinition item = new IODefinition();
+                item.IODesc = s[0];
+                item.IOType = (IOType)Enum.Parse(typeof(IOType), s[1]);
+                item.IONum = int.Parse(s[2]);
+                return item;
+            }
+
+            return base.ConvertFrom(context, culture, value);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="context"></param>
+        /// <param name="culture"></param>
+        /// <param name="value">IOItem</param>
+        /// <param name="destinationType"></param>
+        /// <returns></returns>
+        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
+        {
+            if (value is IODefinition item)
+            {
+                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
+                {
+                    if (pair.Key.IOType == item.IOType && pair.Key.IONum == item.IONum)
+                    {
+                        return pair.Value;
+                    }
+                }
+
+                return item.IODesc + "-" + item.IOType.GetEnumDescription() + "-" + item.IONum;
+            }
+
+            return base.ConvertTo(context, culture, value, destinationType);
         }
     }
 }

--
Gitblit v1.8.0