public ScanService() { _cancelToken = new CancellationTokenSource(); _fileService = new FileService(_cancelToken.Token); _fileService.NewData += _fileService_NewData; _pdf = CreateNewDocument(); }
protected virtual void _fileService_NewData(IMessage message) { //todo cancellation token support if (message is NoNewData) { //message arrives after max pause interval if (message.CreatedAt > _lastImageAddedAt + TimePending) { SavePdf(); _pdf = CreateNewDocument(); } return; } else if (message is Data) { var data = (Data)message; var imageIndex = GetImageIndex(Path.GetFileName(data.Location)); if (_pdf != null) { if (_cancelToken.Token.IsCancellationRequested) { SavePdf(); return; } if (_lastImageAddedAt - data.CreatedAt > TimePending) { SavePdf(); _pdf = CreateNewDocument(); } if (imageIndex != currentIndex + 1 && currentIndex != -1) { SavePdf(); _pdf = CreateNewDocument(); } } Debug.Assert(_pdf != null); _pdf.AddImage(data.Location); currentIndex = imageIndex; imageCount++; _lastImageAddedAt = DateTimeOffset.UtcNow; _fileService.Delete(new FileInfo(data.Location)); } }
protected void SavePdf() { _pdf.Save(GetNextFilename()); _pdf = null; }