protected override IEnumerable <byte> serialize(DataEntityFormat format, object entity)
        {
            switch (format)
            {
            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
            {
                var descriptors = (DataPacketEntityDescriptor[])entity;
                return(descriptors.Select(serialize).Flatten());

                IEnumerable <byte> serialize(DataPacketEntityDescriptor descriptor)
                {
                    var type = (byte)((descriptor.IsSigned ? (1 << 7) : 0) | descriptor.NumOfSignificantBits);

                    return(new Enumerable <byte>
                        {
                            serializeByAnotherSerializer(DataEntityFormat.ASCII_STRING, descriptor.Name),
                            serializeByAnotherSerializer(DataEntityFormat.UINT16, (ushort)descriptor.Position),
                            serializeByAnotherSerializer(DataEntityFormat.UINT8, (byte)descriptor.Length),
                            serializeByAnotherSerializer(DataEntityFormat.UINT8, type),
                        });
                }
            }

            default:
                throw new NotSupportedException();
            }
        }
Example #2
0
 void throwIfFormatNotSupported(DataEntityFormat format)
 {
     if (SupportedFormats.NotContains(format))
     {
         throw new ArgumentException($"Формат {format} не поддерживается данным сериализатором");
     }
 }
Example #3
0
        static Func <dynamic, bool> getValidator(DataEntityFormat valueFormat, FileEntityType entityType)
        {
            switch (entityType)
            {
            case FileEntityType.FORMAT_VERSION:
                return(x => ((string)x).All(char.IsLetterOrDigit));

            case FileEntityType.BURN_YEAR:
                return(x => ((string)x).All(char.IsDigit));

            case FileEntityType.BURN_MONTH:
                return(x => ((string)x).All(char.IsDigit) && x.ParseToIntInvariant() <= 12);

            case FileEntityType.BURN_DAY:
                return(x => ((string)x).All(char.IsDigit) && x.ParseToIntInvariant() <= 31);

            case FileEntityType.SERIAL_NUMBER:
                return(valueFormat.GetDefaultValidator());

            case FileEntityType.MODIFICATION:
                return(x => ((string)x).All(char.IsLetterOrDigit));

            case FileEntityType.ANOTHER:
                return(valueFormat.GetDefaultValidator());

            case FileEntityType.FILE_TYPE_POINTER:
                return(x => "TFSK".Contains(x[0]) && " _".Contains(x[1]));

            default:
                throw new NotSupportedException();
            }
        }
Example #4
0
        public static bool IsInteger(this DataEntityFormat entityFormat)
        {
            switch (entityFormat)
            {
            case DataEntityFormat.INT8:
            case DataEntityFormat.INT16:
            case DataEntityFormat.INT32:
            case DataEntityFormat.INT64:
            case DataEntityFormat.UINT8:
            case DataEntityFormat.UINT16:
            case DataEntityFormat.UINT32:
            case DataEntityFormat.UINT64:
                return(true);

            case DataEntityFormat.BOOLEAN:
            case DataEntityFormat.ASCII_STRING:
            case DataEntityFormat.BYTE_ARRAY:
            case DataEntityFormat.UINT16_ARRAY:
            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
            case DataEntityFormat.CALIBRATION_PACKET_ENTITIES_ARRAY:
                return(false);

            default:
                throw new NotSupportedException();
            }
        }
        protected override string serializeToString(DataEntityFormat format, object entity)
        {
            switch (format)
            {
            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
            {
                var entities = (DataPacketEntityDescriptor[])entity;
                var sb       = new StringBuilder();
                foreach (var arrayEntity in entities)
                {
                    sb.Append($"Name=\"{arrayEntity.Name}\" ");
                    sb.Append($"Position={arrayEntity.Position} ");
                    sb.Append($"Length={arrayEntity.Length} ");
                    sb.Append($"NumOfSignificantBits={arrayEntity.NumOfSignificantBits} ");
                    sb.Append($"IsSigned={arrayEntity.IsSigned} ");
                    sb.AppendLine();
                }

                return(sb.ToString());
            }

            default:
                throw new NotSupportedException();
            }
        }
