Example #1
0
        private async void PrintDocument(Job job, bool manual = false)
        {
            try
            {
                var jobPrinters    = GetPrintersAsync(job);
                var printerToPrint = jobPrinters.FirstOrDefault(x => !_printingJobs.Keys.Any(p => string.Compare(x.Name, p.Name) == 0));
                if (printerToPrint == null)
                {
                    return;
                }

                _loggingService.WriteInformation($"Starting print document {job.Document.TypeTitle} on {printerToPrint.Name}");

                _printingJobs.Add(printerToPrint, job);

                job.Printer  = printerToPrint.Name;
                job.Quantity = printerToPrint.DocumentTypes
                               .Where(x => x.DocumentType == job.Document.Type)
                               .Select(x => x.Quantity).Single();
                job.State     = JobState.Printing;
                job.UpdatedOn = DateTime.Now;
                JobChangedEvent?.Invoke(job);

                await _printerService.PrintDocumentAsync(printerToPrint, job.Document, job.Quantity, (r, e) =>
                {
                    try
                    {
                        if (r)
                        {
                            _loggingService.WriteInformation($"Document {job.Document.TypeTitle} is printed on {printerToPrint.Name}");
                        }
                        else
                        {
                            Debug.WriteLine($"Error in {nameof(JobsService.PrintDocument)}: {e.ToString()}");
                            _loggingService.WriteInformation($"Printing document {job.Document.TypeTitle} on {printerToPrint.Name} is failed");
                            _loggingService.WriteError(e.ToString());
                        }

                        job.Error     = e;
                        job.State     = r ? JobState.Printed : JobState.Error;
                        job.UpdatedOn = DateTime.Now;
                        JobChangedEvent?.Invoke(job);

                        _printingJobs.Remove(printerToPrint);
                        if (!_printingJobs.Any())
                        {
                            MovePrintedJobs();
                        }
                    }
                    catch (Exception exception)
                    {
                        _loggingService.WriteError($"Error complited printing document. {exception}");
                    }
                });
            }
            catch (Exception exception)
            {
                _loggingService.WriteError($"Error printing document. {exception}");
            }
        }
Example #2
0
        private void _pusher_ReadResponse(dynamic message)
        {
            _loggingService.WriteInformation($"Starting read Pusher response: {message.ToString()}");

            var      stringMessage = message.ToString();
            Document document      = null;

            try
            {
                document = JsonConvert.DeserializeObject <Document>(stringMessage, new DocumentSizeJsonConverter());
            }
            catch (Exception ex)
            {
                _loggingService.WriteInformation($"New job is not added");

                Debug.WriteLine($"Error in Pusher: {ex.ToString()}");
                _loggingService.WriteError(ex);

                return;
            }

            if (!document.Location.HasValue || _settingsService.Settings.Locations.Any(l => l.Id == document.Location))
            {
                var newJob = new Job {
                    Document = document
                };

                var jobPrinters = GetPrintersAsync(newJob);
                if (!jobPrinters.Any())
                {
                    return;
                }

                lock (_jobCollectionGuard)
                {
                    _newJobs.Add(newJob);
                }

                JobChangedEvent?.Invoke(newJob);

                _loggingService.WriteInformation($"New job {newJob.Document.TypeTitle} is added");
            }
        }
Example #3
0
        private async void DownloadDocument(Job job)
        {
            try
            {
                var jobPrinters = GetPrintersAsync(job);
                var rotation    = jobPrinters.Any(x => x.Rotation);
                if (rotation)
                {
                    job.Document.FileUri = new Uri($"{job.Document.FileUri}&orientation=portrait");
                }

                _loggingService.WriteInformation($"Starting download document {job.Document.TypeTitle} from {job.Document.FileUri}");

                Interlocked.Increment(ref _downloadingJobCount);
                job.State     = JobState.Processing;
                job.UpdatedOn = DateTime.Now;
                JobChangedEvent?.Invoke(job);

                var localFilePath = $"Documents/{Guid.NewGuid()}.pdf";
                await _fileService.DownloadFileAsync(
                    job.Document.FileUri,
                    localFilePath,
                    p =>
                {
                    try
                    {
                        job.DownloadProgress = p;
                        job.State            = JobState.Downloading;
                        job.UpdatedOn        = DateTime.Now;
                        JobChangedEvent?.Invoke(job);
                    }
                    catch (Exception exception)
                    {
                        _loggingService.WriteError($"Error downloading progress changed action. {exception}");
                    }
                },
                    (r, e) =>
                {
                    try
                    {
                        if (r)
                        {
                            _loggingService.WriteInformation(
                                $"Document {job.Document.TypeTitle} is downloaded to {localFilePath}");
                        }
                        else
                        {
                            Debug.WriteLine($"Error in {nameof(JobsService.DownloadDocument)}: {e.ToString()}");
                            _loggingService.WriteInformation($"Downloading document {job.Document.TypeTitle} is failed");
                            _loggingService.WriteError(e.ToString());
                        }

                        job.Error = e;
                        job.State = r ? JobState.Downloaded : JobState.Error;
                        job.Document.LocalFilePath = r ? localFilePath : null;
                        job.UpdatedOn = DateTime.Now;
                        JobChangedEvent?.Invoke(job);

                        if (Interlocked.Decrement(ref _downloadingJobCount) <= 0)
                        {
                            MoveDownloadedJobs();
                        }
                    }
                    catch (Exception exception)
                    {
                        _loggingService.WriteError($"Error downloading complited action. {exception}");
                    }
                });
            }
            catch (Exception e)
            {
                _loggingService.WriteError($"Error downloading document. {e}");
            }
        }