private static StarSystem DeserializeStarSystem(string systemName, string data, ref bool needToUpdate)
        {
            if (systemName == string.Empty || data == string.Empty)
            {
                return(null);
            }

            StarSystem result = null;

            try
            {
                result = JsonConvert.DeserializeObject <StarSystem>(data);
                if (result == null)
                {
                    Logging.Info("Failed to obtain system for " + systemName + " from the SQLiteRepository");
                }
            }
            catch (Exception)
            {
                Logging.Warn("Problem reading data for star system '" + systemName + "' from database, re-obtaining from source.");
                try
                {
                    result = DataProviderService.GetSystemData(systemName);
                }
                catch (Exception ex)
                {
                    Logging.Warn("Problem obtaining data from source: " + ex);
                    result = null;
                }
            }
            return(result);
        }
        public List <StarSystem> GetOrFetchStarSystems(string[] names, bool fetchIfMissing = true, bool refreshIfOutdated = true)
        {
            if (names.Count() == 0)
            {
                return(null);
            }

            List <StarSystem> systems = Instance.GetStarSystems(names, refreshIfOutdated);

            // If a system isn't found after we've read our local database, we need to fetch it.
            List <string> fetchSystems = new List <string>();

            foreach (string name in names)
            {
                if (fetchIfMissing && systems.FirstOrDefault(s => s.systemname == name) == null)
                {
                    fetchSystems.Add(name);
                }
            }

            List <StarSystem> fetchedSystems = DataProviderService.GetSystemsData(fetchSystems.ToArray());

            if (fetchedSystems?.Count > 0)
            {
                Instance.SaveStarSystems(fetchedSystems);
                systems.AddRange(fetchedSystems);
            }

            return(systems);
        }
Exemplo n.º 3
0
 public static void RecoverStarSystemDB()
 {
     lock (editLock)
     {
         File.Delete(Constants.DATA_DIR + @"\EDDI.sqlite");
         CreateDatabase();
     }
     var updateLogs = Task.Run(() => DataProviderService.syncFromStarMapService(true));
 }
Exemplo n.º 4
0
        public StarSystem GetOrFetchStarSystem(string name, bool fetchIfMissing = true)
        {
            StarSystem system = GetStarSystem(name, fetchIfMissing);

            if (system == null)
            {
                if (fetchIfMissing)
                {
                    system = DataProviderService.GetSystemData(name, null, null, null);
                }
                if (system != null)
                {
                    insertStarSystem(system);
                }
            }
            return(system);
        }
Exemplo n.º 5
0
 public static void RecoverStarSystemDB()
 {
     using (var con = SimpleDbConnection())
     {
         try
         {
             con.Close();
             SQLiteConnection.ClearAllPools();
             File.Delete(Constants.DATA_DIR + @"\EDDI.sqlite");
         }
         catch (SQLiteException ex)
         {
             handleSqlLiteException(con, ex);
         }
     }
     CreateDatabase();
     var updateLogs = Task.Run(() => DataProviderService.syncFromStarMapService());
 }
Exemplo n.º 6
0
        public List <StarSystem> GetOrCreateStarSystems(string[] names, bool fetchIfMissing = true, bool refreshIfOutdated = true)
        {
            if (names.Count() == 0)
            {
                return(null);
            }

            // Get (and update if required) systems already in our database
            List <StarSystem> systems = Instance.GetStarSystems(names, refreshIfOutdated);

            // If a system isn't found after we've read our local database, we need to fetch it.
            List <string> fetchSystems = new List <string>();

            foreach (string name in names)
            {
                if (fetchIfMissing && systems.FirstOrDefault(s => s.name == name) == null)
                {
                    fetchSystems.Add(name);
                }
            }
            List <StarSystem> fetchedSystems = DataProviderService.GetSystemsData(fetchSystems.ToArray());

            if (fetchedSystems?.Count > 0)
            {
                Instance.insertStarSystems(fetchedSystems);
                systems.AddRange(fetchedSystems);
            }

            // Create a new system object for each name that isn't in the database and couldn't be fetched from a server
            foreach (string name in names)
            {
                if (systems?.Find(s => s?.name == name) == null)
                {
                    systems.Add(new StarSystem()
                    {
                        name = name
                    });
                }
            }

            return(systems);
        }
