Esempio n. 1
0
        public override void DownloadCSVFiles()
        {
            try
            {
                SortedDictionary <DateTime, List <ConsoleFileInfo> > dsm_databases = new SortedDictionary <DateTime, List <ConsoleFileInfo> >();
                IConsoleCommand console = null;

                _files.Clear();

                using (SshClient sc = new SshClient(_ci))
                {
                    //_ci.AuthenticationBanner += delegate (object sender, AuthenticationBannerEventArgs e)
                    //  {
                    //      Console.WriteLine(e.BannerMessage);
                    //      Console.WriteLine(e.Username);
                    //  };
                    //var kb = _ci.AuthenticationMethods[0] as KeyboardInteractiveAuthenticationMethod;
                    //if (kb!=null)kb.AuthenticationPrompt += delegate(object sender, AuthenticationPromptEventArgs e)
                    //{
                    //    Console.WriteLine(e.Instruction);
                    //    foreach (var p in e.Prompts)
                    //    {
                    //        Console.WriteLine(p.Request);
                    //        p.Response ="1";

                    //    }
                    //};
                    RaiseDownloadEvent(CacheStatus.FetchingDirectoryInfo);

                    sc.Connect();

                    console = GetConsole(sc);

                    List <ConsoleFileInfo> files = console.GetDirectoryContentsRecursive(sc);

                    foreach (ConsoleFileInfo fi in files)
                    {
                        string file = fi.Path;

                        if (!file.Contains("/tmp."))
                        {
                            if (file.Contains("/csv/"))
                            {
                                CSVToCategory(file);
                            }
                            else
                            {
                                if (fi.FileName.EndsWith(".db") || fi.FileName.Equals("INFO"))
                                {
                                    DateTime folder;
                                    if (ParseTimeStamp(fi, out folder))
                                    {
                                        if (dsm_databases.ContainsKey(folder) == false)
                                        {
                                            dsm_databases.Add(folder, new List <ConsoleFileInfo>());
                                        }
                                        dsm_databases[folder].Add(fi);
                                    }
                                }
                            }
                        }
                    }

                    using (ScpClient cp = new ScpClient(_ci))
                    {
                        cp.Connect();

                        RaiseDownloadEvent(CacheStatus.Downloading, _files.Count, 0);
                        int n = 0;
                        foreach (ICachedReportFile src in _files.Values)
                        {
                            if (src.Type != SynoReportType.Unknown)
                            {
                                int  attempts = 0;
                                bool result   = false;

                                while (result == false && attempts < 2)
                                {
                                    attempts++;
                                    result = DownloadFile(cp, src.Source, src.LocalFile);
                                }

                                if (result == false)
                                {
                                    cp.Disconnect();
                                    cp.Connect();
                                }
                            }

                            RaiseDownloadEvent(CacheStatus.Downloading, _files.Count, ++n);
                        }

                        cp.Disconnect();
                    }
                }

                if (KeepAnalyzerDbCount >= 0)
                {
                    List <DateTime> remove = dsm_databases.Keys.Take(dsm_databases.Count - KeepAnalyzerDbCount).ToList();
                    foreach (DateTime r in remove)
                    {
                        console.RemoveFiles(this, dsm_databases[r]);
                    }
                }
            }
            catch (SshAuthenticationException ex)
            {
                throw new SynoReportViaSSHLoginFailure("The login failed.", ex);
            }
            catch (Exception ex)
            {
                throw new SynoReportViaSSHException("An error occured while refreshing the report cache. (" + ex.Message + ")", ex);
            }
            finally
            {
                RaiseDownloadEvent(CacheStatus.Idle);
            }
        }
Esempio n. 2
0
        public override void DownloadCSVFiles()
        {
            try
            {
                SortedDictionary <DateTime, List <ConsoleFileInfo> > dsm_databases = new SortedDictionary <DateTime, List <ConsoleFileInfo> >();
                IConsoleCommand console = null;

                _files.Clear();

                using (SshClient sc = new SshClient(_ci))
                {
                    sc.HostKeyReceived += client_HostKeyReceived;

                    RaiseDownloadEvent(CacheStatus.FetchingDirectoryInfo);

                    sc.Connect();

                    console = GetConsole(sc);

                    List <ConsoleFileInfo> files = console.GetDirectoryContentsRecursive(sc, this);

                    foreach (ConsoleFileInfo fi in files)
                    {
                        string file = fi.Path;

                        if (!file.Contains("/tmp."))
                        {
                            if (file.Contains("/csv/"))
                            {
                                CSVToCategory(file);
                            }
                            else
                            {
                                if (fi.FileName.EndsWith(".db") || fi.FileName.Equals("INFO"))
                                {
                                    DateTime folder;
                                    if (ParseTimeStamp(fi, out folder))
                                    {
                                        if (dsm_databases.ContainsKey(folder) == false)
                                        {
                                            dsm_databases.Add(folder, new List <ConsoleFileInfo>());
                                        }
                                        dsm_databases[folder].Add(fi);
                                    }
                                }
                            }
                        }
                    }
                    sc.HostKeyReceived -= client_HostKeyReceived;
                }
                using (ScpClient cp = new ScpClient(_ci))
                {
                    cp.HostKeyReceived += client_HostKeyReceived;
                    cp.Connect();

                    RaiseDownloadEvent(CacheStatus.Downloading, _files.Count, 0);
                    int n = 0;
                    foreach (ICachedReportFile src in _files.Values)
                    {
                        if (src.Type != SynoReportType.Unknown)
                        {
                            int  attempts = 0;
                            bool result   = false;

                            while (result == false && attempts < 2)
                            {
                                attempts++;
                                result = DownloadFile(cp, SynoReportHome + src.Source, src.LocalFile);
                            }

                            if (result == false)
                            {
                                cp.Disconnect();
                                cp.Connect();
                            }
                        }

                        RaiseDownloadEvent(CacheStatus.Downloading, _files.Count, ++n);
                    }

                    cp.Disconnect();
                    cp.HostKeyReceived -= client_HostKeyReceived;
                }


                List <ConsoleFileInfo> removal = new List <ConsoleFileInfo>();
                if (KeepAnalyzerDbCount >= 0)
                {
                    List <DateTime> remove = dsm_databases.Keys.Take(dsm_databases.Count - KeepAnalyzerDbCount).ToList();
                    foreach (DateTime r in remove)
                    {
                        removal.AddRange(dsm_databases[r]);
                    }
                    RaiseDownloadEvent(CacheStatus.Cleanup);
                    console.RemoveFiles(this, removal);
                }
            }
            catch (SshAuthenticationException ex)
            {
                throw new SynoReportViaSSHLoginFailure("The login failed.", ex);
            }
            catch (Exception ex)
            {
                throw new SynoReportViaSSHException("An error occured while refreshing the report cache. (" + ex.Message + ")", ex);
            }
            finally
            {
                RaiseDownloadEvent(CacheStatus.Idle);
            }
        }