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, ""); }
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); }