Example #6
0
 public EntityDescriptor(
     string name,
     int position,
     EntityLength length,
     DataEntityFormat valueFormat)
     : this(name, position, length, valueFormat, valueFormat.GetDefaultValidator())
 {
 }
        protected override object deserializeFromString(DataEntityFormat format, string serialized)
        {
            //byte[] bytes;
            //var fixedString = serialized.Skip(char.IsWhiteSpace).Aggregate();
            //if (fixedString.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
            //{
            //    var bytesArray = fixedString.GroupBy(2).Select(g => g.Aggregate()).Aggregate(" ");
            //    bytes = DeserializeFromString(DataEntityFormat.BYTE_ARRAY, bytesArray).To<byte[]>();
            //}

            switch (format)
            {
            case DataEntityFormat.BOOLEAN:
                return(serialized.ParseToBoolean());

            case DataEntityFormat.UINT8:
                return(serialized.ParseToUInt8Invariant());

            case DataEntityFormat.UINT16:
                return(serialized.ParseToUInt16Invariant());

            case DataEntityFormat.UINT32:
                return(serialized.ParseToUInt32Invariant());

            case DataEntityFormat.UINT64:
                return(serialized.ParseToUInt64Invariant());

            case DataEntityFormat.INT8:
                return(serialized.ParseToInt8Invariant());

            case DataEntityFormat.INT16:
                return(serialized.ParseToInt16Invariant());

            case DataEntityFormat.INT32:
                return(serialized.ParseToInt32Invariant());

            case DataEntityFormat.INT64:
                return(serialized.ParseToInt64Invariant());

            case DataEntityFormat.ASCII_STRING:
                return(serialized);

            case DataEntityFormat.BYTE_ARRAY:
                return(serialized
                       .Split(" ")
                       .Select(sb => sb.ParseToUInt8FromHexInvariant())
                       .ToArray());

            case DataEntityFormat.UINT16_ARRAY:
                return(serialized
                       .Split(" ")
                       .Select(sb => sb.ParseToUInt16FromHexInvariant())
                       .ToArray());

            default:
                throw new NotSupportedException();
            }
        }
Example #8
0
 public FileEntityDescriptor(
     string name,
     int position,
     EntityLength length,
     DataEntityFormat valueFormat,
     object defaultValue)
     : this(name, position, length, valueFormat, valueFormat.GetDefaultValidator(), defaultValue)
 {
     DefaultValue = defaultValue;
 }
        protected override object deserialize(DataEntityFormat format, IEnumerable <byte> serialized)
        {
            var fixedData = takeInCorrectOrder(serialized).ToArray();

            switch (format)
            {
            case DataEntityFormat.BOOLEAN:
                return(BitConverter.ToUInt16(fixedData, 0) == BOOL_TRUE);

            case DataEntityFormat.UINT8:
                return(fixedData.Single());

            case DataEntityFormat.UINT16:
                return(BitConverter.ToUInt16(fixedData, 0));

            case DataEntityFormat.UINT32:
                return(BitConverter.ToUInt32(fixedData, 0));

            case DataEntityFormat.UINT64:
                return(BitConverter.ToUInt64(fixedData, 0));

            case DataEntityFormat.INT8:
                return((sbyte)fixedData.Single());

            case DataEntityFormat.INT16:
                return(BitConverter.ToInt16(fixedData, 0));

            case DataEntityFormat.INT32:
                return(BitConverter.ToInt32(fixedData, 0));

            case DataEntityFormat.INT64:
                return(BitConverter.ToInt64(fixedData, 0));

            case DataEntityFormat.ASCII_STRING:
                return(deserializeToASCIIString(serialized));

            case DataEntityFormat.BYTE_ARRAY:
                return(serialized.ToArray());

            case DataEntityFormat.UINT16_ARRAY:
                return(serialized.GroupBy(2)
                       .Select(g => deserialize(DataEntityFormat.UINT16, g))
                       .ToArray());

            default:
                throw new NotSupportedException();
            }

            string deserializeToASCIIString(IEnumerable <byte> bytesSequence)
            {
                var arr = bytesSequence.ToArray();

                return(CommonUtils.TryOrDefault(() => Encoding.ASCII.GetString(arr), "?".Repeat(arr.Length)));
            }
        }
