| | |
| | | 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点编号 |
| | |
| | | [Description("IO类型")] |
| | | public virtual IOType IOType { get; set; } |
| | | |
| | | public string GetDisplayText() |
| | | public new bool Equals(object x, object y) |
| | | { |
| | | return $"{IOType.GetEnumDescription()},编号{IONum},IO点的值{Value}"; |
| | | if (x is IOItem a && y is IOItem b) |
| | | { |
| | | return a.IOType == b.IOType && a.IONum == b.IONum; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | public virtual string GetDisplayText() |
| | | { |
| | | return $"{IOType.GetEnumDescription()}-{IONum}-{Value}"; |
| | | } |
| | | |
| | | public int GetHashCode(object obj) |
| | | { |
| | | return obj.GetHashCode(); |
| | | } |
| | | } |
| | | |
| | | public class IODefinition : IOItem |
| | | { |
| | | [Category("IO配置")] |
| | | [Description("IO预定义")] |
| | | [TypeConverter(typeof(EnumDescriptionConverter<IOPrestatement>))] |
| | | public IOPrestatement IOPreStatement { get; set; } = IOPrestatement.Customized; |
| | | |
| | | [Category("IO配置")] |
| | | [Description("IO用途描述")] |
| | | public string IODesc { get; set; } |
| | |
| | | [JsonIgnore] |
| | | public override int Value { get; set; } |
| | | |
| | | public new string GetDisplayText() |
| | | public override string GetDisplayText() |
| | | { |
| | | return $"{IODesc} {IOType.GetEnumDescription()} {IONum}"; |
| | | return $"{(IOPreStatement == IOPrestatement.Customized ? IODesc : IOPreStatement.GetEnumDescription())} {IOType.GetEnumDescription()} {IONum}"; |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | } |