private void CheckSystems(Func <bool> cancelRequested, Action <int, string> reportProgress)  // ASYNC process, done via start up, must not be too slow.
        {
            reportProgress(-1, "");

            string   rwsystime = SQLiteConnectionSystem.GetSettingString("EDSMLastSystems", "2000-01-01 00:00:00"); // Latest time from RW file.
            DateTime edsmdate;

            if (!DateTime.TryParse(rwsystime, CultureInfo.InvariantCulture, DateTimeStyles.None, out edsmdate))
            {
                edsmdate = new DateTime(2000, 1, 1);
            }

            if (DateTime.Now.Subtract(edsmdate).TotalDays > 7)  // Over 7 days do a sync from EDSM
            {
                // Also update galactic mapping from EDSM
                LogLine("Get galactic mapping from EDSM.");
                galacticMapping.DownloadFromEDSM();

                // Skip EDSM full update if update has been performed in last 4 days
                bool     outoforder = SQLiteConnectionSystem.GetSettingBool("EDSMSystemsOutOfOrder", true);
                DateTime lastmod    = outoforder ? SystemClassDB.GetLastSystemModifiedTime() : SystemClassDB.GetLastSystemModifiedTimeFast();

                if (DateTime.UtcNow.Subtract(lastmod).TotalDays > 4 ||
                    DateTime.UtcNow.Subtract(edsmdate).TotalDays > 28)
                {
                    syncstate.performedsmsync = true;
                }
                else
                {
                    SQLiteConnectionSystem.PutSettingString("EDSMLastSystems", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                }
            }

            if (!cancelRequested())
            {
                SQLiteConnectionUser.TranferVisitedSystemstoJournalTableIfRequired();
                SQLiteConnectionSystem.CreateSystemsTableIndexes();
                SystemNoteClass.GetAllSystemNotes();                    // fill up memory with notes, bookmarks, galactic mapping
                BookmarkClass.GetAllBookmarks();
                galacticMapping.ParseData();                            // at this point, EDSM data is loaded..
                SystemClassDB.AddToAutoComplete(galacticMapping.GetGMONames());

                LogLine("Loaded Notes, Bookmarks and Galactic mapping.");

                string   timestr = SQLiteConnectionSystem.GetSettingString("EDDBSystemsTime", "0");
                DateTime time    = new DateTime(Convert.ToInt64(timestr), DateTimeKind.Utc);
                if (DateTime.UtcNow.Subtract(time).TotalDays > 6.5)     // Get EDDB data once every week.
                {
                    syncstate.performeddbsync = true;
                }
            }
        }
Esempio n. 2
0
        public static void PerformSync(Func <bool> cancelRequested, Action <int, string> reportProgress, Action <string> logLine, Action <string> logError, SystemsSyncState state)           // big check.. done in a thread.
        {
            reportProgress(-1, "");

            state.performhistoryrefresh = false;
            state.syncwasfirstrun       = SystemClassDB.IsSystemsTableEmpty();           // remember if DB is empty

            // Force a full sync if newest data is more than 14 days old

            bool     outoforder = SQLiteConnectionSystem.GetSettingBool("EDSMSystemsOutOfOrder", true);
            DateTime lastmod    = outoforder ? SystemClassDB.GetLastSystemModifiedTime() : SystemClassDB.GetLastSystemModifiedTimeFast();

            if (DateTime.UtcNow.Subtract(lastmod).TotalDays >= 14)
            {
                state.performedsmsync = true;
            }

            bool edsmoreddbsync = state.performedsmsync || state.performeddbsync;           // remember if we are syncing

            state.syncwaseddboredsm = edsmoreddbsync;

            if (state.performedsmsync || state.performeddbsync)
            {
                if (state.performedsmsync && !cancelRequested())
                {
                    // Download new systems
                    try
                    {
                        state.performhistoryrefresh |= PerformEDSMFullSync(cancelRequested, reportProgress, logLine, logError);
                        state.performedsmsync        = false;
                    }
                    catch (Exception ex)
                    {
                        logError("GetAllEDSMSystems exception:" + ex.Message);
                    }
                }

                if (!cancelRequested())
                {
                    logLine("Indexing systems table");
                    SQLiteConnectionSystem.CreateSystemsTableIndexes();

                    try
                    {
                        EliteDangerousCore.EDDB.SystemClassEDDB.PerformEDDBFullSync(cancelRequested, reportProgress, logLine, logError);
                        state.performeddbsync = false;
                    }
                    catch (Exception ex)
                    {
                        logError("GetEDDBUpdate exception: " + ex.Message);
                    }
                    state.performhistoryrefresh = true;
                }
            }

            if (!cancelRequested())
            {
                logLine("Indexing systems table");
                SQLiteConnectionSystem.CreateSystemsTableIndexes();

                lastmod = outoforder ? SystemClassDB.GetLastSystemModifiedTime() : SystemClassDB.GetLastSystemModifiedTimeFast();
                if (DateTime.UtcNow.Subtract(lastmod).TotalHours >= 1)
                {
                    logLine("Checking for new EDSM systems (may take a few moments).");
                    EDSMClass edsm    = new EDSMClass();
                    long      updates = edsm.GetNewSystems(cancelRequested, reportProgress, logLine);
                    logLine($"EDSM updated {updates:N0} systems.");
                    state.performhistoryrefresh |= (updates > 0);
                }
            }

            reportProgress(-1, "");
        }
Esempio n. 3
0
        internal long GetNewSystems(Func <bool> cancelRequested, Action <int, string> reportProgress, Action <string> logLine)
        {
            string lstsyst;

            DateTime lstsystdate;
            // First system in EDSM is from 2015-05-01 00:39:40
            DateTime gammadate  = new DateTime(2015, 5, 1, 0, 0, 0, DateTimeKind.Utc);
            bool     outoforder = SQLiteConnectionSystem.GetSettingBool("EDSMSystemsOutOfOrder", true);

            if (SystemClassDB.IsSystemsTableEmpty())
            {
                lstsystdate = gammadate;
            }
            else
            {
                // Get the most recent modify time returned from EDSM
                DateTime lastmod = outoforder ? SystemClassDB.GetLastSystemModifiedTime() : SystemClassDB.GetLastSystemModifiedTimeFast();
                lstsystdate = lastmod - TimeSpan.FromSeconds(1);

                if (lstsystdate < gammadate)
                {
                    lstsystdate = gammadate;
                }
            }

            lstsyst = lstsystdate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

            Console.WriteLine("EDSM Check date: " + lstsyst);

            long updates = 0;

            while (lstsystdate < DateTime.UtcNow)
            {
                if (cancelRequested())
                {
                    return(updates);
                }

                DateTime enddate = lstsystdate + TimeSpan.FromHours(12);
                if (enddate > DateTime.UtcNow)
                {
                    enddate = DateTime.UtcNow;
                }

                logLine($"Downloading systems from {lstsystdate.ToLocalTime().ToString()} to {enddate.ToLocalTime().ToString()}");
                reportProgress(-1, "Requesting systems from EDSM");
                string json = null;

                try
                {
                    json = RequestSystems(lstsystdate, enddate);
                }
                catch (WebException ex)
                {
                    reportProgress(-1, $"EDSM request failed");
                    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null && ex.Response is HttpWebResponse)
                    {
                        string status = ((HttpWebResponse)ex.Response).StatusDescription;
                        logLine($"Download of EDSM systems from the server failed ({status}), will try next time program is run");
                    }
                    else
                    {
                        logLine($"Download of EDSM systems from the server failed ({ex.Status.ToString()}), will try next time program is run");
                    }
                    break;
                }
                catch (Exception ex)
                {
                    reportProgress(-1, $"EDSM request failed");
                    logLine($"Download of EDSM systems from the server failed ({ex.Message}), will try next time program is run");
                    break;
                }

                if (json == null)
                {
                    reportProgress(-1, "EDSM request failed");
                    logLine("Download of EDSM systems from the server failed (no data returned), will try next time program is run");
                    break;
                }

                updates     += SystemClassEDSM.ParseEDSMUpdateSystemsString(json, ref lstsyst, ref outoforder, false, cancelRequested, reportProgress, false);
                lstsystdate += TimeSpan.FromHours(12);
            }
            logLine($"System download complete");

            return(updates);
        }