Example #10
0
 public FileEntityDescriptor(
     string name,
     int position,
     EntityLength length,
     DataEntityFormat valueFormat,
     Func <object, bool> valueRangeValidator,
     object defaultValue)
     : base(name, position, length, valueFormat, valueRangeValidator)
 {
     DefaultValue = defaultValue;
 }
        public static IEntitySerializer GetSerializer(DataEntityFormat format)
        {
            for (int i = 0; i < _serializers.Length; i++)
            {
                var serializer = _serializers[i];
                if (serializer.SupportedFormats.Contains(format))
                {
                    return(serializer);
                }
            }

            throw new NotSupportedException();
        }
Example #12
0
        internal static Func <dynamic, bool> GetDefaultValidator(this DataEntityFormat entityFormat)
        {
            switch (entityFormat)
            {
            case DataEntityFormat.BOOLEAN:
                return(x => x == true || x == false);

            case DataEntityFormat.INT8:
                return(x => x >= sbyte.MinValue && x <= sbyte.MaxValue);

            case DataEntityFormat.INT16:
                return(x => x >= short.MinValue && x <= short.MaxValue);

            case DataEntityFormat.INT32:
                return(x => x >= int.MinValue && x <= int.MaxValue);

            case DataEntityFormat.INT64:
                return(x => x >= long.MinValue && x <= long.MaxValue);

            case DataEntityFormat.UINT8:
                return(x => x >= byte.MinValue && x <= byte.MaxValue);

            case DataEntityFormat.UINT16:
                return(x => x >= ushort.MinValue && x <= ushort.MaxValue);

            case DataEntityFormat.UINT32:
                return(x => x >= uint.MinValue && x <= uint.MaxValue);

            case DataEntityFormat.UINT64:
                return(x => x >= ulong.MinValue && x <= ulong.MaxValue);

            case DataEntityFormat.BYTE_ARRAY:
                return(x => true);

            case DataEntityFormat.UINT16_ARRAY:
                return(x => true);

            case DataEntityFormat.ASCII_STRING:
                return(x => true);

            case DataEntityFormat.CALIBRATION_PACKET_ENTITIES_ARRAY:
                return(x => true);

            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
                return(x => true);

            default:
                throw new NotSupportedException();
            }
        }
        protected override IEnumerable <byte> serialize(DataEntityFormat format, object entity)
        {
            switch (format)
            {
            case DataEntityFormat.BOOLEAN:
                var encoded = ((bool)entity)
                        ? BOOL_TRUE
                        : BOOL_FALSE;
                return(serialize(DataEntityFormat.UINT16, encoded));

            case DataEntityFormat.UINT8:
                return(((byte)entity).ToSequence());

            case DataEntityFormat.UINT16:
                return(takeInCorrectOrder(BitConverter.GetBytes((ushort)entity)));

            case DataEntityFormat.UINT32:
                return(takeInCorrectOrder(BitConverter.GetBytes((uint)entity)));

            case DataEntityFormat.UINT64:
                return(takeInCorrectOrder(BitConverter.GetBytes((ulong)entity)));

            case DataEntityFormat.INT8:
                return(((byte)(sbyte)entity).ToSequence());

            case DataEntityFormat.INT16:
                return(takeInCorrectOrder(BitConverter.GetBytes((short)entity)));

            case DataEntityFormat.INT32:
                return(takeInCorrectOrder(BitConverter.GetBytes((int)entity)));;

            case DataEntityFormat.INT64:
                return(takeInCorrectOrder(BitConverter.GetBytes((long)entity)));

            case DataEntityFormat.ASCII_STRING:
                return(Encoding.ASCII.GetBytes((string)entity));

            case DataEntityFormat.BYTE_ARRAY:
                return((byte[])entity);

            case DataEntityFormat.UINT16_ARRAY:
                return(((UInt16[])entity)
                       .Select(v => serialize(DataEntityFormat.UINT16, v))
                       .Flatten()
                       .ToArray());

            default:
                throw new NotSupportedException();
            }
        }
        protected override string serializeToString(DataEntityFormat format, object entity)
        {
            switch (format)
            {
            case DataEntityFormat.BOOLEAN:
                return(((bool)entity).ToString());

            case DataEntityFormat.UINT8:
                return(((byte)entity).ToString());

            case DataEntityFormat.UINT16:
                return(((ushort)entity).ToString());

            case DataEntityFormat.UINT32:
                return(((uint)entity).ToString());

            case DataEntityFormat.UINT64:
                return(((ulong)entity).ToString());

            case DataEntityFormat.INT8:
                return(((sbyte)entity).ToString());

            case DataEntityFormat.INT16:
                return(((short)entity).ToString());

            case DataEntityFormat.INT32:
                return(((int)entity).ToString());

            case DataEntityFormat.INT64:
                return(((long)entity).ToString());

            case DataEntityFormat.ASCII_STRING:
                return((string)entity);

            case DataEntityFormat.BYTE_ARRAY:
                return(((byte[])entity)
                       .Select(b => b.ToString("X2"))
                       .Aggregate(" "));

            case DataEntityFormat.UINT16_ARRAY:
                return(((byte[])entity)
                       .Select(b => b.ToString("X4"))
                       .Aggregate(" "));

            default:
                throw new NotSupportedException();
            }
        }
