using System; using System.Collections.Generic; using System.Linq; namespace Protocol { #region Consts public enum ObjectType{ Register, Data, Clock, Profile } public enum DataType{ INT_16, INT_32, INT_64, UINT_16, UINT_32, UINT_64, BYTE, FLOAT_32, FLOAT_64, UFLOAT_32, UFLOAT_64, STRING, BYTE_STRING, ARRAY, STRUCT, TIME } #endregion public class ProtocolObject { private readonly Dictionary _attributes; private readonly string _code; private readonly string _name; public ProtocolObject(string code, string name) { if (code is null) { throw new ArgumentNullException(nameof(code)); } if (name is null) { throw new ArgumentNullException(nameof(name)); } _code = code; _name = name; _attributes = new Dictionary(); } public string Code => _code; public string Name => _name; public ObjectType Type {get;set;} public void AddAttribute(ObjectAttribute attribute) { if(attribute is null) throw new ArgumentNullException(nameof(attribute)); if(_attributes.Any(x=>x.Key == attribute.Index)) throw new InvalidOperationException("Атрибут с данным индексом присутствует в объекте"); if (attribute.Index <= 0) throw new ArgumentException("Индекс должен быть положительным числом больше 0"); _attributes.Add(attribute.Index, attribute); } public ObjectAttribute GetAttributeByIndex(int index) => _attributes[index]; } public class ObjectAttribute { public DataType Type {get;set;} public int Index {get;set;} public string Value { get; set; } = string.Empty; } }