Esempio n. 1
0
        /// <summary>
        /// Throws exception when the data are damaged
        /// </summary>
        /// <param name="address"></param>
        /// <param name="rawAnswer"></param>
        /// <returns></returns>
        IEnumerable <IDataEntity> getDataEntities(Command address, SalachovResponse rawAnswer, IEnumerable <EntityDescriptor> dataPacketDescriptors)
        {
            if (address == Command.DATA)
            {
                var descriptors = dataPacketDescriptors ?? getDescriptors();
                return(EntitiesDeserializer.Deserialize(rawAnswer.Body, descriptors));

                IEnumerable <EntityDescriptor> getDescriptors()
                {
                    foreach (var i in rawAnswer.Body.Count().Range())
                    {
                        var format = DataEntityFormat.UINT8;
                        yield return(new EntityDescriptor($"Byte{i}", i, 1, format, format.GetDefaultValidator()));
                    }
                }
            }
            else if (address == Command.STATUS)
            {
                return(EntitiesDeserializer.Deserialize(rawAnswer.Body, Requests.GetStatusRequestDescriptors(Id)));
            }
            else if (address.IsFileRequest())
            {
                var version     = FileDescriptorsTarget.ExtractFileVersion(rawAnswer.Body);
                var target      = new FileDescriptorsTarget(address.GetFileType(), version, rawAnswer.Request.DeviceId);
                var descriptors = Files.Descriptors.Find(kvp => kvp.Key.Equals(target));
                if (descriptors.Found)
                {
                    return(EntitiesDeserializer.Deserialize(rawAnswer.Body, descriptors.Value.Value.Descriptors));
                }
                else
                {
                    Logger.LogError(null, $"Не было найдено подходящих дескрипторов полей. Файл с версией \"{version}\" не поддерживается.");
                    throw new NotSupportedException();
                }
            }
            else
            {
                var description = Requests.GetRequestDescription(Id, rawAnswer.Request.Address);

                return(EntitiesDeserializer.Deserialize(rawAnswer.Body, description.Descriptors));
            }
        }
        public async Task <IEnumerable <IDataEntity> > GenerateAsync(IEnumerable <CalibrationFileEntity> coefficients)
        {
            var result = await _device.ReadAsync(_calibrationFileType.GetInfo().RequestAddress, DeviceOperationScope.DEFAULT, CancellationToken.None);

            if (result.Status != ReadStatus.OK)
            {
                throw new InvalidOperationException("Не удалось запросить файл калибровки для определения серийного номера и модификации прибора.");
            }
            var serialNumber = Files.GetFileEntity(result.Entities, FileEntityType.SERIAL_NUMBER);
            var modification = Files.GetFileEntity(result.Entities, FileEntityType.MODIFICATION);

            return(generateFile());

            IEnumerable <IDataEntity> generateFile()
            {
                var fileInfo = new FileDescriptorsTarget(_calibrationFileType, _fileVersion, _device.Id);

                foreach (var descriptor in Files.Descriptors[fileInfo].Descriptors)
                {
                    var entity = descriptor.FileDefaultDataEntity;
                    if (descriptor.ValueFormat == DataEntityFormat.CALIBRATION_PACKET_ENTITIES_ARRAY)
                    {
                        entity = descriptor.InstantiateEntity(descriptor.Serialize(coefficients));
                    }

                    if (entity.Descriptor.Name == serialNumber.Descriptor.Name)
                    {
                        yield return(serialNumber);
                    }
                    else if (entity.Descriptor.Name == modification.Descriptor.Name)
                    {
                        yield return(modification);
                    }
                    else
                    {
                        yield return(entity);
                    }
                }
            }
        }