Example #15
0
        public EntityDescriptor(
            string name,
            int position,
            EntityLength length,
            DataEntityFormat valueFormat,
            Func <object, bool> valueRangeValidator)
        {
            Name               = name ?? throw new ArgumentNullException(nameof(name));
            Position           = position;
            Length             = length;
            ValueFormat        = valueFormat;
            ValidateValueRange = valueRangeValidator ?? throw new ArgumentNullException(nameof(valueRangeValidator));

            var defaultValue = valueFormat.GetAttribute <DefaultEntityValueAttribute>().Value;

            DefaultEntity = new DataEntity(defaultValue, this);
        }
        protected override string serializeToString(DataEntityFormat format, object entity)
        {
            switch (format)
            {
            case DataEntityFormat.CALIBRATION_PACKET_ENTITIES_ARRAY:
            {
                var entities = (CalibrationFileEntity[])entity;
                var sb       = new StringBuilder();
                foreach (var arrayEntity in entities)
                {
                    sb.Append($"Name=\"{arrayEntity.Name}\" ");
                    sb.Append($"ItemType={(byte)arrayEntity.ItemType} ");
                    sb.Append($"DataType={(byte)arrayEntity.DataType} ");
                    sb.Append("Data=");
                    switch (arrayEntity.DataType)
                    {
                    case DataTypes.FLOAT:
                    case DataTypes.UINT16:
                    case DataTypes.INT16:
                    case DataTypes.INT8:
                        var data = ((Array)arrayEntity.Data)
                                   .ToEnumerable()
                                   .Cast <dynamic>()
                                   .Select(v => (string)NumericEx.ToStringInvariant(v))
                                   .Aggregate(" ");
                        sb.Append(data);
                        break;

                    case DataTypes.STRING_CP1251:
                        sb.Append($"\"{arrayEntity.Data}\"");
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                    sb.AppendLine();
                }

                return(sb.ToString());
            }

            default:
                throw new NotSupportedException();
            }
        }
        protected override object deserialize(DataEntityFormat format, IEnumerable <byte> serialized)
        {
            switch (format)
            {
            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
            {
                return(serialized
                       .GroupBy(8)
                       .Select(ed => ed.StartEnumeration())
                       .Select(tryDeserialize).ToArray());

                DataPacketEntityDescriptor tryDeserialize(Enumerator <byte> eDataEntityEnumerator)
                {
                    var mnemonic   = deserializeToASCIIString(eDataEntityEnumerator.Pull(4).ToArray());
                    var position   = BitConverter.ToUInt16(takeInCorrectOrder(eDataEntityEnumerator.Pull(2)).ToArray(), 0);
                    var numOfBytes = eDataEntityEnumerator.AdvanceOrThrow();

                    eDataEntityEnumerator.AdvanceOrThrow();
                    var isSigned     = (eDataEntityEnumerator.Current & 0b10000000) > 0;
                    var numOfSigBits = eDataEntityEnumerator.Current & (~0b10000000);

                    return(new DataPacketEntityDescriptor(mnemonic, numOfBytes, position, numOfSigBits, isSigned));
                }
            }

            default:
                throw new NotSupportedEnumStateException(typeof(DataEntityFormat), format);
            }

            string deserializeToASCIIString(IEnumerable <byte> bytesSequence)
            {
                var arr = bytesSequence.ToArray();

                return(CommonUtils.TryOrDefault(() => Encoding.ASCII.GetString(arr), "?".Repeat(arr.Length)));
            }
        }
Example #18
0
 protected abstract object deserializeFromString(DataEntityFormat format, string serialized);
Example #19
0
        public object Deserialize(DataEntityFormat format, IEnumerable <byte> serialized)
        {
            throwIfFormatNotSupported(format);

            return(deserialize(format, serialized));
        }
Example #20
0
        public object DeserializeFromString(DataEntityFormat format, string serialized)
        {
            throwIfFormatNotSupported(format);

            return(deserializeFromString(format, serialized));
        }
Example #21
0
        public IEnumerable <byte> Serialize(DataEntityFormat format, object entity)
        {
            throwIfFormatNotSupported(format);

            return(serialize(format, entity));
        }
Example #22
0
 public FileBaseEntityDescriptor
     (string name, int position, int length, DataEntityFormat valueFormat, FileEntityType entityType)
     : base(name, position, length, valueFormat, getValidator(valueFormat, entityType))
 {
     EntityType = entityType;
 }
Example #23
0
        public string SerializeToString(DataEntityFormat format, object entity)
        {
            throwIfFormatNotSupported(format);

            return(serializeToString(format, entity));
        }
Example #24
0
 protected abstract object deserialize(DataEntityFormat format, IEnumerable <byte> serialized);
Example #25
0
 protected object deserializeFromStringByAnotherSerializer(DataEntityFormat format, string entity)
 {
     return(EntitySerializersFactory.GetSerializer(format).DeserializeFromString(format, entity));
 }
Example #26
0
 protected string serializeToStringByAnotherSerializer(DataEntityFormat format, object entity)
 {
     return(EntitySerializersFactory.GetSerializer(format).SerializeToString(format, entity));
 }
Example #27
0
 protected IEnumerable <byte> serializeByAnotherSerializer(DataEntityFormat format, object entity)
 {
     return(EntitySerializersFactory.GetSerializer(format).Serialize(format, entity));
 }
Example #28
0
 protected abstract string serializeToString(DataEntityFormat format, object entity);
        protected override object deserializeFromString(DataEntityFormat format, string serialized)
        {
            switch (format)
            {
#warning code repetition! Incapsulate to another class!
            case DataEntityFormat.DATA_PACKET_ENTITIES_ARRAY:
            {
                var entities = serialized.Split(Global.NL);
                return(parseEntities().ToArray());

                IEnumerable <DataPacketEntityDescriptor> parseEntities()
                {
                    foreach (var entity in entities)
                    {
                        var next = entity.AsEnumerable();

                        var name                 = takeString();
                        var position             = takeNumber().ParseToUInt16Invariant();
                        var length               = takeNumber().ParseToUInt8Invariant();
                        var numOfSignificantBits = takeNumber().ParseToUInt8Invariant();
                        var isSigned             = takeName().ParseToBoolean();

                        yield return(new DataPacketEntityDescriptor(
                                         name,
                                         length,
                                         position,
                                         numOfSignificantBits,
                                         isSigned));

                        string takeString()
                        {
                            var @string = entity.Between("\"", "\"", false, false);

                            next = next.Skip(next.Find(@string).Index + @string.Length);

                            return(@string);
                        }

                        string takeNumber()
                        {
                            var @string = next
                                          .SkipWhile(c => !char.IsDigit(c))
                                          .TakeWhile(char.IsDigit)
                                          .Aggregate();

                            next = next.Skip(next.Find(@string).Index + @string.Length);

                            return(@string);
                        }

                        string takeName()
                        {
                            var @string = next
                                          .SkipWhile(c => c != '=')
                                          .Skip(1)
                                          .SkipWhile(c => !char.IsLetter(c))
                                          .TakeWhile(char.IsLetter)
                                          .Aggregate();

                            next = next.Skip(next.Find(@string).Index + @string.Length);

                            return(@string);
                        }
                    }
                }
            }

            default:
                throw new NotSupportedException();
            }
        }
Example #30
0
 protected abstract IEnumerable <byte> serialize(DataEntityFormat format, object entity);