Example #1
0
        public virtual async Task <ReadResult> ReadAsync(Command requestAddress, DeviceOperationScope scope, AsyncOperationInfo cancellation)
        {
            Logger.LogInfo(null, $"Выполнение запроса чтения по адресу {requestAddress} для устройства \"{Name}\"...");
            var        request   = SalachovRequest.CreateReadRequest(Id, requestAddress, scope);
            ReadResult result    = null;
            var        rawResult = await _pipe.RequestAsync(request, scope, cancellation);

            if (rawResult.Status == RequestStatus.OK)
            {
                var entities = CommonUtils.TryOrDefault
                                   (() => getDataEntities(requestAddress, rawResult.To <SalachovResponse>(), _dataPacketDescriptors).ToArray(), null, out Exception ex);
                if (entities == null)
                {
                    Logger.LogError($"Не удалось разобрать данные в ответе", $"Произошла ошибка при десериализации полей ответа", ex);
                    result = new ReadResult(
                        ReadStatus.DESERIALIZATION_FAILURE,
                        null,
                        rawResult.Data,
                        rawResult.DataSections);
                }
                else
                {
                    result = new ReadResult(
                        mapRStatusToRRStatus(rawResult.Status),
                        entities,
                        rawResult.Data,
                        rawResult.DataSections);
                    if (requestAddress == Command.DATA_PACKET_CONFIGURATION_FILE)
                    {
                        _dataPacketDescriptors = EntitiesDeserializer.ExtractDataPacketDescriptors(result.Entities);
                    }
                }
            }
            else
            {
                Logger.LogError($"Не удалось выполнить запрос. Код ошибки: {rawResult.Status}", $"Произошла ошибка {rawResult.Status}. {rawResult.Status.GetEnumValueDescription()}");
                result = new ReadResult(
                    mapRStatusToRRStatus(rawResult.Status),
                    null,
                    rawResult.Data,
                    rawResult.DataSections);
            }

            return(result);
        }
Example #2
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));
            }
        }