Exemplo n.º 7
0
        public StarSystem GetOrCreateStarSystem(string name, bool fetchIfMissing = true)
        {
            StarSystem system = GetStarSystem(name, fetchIfMissing);

            if (system == null)
            {
                if (fetchIfMissing)
                {
                    system = DataProviderService.GetSystemData(name, null, null, null);
                }
                if (system == null)
                {
                    system      = new StarSystem();
                    system.name = name;
                }

                insertStarSystem(system);
            }
            return(system);
        }
        public StarSystem GetStarSystem(string name, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }

            StarSystem result = null;

            try
            {
                bool needToUpdate = false;
                using (var con = SimpleDbConnection())
                {
                    con.Open();
                    using (var cmd = new SQLiteCommand(con))
                    {
                        cmd.CommandText = SELECT_BY_NAME_SQL;
                        cmd.Prepare();
                        cmd.Parameters.AddWithValue("@name", name);
                        using (SQLiteDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.Read())
                            {
                                result = JsonConvert.DeserializeObject <StarSystem>(rdr.GetString(2));
                                if (result == null)
                                {
                                    Logging.Info("Failed to obtain system for " + name);
                                }
                                if (result != null)
                                {
                                    if (result.visits < 1)
                                    {
                                        // Old-style system; need to update
                                        result.visits    = rdr.GetInt32(0);
                                        result.lastvisit = rdr.GetDateTime(1);
                                        needToUpdate     = true;
                                    }
                                    if (result.lastupdated == null)
                                    {
                                        result.lastupdated = rdr.GetDateTime(4);
                                    }
                                    if (result.comment == null)
                                    {
                                        if (!rdr.IsDBNull(4))
                                        {
                                            result.comment = rdr.GetString(4);
                                        }
                                    }
                                    if (refreshIfOutdated && result.lastupdated < DateTime.Now.AddDays(-7))
                                    {
                                        // Data is stale
                                        StarSystem updatedResult = DataProviderService.GetSystemData(name, null, null, null);
                                        updatedResult.visits      = result.visits;
                                        updatedResult.lastvisit   = result.lastvisit;
                                        updatedResult.lastupdated = DateTime.Now;
                                        result       = updatedResult;
                                        needToUpdate = true;
                                    }
                                }
                            }
                        }
                    }
                    con.Close();
                }
                if (needToUpdate)
                {
                    updateStarSystem(result);
                }
            }
            catch (Exception ex)
            {
                Logging.Warn("Problem obtaining data: " + ex);
            }

            // TODO if star system data is out-of-date then refresh it
            return(result);
        }
Exemplo n.º 9
0
 private StarSystemSqLiteRepository(IEdsmService edsmService)
 {
     this.edsmService    = edsmService;
     dataProviderService = new DataProviderService(edsmService);
     starSystemCache     = new StarSystemCache(30);
 }
Exemplo n.º 10
0
        public StarSystem GetStarSystem(string name, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }

            StarSystem result = null;

            try
            {
                bool needToUpdate = false;
                using (var con = SimpleDbConnection())
                {
                    con.Open();
                    using (var cmd = new SQLiteCommand(con))
                    {
                        cmd.CommandText = SELECT_BY_NAME_SQL;
                        cmd.Prepare();
                        cmd.Parameters.AddWithValue("@name", name);
                        using (SQLiteDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.Read())
                            {
                                string data = rdr.GetString(2);
                                // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                                data   = data.Replace(@"""No volcanism""", "null");
                                result = JsonConvert.DeserializeObject <StarSystem>(data);
                                if (result == null)
                                {
                                    Logging.Info("Failed to obtain system for " + name);
                                }
                                if (result != null)
                                {
                                    if (result.visits < 1)
                                    {
                                        // Old-style system; need to update
                                        result.visits    = rdr.GetInt32(0);
                                        result.lastvisit = rdr.GetDateTime(1);
                                        needToUpdate     = true;
                                    }
                                    if (result.lastupdated == null)
                                    {
                                        result.lastupdated = rdr.GetDateTime(4);
                                    }
                                    if (result.comment == null)
                                    {
                                        if (!rdr.IsDBNull(4))
                                        {
                                            result.comment = rdr.GetString(4);
                                        }
                                    }
                                    if (refreshIfOutdated && result.lastupdated < DateTime.UtcNow.AddHours(-1))
                                    {
                                        // Data is stale
                                        StarSystem updatedResult = DataProviderService.GetSystemData(name, null, null, null);
                                        updatedResult.visits      = result.visits;
                                        updatedResult.lastvisit   = result.lastvisit;
                                        updatedResult.lastupdated = DateTime.UtcNow;
                                        result       = updatedResult;
                                        needToUpdate = true;
                                    }
                                }
                            }
                        }
                    }
                }
                if (needToUpdate)
                {
                    updateStarSystem(result);
                }
            }
            catch (Exception)
            {
                Logging.Warn("Problem reading data from database, re-obtaining from source.");
                try
                {
                    result = DataProviderService.GetSystemData(name, null, null, null);
                    updateStarSystem(result);
                }
                catch (Exception ex)
                {
                    Logging.Warn("Problem obtaining data from source: " + ex);
                    result = null;
                }
            }

            return(result);
        }
