public SubprogressAcceptorRetranslator(IProgressAcceptor Parent, IProgressAcceptor Recipient, double InitialValue, double Weight)
     : base(Parent, InitialValue, Weight)
 {
     this.Recipient = Recipient;
 }
 public SubprogressAcceptor(IProgressAcceptor Parent, double InitialValue, double Weight)
 {
     this.Parent = Parent;
     _parentInitialValue = InitialValue;
     _weight = Weight;
 }
 public FudpProgressAcceptorShell(IProgressAcceptor BaseAcceptor)
 {
     this.BaseAcceptor = BaseAcceptor;
 }
        public void SynchronizeComponent(FirmwareComponent Component, PackageInformation FirmwareInformation, IProgressAcceptor GlobalProgressAcceptor = null, IProgressAcceptor LocalProgressAcceptor = null)
        {
            // Проверяем, можно ли применить программный компонент к данной прошивке
            if (!Component.Targets.Any(ct => CanApply(ct, Session.Device))) throw new CanUpdateComponentMismatchException();

            bool forceReplace = false;
            #if DEBUG
            forceReplace = true;
            #endif

            PostProgressMessage(GlobalProgressAcceptor, "Получение списка файлов");
            var filesOnDevice = Session.ListFiles();
            _logger.Debug("FILES:{0}", string.Join("", filesOnDevice.Select(f => String.Format("{3}  {0,-16} {1:N0} Bytes [0x{2:x4}]", f.FileName, f.FileSize, f.ControlSum, Environment.NewLine))));

            var filesInComponent = Component.Files;
            var filesToRemove = filesOnDevice.Where(df => filesInComponent.All(cf => cf.RelativePath != df.FileName)).ToList();

            var filesToReplace = filesInComponent.Select(componentFile =>
                                                         new
                                                         {
                                                             componentFile,
                                                             deviceFile = filesOnDevice.FirstOrDefault(f => f.FileName == componentFile.RelativePath)
                                                         })
                                                 .Where(
                                                        pair =>
                                                        pair.deviceFile == null
                                                        || forceReplace
                                                        || pair.deviceFile.ControlSum != FudpCrc.CalcCrc(pair.componentFile.Content))
                                                 .ToList();

            int transferWeight = filesToReplace.Sum(pair => pair.componentFile.Content.Length);

            try
            {
                PostProgressMessage(GlobalProgressAcceptor, "Передача прошивки {0:P0}");
                foreach (var fileToRemove in filesToRemove)
                {
                    PostProgressMessage(LocalProgressAcceptor, "Удаление файла " + fileToRemove.FileName);
                    RemoveFile(fileToRemove);
                    _logger.Info("Файл {0} удалён с устройства", fileToRemove.FileName);
                }

                double totalTransferred = 0;
                foreach (var filePairToReplace in filesToReplace)
                {
                    IProgressAcceptor subProgressAcceptor = null;
                    if (GlobalProgressAcceptor != null)
                    {
                        subProgressAcceptor =
                            new SubprogressAcceptorRetranslator(
                                GlobalProgressAcceptor,
                                LocalProgressAcceptor,
                                totalTransferred / transferWeight,
                                (double)filePairToReplace.componentFile.Content.Length / transferWeight);
                    }

                    _logger.Info(filePairToReplace.deviceFile == null ? "Создание файла {0}" : "Перезапись файла {0}", filePairToReplace.componentFile);
                    PushFile(filePairToReplace.componentFile, filePairToReplace.deviceFile != null, GlobalProgressAcceptor, subProgressAcceptor);
                    _logger.Info("Файл {0} передан на устройство", filePairToReplace.componentFile);

                    totalTransferred += filePairToReplace.componentFile.Content.Length;
                }

                PostProgressMessage(GlobalProgressAcceptor, "Обновление свойств");
                Session.SetParam(ParamKeys.MajorVersion, FirmwareInformation.FirmwareVersion.Major);
                Session.SetParam(ParamKeys.MinorVersion, FirmwareInformation.FirmwareVersion.Minor);
                Session.SetParam(ParamKeys.LastUpgrateDate, DateTimeFormatter.ToUnixTime(DateTime.Now));
                Session.SetParam(ParamKeys.GlobalChecksum, GetGlobalChecksum(Component));
                Session.SetParam(ParamKeys.VersionLabel, LabelEncoder.Default.Encode(FirmwareInformation.FirmwareVersionLabel));
                _logger.Info("Свойства загрузчика обновлены");
            }
            catch (CanProgFileopException exc)
            {
                throw new CanUpdateFileOperationException(exc);
            }
        }
        private void PushFile(FirmwareFile file, bool overwrite, IProgressAcceptor GlobalProgressAcceptor = null, IProgressAcceptor LocalProgressAcceptor = null)
        {
            if (LocalProgressAcceptor != null) LocalProgressAcceptor.OnProgressChanged(0);
            PostProgressMessage(LocalProgressAcceptor, "Файл \"" + file.RelativePath + "\" – {0:P0}");

            if (overwrite) Session.DeleteFile(file.RelativePath);
            var pushingFile = new DevFileInfo(file.RelativePath, file.Content);
            Session.CreateFile(pushingFile, new FudpProgressAcceptorShell(LocalProgressAcceptor));

            PostProgressMessage(LocalProgressAcceptor, "");
        }
 private void PostProgressMessage(IProgressAcceptor ProgressAcceptor, string Format)
 {
     var formatAcceptor = ProgressAcceptor as IMessageFormatAcceptor;
     if (formatAcceptor != null) formatAcceptor.SetMessageFormat(Format);
 }