Exemple #1
0
        private void DownloadThreadMethod(object state)
        {
            long   _count     = 0;
            string _localPath = string.Empty;
            string _url       = string.Empty;

            Thread.Sleep(3000);

            while (true)
            {
                ImageItem _item = null;

                Thread.Sleep(100);

                if ((ThumbnailItems.Count == 0) && (PhotoItems.Count == 0))
                {
                    Thread.Sleep(1000);
                    continue;
                }

                if ((_count++ % 3) != 2)
                {
                    if (ThumbnailItems.Count > 0)
                    {
                        _item = ThumbnailItems[0];
                    }
                }
                else
                {
                    if (PhotoItems.Count > 0)
                    {
                        _item = PhotoItems[0];
                    }
                }

                if (_item == null)
                {
                    continue;
                }

                switch (_item.PostItemType)
                {
                case PostItemType.Thumbnail:
                    _url       = _item.ThumbnailPath.Replace("[IP]", WService.HostIP);
                    _localPath = _item.LocalFilePath_Origin;
                    break;

                case PostItemType.Origin:
                    _url       = Main.Current.RT.StationMode ? _item.OriginPath : _item.CloudOriginPath;
                    _localPath = _item.LocalFilePath_Origin;
                    break;

                case PostItemType.Medium:
                    _url       = _item.MediumPath.Replace("[IP]", WService.HostIP);
                    _localPath = _item.LocalFilePath_Medium;
                    break;
                }

                if (File.Exists(_localPath))
                {
                    lock (PhotoItems)
                    {
                        if (PhotoItems.Contains(_item))
                        {
                            PhotoItems.Remove(_item);
                        }
                    }

                    continue;
                }

                bool _relpaceOriginToMedium = false;

                if (_item.PostItemType == PostItemType.Origin)
                {
                    if (File.Exists(_item.LocalFilePath_Medium))
                    {
                        if (!canGetOrigin(_item))
                        {
                            lock (PhotoItems)
                            {
                                PhotoItems.Remove(_item);

                                if (PhotoItems.Count == 0)
                                {
                                    PhotoItems.Insert(0, _item);
                                }
                                else
                                {
                                    PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                }
                            }

                            continue;
                        }
                    }
                    else
                    {
                        _url       = _item.MediumPath.Replace("[IP]", WService.HostIP);
                        _localPath = _item.LocalFilePath_Medium;

                        _relpaceOriginToMedium = true;
                    }
                }

                try
                {
                    m_currentURL = _url;

                    WebRequest _wReq = WebRequest.Create(_url);
                    _wReq.Timeout = 10000;

                    WebResponse _wRep = _wReq.GetResponse();

                    Image _img = Image.FromStream(_wRep.GetResponseStream());

                    if (!File.Exists(_localPath))
                    {
                        _img.Save(_localPath);
                    }

                    _img = null;

                    s_logger.Trace("GetFile:" + _localPath);

                    if (_item.PostItemType == PostItemType.Thumbnail)
                    {
                        lock (ThumbnailItems)
                        {
                            if (ThumbnailItems.Contains(_item))
                            {
                                ThumbnailItems.Remove(_item);
                            }
                        }

                        if (ThumbnailEvent != null)
                        {
                            ThumbnailEvent(_item);
                        }
                    }
                    else
                    {
                        lock (PhotoItems)
                        {
                            if (_relpaceOriginToMedium)
                            {
                                PhotoItems.Remove(_item);

                                if (PhotoItems.Count == 0)
                                {
                                    PhotoItems.Insert(0, _item);
                                }
                                else
                                {
                                    PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                }
                            }
                            else
                            {
                                if (PhotoItems.Contains(_item))
                                {
                                    PhotoItems.Remove(_item);
                                }
                            }
                        }

                        if (PhotoEvent != null)
                        {
                            PhotoEvent(_item);
                        }
                    }
                }
                catch (Exception _e)
                {
                    NLogUtility.Exception_Warn(s_logger, _e, "Download Image URL", m_currentURL);

                    try
                    {
                        _item.ErrorTry++;

                        if (_item.PostItemType == PostItemType.Thumbnail)
                        {
                            lock (ThumbnailItems)
                            {
                                if (ThumbnailItems.Contains(_item))
                                {
                                    ThumbnailItems.Remove(_item);

                                    if (_item.ErrorTry != ERROR_TRY)
                                    {
                                        if (ThumbnailItems.Count == 0)
                                        {
                                            ThumbnailItems.Insert(0, _item);
                                        }
                                        else
                                        {
                                            ThumbnailItems.Insert(ThumbnailItems.Count - 1, _item);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            lock (PhotoItems)
                            {
                                if (_item.PostItemType == PostItemType.Origin)
                                {
                                    m_downlaodErrorOriginFiles.Add(_url, DateTime.Now);
                                }

                                if (PhotoItems.Contains(_item))
                                {
                                    PhotoItems.Remove(_item);

                                    if (_item.ErrorTry != ERROR_TRY)
                                    {
                                        if (PhotoItems.Count == 0)
                                        {
                                            PhotoItems.Insert(0, _item);
                                        }
                                        else
                                        {
                                            PhotoItems.Insert(PhotoItems.Count - 1, _item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
        private void ThreadMethod(object state)
        {
            while (true)
            {
                try
                {
                    if (Main.Current.RT.Login == null)
                    {
                        NotifyState(ConnectServiceStateType.NetworkDisconnected);

                        Delay(2);
                        continue;
                    }

                    if (Main.Current.RT.StationMode)
                    {
                        if (Main.Current.RT.REST.CheckStationAlive(WService.StationIP, m_lastTimeout))
                        {
                            Delay(10);
                            continue;
                        }
                        else
                        {
                            WService.StationIP          = string.Empty;
                            Main.Current.RT.StationMode = false;

                            NotifyState(ConnectServiceStateType.Cloud);

                            //RefreshDownloadPhotosCache();

                            s_logger.Info("Station Disappear");
                        }
                    }
                    else
                    {
                        NotifyState(ConnectServiceStateType.Cloud);

                        MR_users_findMyStation _findMyStation = Main.Current.RT.REST.Users_findMyStation();

                        s_logger.Trace("Call FindMyStation");

                        if (_findMyStation != null)
                        {
                            if (_findMyStation.stations != null)
                            {
                                //Test Local IP
                                string _ip = GetStationIP(_findMyStation.stations, false);

                                if (!string.IsNullOrEmpty(_ip))
                                {
                                    m_lastTimeout = 3000;

                                    if (Main.Current.RT.REST.CheckStationAlive(_ip, m_lastTimeout))
                                    {
                                        WService.StationIP          = _ip;
                                        Main.Current.RT.StationMode = true;

                                        NotifyState(ConnectServiceStateType.Station_LocalIP);

                                        //RefreshDownloadPhotosCache();

                                        s_logger.Info("Station IP:" + _ip);

                                        Delay(5);
                                        continue;
                                    }
                                }

                                //Test UPnP
                                _ip = GetStationIP(_findMyStation.stations, true);

                                m_lastTimeout = 6000;

                                if (!string.IsNullOrEmpty(_ip))
                                {
                                    if (Main.Current.RT.REST.CheckStationAlive(_ip, m_lastTimeout))
                                    {
                                        WService.StationIP          = _ip;
                                        Main.Current.RT.StationMode = true;

                                        NotifyState(ConnectServiceStateType.Station_UPnP);

                                        //RefreshDownloadPhotosCache();

                                        s_logger.Info("Station IP(UPnP):" + _ip);

                                        Delay(10);
                                        continue;
                                    }
                                }
                            }
                        }
                    }

                    Delay(60);
                }
                catch (Exception _e)
                {
                    NLogUtility.Exception_Warn(s_logger, _e, "ThreadMethod", "");
                }
            }
        }