Пример #1
0
        private async void ButtonSaveToDB_Click(object sender, EventArgs e)
        {
            FooterStatusProgressBar.Value = 0;
            var _cancellationTokenSource = new CancellationTokenSource();

            _cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(TimeOutSeconds));
            try
            {
                ButtonSaveToDB.Enabled = false;
                FooterStatusLabel.Text = _labels.GetLabel("InitSavingToDb");
                await _ic.SaveChangesAsync(_cancellationTokenSource.Token);

                FooterStatusLabel.Text        = _labels.GetLabel("SavedToDbSuccessfuly");
                FooterStatusProgressBar.Value = FooterStatusProgressBar.Maximum;
            }
            catch (OperationCanceledException)
            {
                FooterStatusLabel.Text = _labels.GetLabel("ExportCancelledByTimeout");
            }
            catch (DbUpdateException tie)
            {
                MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                {
                    exception = tie.InnerException, Level = ExceptionLevel.Warn
                });
            }
            finally
            {
                ButtonSaveToDB.Enabled = true;
                _cancellationTokenSource.Dispose();
            }
        }
Пример #2
0
        private async void MenuItemMenuSpectraAll_Click(object sender, EventArgs e)
        {
            if (_folderDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            _cts = new CancellationTokenSource();

            FooterStatusLabel.Text        = $"{_labels.GetLabel("InitAllSpectraDownloading")}";
            FooterStatusProgressBar.Value = 0;
            ButtonCancel.Enabled          = true;
            try
            {
                _cts = new CancellationTokenSource();
                var sliSpectras  = SpectraTools.SLISetSpectrasAsync(SetKey, _cts.Token).WithCancellation(_cts.Token);
                var lli1Spectras = SpectraTools.LLISetSpectrasAsync(SetKey, IrradiationType.LLI1, _cts.Token).WithCancellation(_cts.Token);
                var lli2Spectras = SpectraTools.LLISetSpectrasAsync(SetKey, IrradiationType.LLI2, _cts.Token).WithCancellation(_cts.Token);

                var taskSli = Task.Run(async() =>
                {
                    await foreach (var ssi in sliSpectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, "SLI", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                var taskLLI1 = Task.Run(async() =>
                {
                    await foreach (var ssi in lli1Spectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[IrradiationType.LLI1], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                var taskLLI2 = Task.Run(async() =>
                {
                    await foreach (var ssi in lli2Spectras.WithCancellation(_cts.Token))
                    {
                        if (_cts == null)
                        {
                            _cts = new CancellationTokenSource();
                        }
                        await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[IrradiationType.LLI2], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);
                    }
                });

                await Task.WhenAll(taskSli, taskLLI1, taskLLI2);

                FooterStatusLabel.Text        = $"{_labels.GetLabel("AllSpectraDownldCompl")}";
                FooterStatusProgressBar.Value = FooterStatusProgressBar.Maximum;
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show(_labels.GetLabel("SpectraNotFound"), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (OperationCanceledException ose)
            {
                FooterStatusProgressBar.Value = 0;
                FooterStatusLabel.Text        = _labels.GetLabel("CancelOperation");
            }
            catch (AggregateException ae)
            {
                foreach (var ie in ae.InnerExceptions)
                {
                    MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                    {
                        exception = ie, Level = ExceptionLevel.Error
                    });
                }
            }
            finally
            {
                ButtonCancel.Enabled = false;
                _cts.Dispose();
                _cts = null;
            }
        }
Пример #3
0
        private async void MenuItemMenuSpectraLLI_Click(object sender, EventArgs e)
        {
            if (_folderDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var objName = (sender as ToolStripMenuItem)?.Name;

            var type = objName.Contains("LLI1") ? IrradiationType.LLI1 : IrradiationType.LLI2;

            _cts = new CancellationTokenSource();
            ButtonCancel.Enabled = true;
            try
            {
                FooterStatusProgressBar.Value   = 0;
                FooterStatusProgressBar.Maximum = 1;
                var i = 0;

                await foreach (var ssi in SpectraTools.LLISetSpectrasAsync(SetKey, type, _cts.Token).WithCancellation(_cts.Token))
                {
                    if (_cts == null)
                    {
                        _cts = new CancellationTokenSource();
                    }
                    i++;
                    if (i % 2 == 0)
                    {
                        FooterStatusProgressBar.Value = 0;
                    }

                    FooterStatusLabel.Text = $"{_labels.GetLabel("DownloadingFile")}{ssi.SampleSpectra}";
                    await WebDavClientApi.DownloadFile(ssi.token, Path.Combine(_folderDialog.SelectedPath, SetKey, SpectraTools.IrradiationTypeMap[type], ssi.LoadNumber.ToString(), $"c-{ssi.Container}", ssi.SampleType, $"{ssi.SampleSpectra}.cnf"), _cts.Token);

                    FooterStatusProgressBar.Value = 1;
                }

                if (i == 0)
                {
                    throw new IndexOutOfRangeException();
                }

                FooterStatusLabel.Text = $"{_labels.GetLabel("DownloadingHasDone")}{i}";
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show(_labels.GetLabel("SpectraNotFound"), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (OperationCanceledException ose)
            {
                try
                {
                    FooterStatusProgressBar.Value = 0;
                    FooterStatusLabel.Text        = _labels.GetLabel("CancelOperation");
                }
                catch (NullReferenceException)
                { }
            }
            catch (AggregateException ae)
            {
                foreach (var ie in ae.InnerExceptions)
                {
                    MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                    {
                        exception = ie, Level = ExceptionLevel.Error
                    });
                }
            }
            finally
            {
                ButtonCancel.Enabled = false;
                _cts?.Dispose();
                _cts = null;
            }
        }
Пример #4
0
        private async void ExportFromGoogleButton_Click(object sender, EventArgs e)
        {
            FooterStatusProgressBar.Value = 0;
            FooterStatusLabel.Text        = _labels.GetLabel("InitialExport");

            if (Data.Any())
            {
                var res = MessageBox.Show(_labels.GetLabel("DeleteFromDB"), _labels.GetLabel("HelpCaptionPromt"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (res == DialogResult.No)
                {
                    return;
                }
                _ic.Samples.RemoveRange(Data);
            }

            Data.Clear();
            var result = "";

            using (Prompt prompt = new Prompt(_labels.GetLabel("HelpMessagePromt"), _labels.GetLabel("HelpCaptionPromt")))
            {
                result = prompt.Result;
            }

            if (string.IsNullOrEmpty(result))
            {
                FooterStatusLabel.Text = "";
                return;
            }

            var _cancellationTokenSource = new CancellationTokenSource();

            _cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(TimeOutSeconds));

            try
            {
                var samples = await ExportData.FromGoogleSheet <Sample>(result, _cancellationTokenSource.Token);

                FooterStatusProgressBar.Maximum = samples.Length;
                foreach (var l in samples)
                {
                    if (string.IsNullOrEmpty(l.A_Client_Sample_ID))
                    {
                        break;
                    }
                    l.F_Country_Code     = SetKey.Split('-')[0];
                    l.F_Client_Id        = SetKey.Split('-')[1];
                    l.F_Year             = SetKey.Split('-')[2];
                    l.F_Sample_Set_Id    = SetKey.Split('-')[3];
                    l.F_Sample_Set_Index = SetKey.Split('-')[4];
                    l.A_Sample_ID        = (Array.IndexOf(samples, l) + 1).ToString("d2");
                    Data.Add(l);
                    FooterStatusProgressBar.Value++;
                }
                FooterStatusProgressBar.Value = FooterStatusProgressBar.Maximum;
                FooterStatusLabel.Text        = _labels.GetLabel("SuccessExport");
                await _ic.Samples.AddRangeAsync(Data);

                //await _ic.SaveChangesAsync();
            }
            catch (NullReferenceException nre)
            {
                MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                {
                    exception = nre, Level = ExceptionLevel.Error
                });
            }
            catch (OperationCanceledException)
            {
                FooterStatusLabel.Text = _labels.GetLabel("ExportCancelledByTimeout");
            }
            catch (AggregateException ae)
            {
                foreach (var ie in ae.InnerExceptions)
                {
                    MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                    {
                        exception = ie, Level = ExceptionLevel.Error
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBoxTemplates.WrapExceptionToMessageBox(new ExceptionEventsArgs()
                {
                    exception = ex, Level = ExceptionLevel.Error
                });
            }
            finally
            {
                _cancellationTokenSource.Dispose();
            }
        }