Exemplo n.º 11
0
        public StarSystem GetStarSystem(string name, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }

            StarSystem result = null;

            try
            {
                bool needToUpdate = false;
                using (var con = SimpleDbConnection())
                {
                    con.Open();
                    using (var cmd = new SQLiteCommand(con))
                    {
                        cmd.CommandText = SELECT_BY_NAME_SQL;
                        cmd.Prepare();
                        cmd.Parameters.AddWithValue("@name", name);
                        using (SQLiteDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.Read())
                            {
                                string data = rdr.GetString(2);
                                // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                                data = data.Replace(@"""No volcanism""", "null");

                                try
                                {
                                    result = JsonConvert.DeserializeObject <StarSystem>(data);
                                    if (result == null)
                                    {
                                        Logging.Info("Failed to obtain system for " + name + " from the SQLiteRepository");
                                    }
                                    if (result != null)
                                    {
                                        if (result.visits < 1)
                                        {
                                            // Old-style system; need to update
                                            result.visits    = rdr.GetInt32(0);
                                            result.lastvisit = rdr.GetDateTime(1);
                                            needToUpdate     = true;
                                        }
                                        if (result.lastupdated == null)
                                        {
                                            result.lastupdated = rdr.GetDateTime(4);
                                        }
                                        if (result.comment == null)
                                        {
                                            if (!rdr.IsDBNull(4))
                                            {
                                                result.comment = rdr.GetString(4);
                                            }
                                        }
                                    }
                                    if (refreshIfOutdated && result.lastupdated < DateTime.UtcNow.AddHours(-1))
                                    {
                                        // Data is stale
                                        StarSystem updatedResult = DataProviderService.GetSystemData(name);
                                        if (updatedResult.systemAddress == null && result.systemAddress != null)
                                        {
                                            // The "updated" data might be a basic system, empty except for the name.
                                            // If so, return the old result.
                                            return(result);
                                        }
                                        else
                                        {
                                            updatedResult.visits      = result.visits;
                                            updatedResult.lastvisit   = result.lastvisit;
                                            updatedResult.lastupdated = DateTime.UtcNow;
                                            result       = updatedResult;
                                            needToUpdate = true;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    Logging.Warn("Problem reading data for star system '" + name + "' from database, re-obtaining from source. ");
                                    try
                                    {
                                        result = DataProviderService.GetSystemData(name);
                                        // Recover data unique to the local user and database
                                        IDictionary <string, object> system = Deserializtion.DeserializeData(data);
                                        system.TryGetValue("visits", out object visitVal);
                                        result.visits = (int)(long)visitVal;
                                        system.TryGetValue("comment", out object commentVal);
                                        result.comment = (string)commentVal;
                                        system.TryGetValue("lastvisit", out object lastVisitVal);
                                        result.lastvisit   = (DateTime?)lastVisitVal;
                                        result.lastupdated = DateTime.UtcNow;
                                        needToUpdate       = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        Logging.Warn("Problem obtaining data from source: " + ex);
                                        result = null;
                                    }
                                }
                            }
                        }
                    }
                }
                if (needToUpdate)
                {
                    updateStarSystem(result);
                }
            }
            catch (SQLiteException)
            {
                Logging.Warn("Problem reading data for star system '" + name + "' from database, refreshing database and re-obtaining from source.");
                RecoverStarSystemDB();
                GetStarSystem(name);
            }

            return(result);
        }
