Пример #1
0
        public FlashUploadCommandVM(IRUSDevice device, IFlashDumpDataParserFactory parserFactory, IFlashDumpSaver flashDumpSaver, IFlashDumpLoader dumpLoader, BusyObject busy)
        {
            _device         = device ?? throw new ArgumentNullException(nameof(device));
            _parserFactory  = parserFactory;
            _flashDumpSaver = flashDumpSaver;
            IsBusy          = busy ?? throw new ArgumentNullException(nameof(busy));

            SaveDump = new ActionCommand(saveDumpAsync, () => _dump != null && IsBusy.IsNotBusy, IsBusy);

            async Task saveDumpAsync()
            {
                try
                {
                    var path = IOUtils.RequestFileSavingPath("BIN(*.bin)|*.bin");
                    if (path != null)
                    {
                        using (var targetDumpFile = File.Create(path))
                        {
                            await _dump.SaveDumpAsync(targetDumpFile, _flashDumpSaver, new AsyncOperationInfo());
                        }

                        await dumpLoader.LoadAsync(path, new AsyncOperationInfo());

                        UserInteracting.ReportSuccess("Сохранение дампа Flash-памяти", "Файл успешно сохранен");
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogErrorEverywhere("Ошибка сохранения дампа Flash-памяти", ex);
                }
            }
        }
Пример #2
0
        public LasExportMainVM(IList <IPointsRow> rows, IEnumerable <ICurveInfo> curveInfos)
        {
            _rows      = rows ?? throw new ArgumentNullException(nameof(rows));
            CurveInfos = curveInfos?.Select(ci => new CurveInfo(ci.Title, ci.IsShown)).ToArray() ?? throw new ArgumentNullException(nameof(curveInfos));

            SelectAll   = new ActionCommand(selectAll, () => !_cancellationManager.HasTasks, _cancellationManager);
            UnselectAll = new ActionCommand(unselectAll, () => !_cancellationManager.HasTasks, _cancellationManager);
            Export      = new ActionCommand(exportAsync, () => !_cancellationManager.HasTasks, _cancellationManager);
            Cancel      = new ActionCommand(TryCancelExportAsync, () => _cancellationManager.HasTasks, _cancellationManager);

            void selectAll()
            {
                foreach (var ci in CurveInfos)
                {
                    ci.IsShown = true;
                }
            }

            void unselectAll()
            {
                foreach (var ci in CurveInfos)
                {
                    ci.IsShown = false;
                }
            }

            async Task exportAsync()
            {
                var file = tryRequestFile();

                if (file.Stream != null)
                {
                    using (file.Stream)
                    {
                        try
                        {
                            ExportProgress.Reset();

                            var exportingCurvesIndexes = CurveInfos.FindAllIndexes(ci => ci.IsShown).ToArray();
                            var curvesNames            = CurveInfos.Get(exportingCurvesIndexes).Select(ci => ci.Title).ToArray();
                            var filteredRows           = _rows.GetRange(0, _rows.Count)
                                                         .Select(r => r.Points.Get(exportingCurvesIndexes).ToArray());
                            var task = new LasExporter()
                                       .SaveToAsync(file.Stream, curvesNames, filteredRows, _rows.Count, new AsyncOperationInfo(ExportProgress, _cancellationManager.Token));
                            await _cancellationManager.RegisterAndReturn(task);

                            UserInteracting.ReportSuccess("Экспорт в Las", "Экспорт успешно завершен");
                        }
                        catch (OperationCanceledException)
                        {
                            file.Stream.Dispose();
                            CommonUtils.Try(() => File.Delete(file.Path));

                            ExportProgress.Stage           = "Операция прервана";
                            ExportProgress.OperationStatus = OperationStatus.ABORTED;
                        }
                        catch (Exception ex)
                        {
                            Logger.LogErrorEverywhere("Ошибка экспорта в Las", ex);
                            Reporter.ReportError("Не удалось произвести экспорт в Las", ex);
                            ExportProgress.Stage           = "Ошибка экспорта";
                            ExportProgress.OperationStatus = OperationStatus.FAILED;

                            UserInteracting.ReportError("Экспорт в Las", "Неизвестная ошибка экспорта");

                            await TryCancelExportAsync();
                        }
                        finally
                        {
                            _cancellationManager.Reset();
                        }
                    }
                }

                (Stream Stream, string Path) tryRequestFile()
                {
                    var filePath = IOUtils.RequestFileSavingPath("LAS (*las)|*las");

                    return(CommonUtils.TryOrDefault(() =>
                    {
                        if (!Path.HasExtension(filePath))
                        {
                            filePath += ".las";
                        }
                        File.Delete(filePath);
                        return File.OpenWrite(filePath);
                    }), filePath);
                }
            }
        }