public virtual void StartSession()
        {
            bool started = _imageRepository.StartSession();

            if (!started)
            {
                return;
            }

            _imageRepository.Commit();
        }
Exemple #2
0
        //TODO передать токен для отмены ниже по стеку
        public void StartMonitoring(CancellationTokenSource tokenSource, DateTime startDate, DateTime endDate, string hashTag, Action stopService)
        {
            EventLog.WriteEntry(EventTarget, string.Format("Monitoring start with pararams: startData - {0} endDate - {1} HashTag - #{2}", startDate, endDate, hashTag), EventLogEntryType.Information);
            endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, endDate.Hour, endDate.Minute, endDate.Second);
            var thread = new Thread(() =>
            {
                var printed    = new List <string>();
                string nextUrl = null;
                while (!tokenSource.IsCancellationRequested)
                {
                    try
                    {
                        if (DateTime.Now.Ticks >= endDate.Ticks)
                        {
                            break;
                        }
                        Task.Delay(TimeSpan.FromSeconds(5)).Wait();

                        ImageResponse result = string.IsNullOrEmpty(nextUrl)
                             ? _instagramExplorer.GetImagesByHashTag(hashTag, "", tokenSource.Token).Result
                             : _instagramExplorer.GetImagesFromUrl(nextUrl, tokenSource.Token).Result;

                        nextUrl            = result.Return(x => x.NextUrl, null);
                        long?lastPhotoTime = _imageRep.GetLastPhotoTimeCurrentSession();

                        foreach (var image in result.Images)
                        {
                            if (image.CreatedTime < _startTime.Ticks || image.CreatedTime <= lastPhotoTime)
                            {
                                nextUrl = null;
                                printed.Add(image.Url);
                            }
                            if (printed.Contains(image.Url))
                            {
                                continue;
                            }
                            _imageRep.SetLastPhotoTimeCurrentSession(image.CreatedTime);
                            _imageRep.Commit();
                            printed.Add(image.Url);

                            _messageAdapter.Print(image, _printerName);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry(EventTarget, string.Format("Error process image:{0}\n{1}", ex.Message, ex.StackTrace), EventLogEntryType.Information);
                    }
                }
                EventLog.WriteEntry(EventTarget, "Monitoring stop", EventLogEntryType.Information);
                stopService();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
Exemple #3
0
        public virtual void SaveImage(ImageViewModel image)
        {
            try
            {
                Session session = _imageRepository.GetActiveSession() ?? _imageRepository.GetLastSession();

                if (session == null)
                {
                    if (_imageRepository.StartSession())
                    {
                        session = _imageRepository.GetActiveSession();
                    }
                    else
                    {
                        throw new Exception("Cannot start session");
                    }
                }

                string        baseDir = AppDomain.CurrentDomain.BaseDirectory;
                DirectoryInfo info    = !Directory.Exists(Path.Combine(baseDir, "Images"))
                ? Directory.CreateDirectory(Path.Combine(baseDir, "Images"))
                : new DirectoryInfo(Path.Combine(baseDir, "Images"));

                string currentSessionStartTime = session.StartTime.ToString("dd_MM_yyyy");
                string subDirectoryPath        = Path.Combine(info.FullName, string.Format("{0}_{1}", currentSessionStartTime, session.Id));

                DirectoryInfo subDirectory = !Directory.Exists(subDirectoryPath)
                    ? info.CreateSubdirectory(string.Format("{0}_{1}", currentSessionStartTime, session.Id))
                    : new DirectoryInfo(subDirectoryPath);

                string path = Path.Combine(subDirectory.FullName, string.Format("{0}.png", Guid.NewGuid()));

                File.WriteAllBytes(path, image.Data);

                Image imageDb = new Image()
                {
                    Session = session,
                    Name    = image.Name,
                    Path    = path
                };

                imageDb.Session = session;
                _imageRepository.AddImage(imageDb);
                _imageRepository.Commit();
            }
            catch (Exception)
            {
            }
        }
Exemple #4
0
 public async Task UpdateImage(Image updatedImage)
 {
     imgRepo.Update(updatedImage);
     await imgRepo.Commit();
 }
 public void SaveChanges()
 {
     _imageRepository.Commit();
 }
 public void SaveChanges()
 {
     _imageDataProvider.Commit();
 }