Exemplo n.º 12
0
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }
            if (names.Count() == 0)
            {
                return(null);
            }

            List <StarSystem> results         = new List <StarSystem>();
            List <string>     systemsToUpdate = new List <string>();
            List <KeyValuePair <string, string> > dataSets = Instance.ReadStarSystems(names);

            foreach (KeyValuePair <string, string> kv in dataSets)
            {
                bool       needToUpdate = false;
                StarSystem result       = null;

                if (kv.Value != null && kv.Value != "")
                {
                    string name = kv.Key;

                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    string data = kv.Value?.Replace(@"""No volcanism""", "null");

                    // Determine whether our data is stale (We won't deserialize the the entire system if it's stale)
                    IDictionary <string, object> system = Deserializtion.DeserializeData(data);
                    system.TryGetValue("comment", out object commentVal);
                    system.TryGetValue("lastupdated", out object lastUpdatedVal);
                    system.TryGetValue("systemAddress", out object systemAddressVal);
                    system.TryGetValue("EDSMID", out object edsmIdVal);

                    string   comment       = (string)commentVal ?? "";
                    DateTime?lastupdated   = (DateTime?)lastUpdatedVal;
                    long?    systemAddress = (long?)systemAddressVal;
                    long?    edsmId        = (long?)edsmIdVal;

                    if (refreshIfOutdated)
                    {
                        if (lastupdated < DateTime.UtcNow.AddHours(-1))
                        {
                            // Data is stale
                            needToUpdate = true;
                        }
                        else if (lastupdated is null)
                        {
                            // We have no record of ever updating this star system
                            needToUpdate = true;
                        }
                        else if (SCHEMA_VERSION >= 2 && (systemAddress is null || edsmId is null))
                        {
                            // Obtain data for optimized data searches starting with schema version 2
                            needToUpdate = true;
                        }
                    }

                    if (needToUpdate)
                    {
                        // We want to update this star system
                        systemsToUpdate.Add(name);
                    }
                    else
                    {
                        // Deserialize the old result
                        result = DeserializeStarSystem(name, data, ref needToUpdate);
                        if (result != null)
                        {
                            results.Add(result);
                        }
                    }
                }
            }

            if (systemsToUpdate.Count > 0)
            {
                List <StarSystem> updatedSystems = DataProviderService.GetSystemsData(systemsToUpdate.ToArray());

                // If the newly fetched star system is an empty object except (for the object name), reject it
                List <string> systemsToRevert = new List <string>();
                foreach (StarSystem starSystem in updatedSystems)
                {
                    if (starSystem.systemAddress == null)
                    {
                        systemsToRevert.Add(starSystem.systemname);
                    }
                }
                updatedSystems.RemoveAll(s => systemsToRevert.Contains(s.systemname));

                // Return old results when new results have been rejected
                foreach (string systemName in systemsToRevert)
                {
                    results.Add(GetStarSystem(systemName, false));
                }

                // Synchronize EDSM visits and comments
                updatedSystems = DataProviderService.syncFromStarMapService(updatedSystems);

                // Add our updated systems to our results
                results.AddRange(updatedSystems);

                // Save changes to our star systems
                Instance.updateStarSystems(updatedSystems);
            }

            foreach (StarSystem starSystem in results)
            {
                starSystem.lastupdated = DateTime.UtcNow;
            }
            return(results);
        }
Exemplo n.º 13
0
        public List <StarSystem> GetStarSystems(string[] names, bool refreshIfOutdated = true)
        {
            if (!File.Exists(DbFile))
            {
                return(null);
            }
            if (names.Count() == 0)
            {
                return(null);
            }

            List <StarSystem> results         = new List <StarSystem>();
            List <StarSystem> systemsToUpdate = new List <StarSystem>();
            List <KeyValuePair <string, string> > dataSets = Instance.ReadStarSystems(names);

            foreach (KeyValuePair <string, string> kv in dataSets)
            {
                bool       needToUpdate = false;
                StarSystem result       = null;

                if (kv.Value != null && kv.Value != "")
                {
                    string name = kv.Key;

                    // Old versions of the data could have a string "No volcanism" for volcanism.  If so we remove it
                    string data = ((string)kv.Value)?.Replace(@"""No volcanism""", "null");

                    // Determine whether our data is stale (We won't deserialize the the entire system if it's stale)
                    IDictionary <string, object> system = Deserializtion.DeserializeData(data);
                    system.TryGetValue("visits", out object visitVal);
                    system.TryGetValue("comment", out object commentVal);
                    system.TryGetValue("lastvisit", out object lastVisitVal);
                    system.TryGetValue("lastupdated", out object lastUpdatedVal);
                    system.TryGetValue("systemAddress", out object systemAddressVal);

                    int      visits        = (int)(long)visitVal;
                    string   comment       = (string)commentVal ?? "";
                    DateTime?lastvisit     = (DateTime?)lastVisitVal;
                    DateTime?lastupdated   = (DateTime?)lastUpdatedVal;
                    long?    systemAddress = (long?)systemAddressVal;

                    if (lastvisit == null || lastupdated == null || comment == "")
                    {
                        if (Instance.OldDataFormat(name, ref visits, comment, ref lastupdated, ref lastvisit))
                        {
                            needToUpdate = true;
                        }
                    }

                    if (refreshIfOutdated && lastupdated < DateTime.UtcNow.AddHours(-1))
                    {
                        // Data is stale
                        StarSystem updatedResult = DataProviderService.GetSystemData(name);
                        if (updatedResult.systemAddress == null && systemAddress != null)
                        {
                            // The "updated" data might be a basic system, empty except for the name. If so, return the old result.
                            updatedResult = DeserializeStarSystem(name, data, ref needToUpdate);
                        }
                        else
                        {
                            needToUpdate = true;
                        }
                        result = updatedResult;
                    }
                    else
                    {
                        // Data is fresh
                        result = DeserializeStarSystem(name, data, ref needToUpdate);
                    }

                    result.visits      = visits;
                    result.comment     = comment;
                    result.lastvisit   = lastvisit;
                    result.lastupdated = DateTime.UtcNow;

                    if (needToUpdate)
                    {
                        systemsToUpdate.Add(result);
                    }
                }
                results.Add(result);
            }
            Instance.updateStarSystems(systemsToUpdate);
            return(results);
        }