GetSystemData() public static method

public static GetSystemData ( string system, decimal x, decimal y, decimal z ) : StarSystem
system string
x decimal
y decimal
z decimal
return EddiDataDefinitions.StarSystem
        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);
        }
Exemplo n.º 2
0
        private StarSystem DeserializeStarSystem(string systemName, string data, ref bool needToUpdate)
        {
            if (systemName == string.Empty || data == string.Empty)
            {
                return(null);
            }

            // Check our short term star system cache for a previously deserialized star system and return that if it is available.
            if (starSystemCache.Contains(systemName))
            {
                return(starSystemCache.Get(systemName));
            }

            // Not found in memory, proceed with deserialization
            StarSystem result;

            try
            {
                result = JsonConvert.DeserializeObject <StarSystem>(data);
                if (result == null)
                {
                    Logging.Info("Failed to obtain system for " + systemName + " from the SQLiteRepository");
                    needToUpdate = true;
                }
            }
            catch (Exception)
            {
                Logging.Warn("Problem reading data for star system '" + systemName + "' from database, re-obtaining from source.");
                try
                {
                    result = dataProviderService.GetSystemData(systemName);
                    result = dataProviderService.syncFromStarMapService(new List <StarSystem> {
                        result
                    })?.FirstOrDefault();                                                                                   // Synchronize EDSM visits and comments
                    needToUpdate = true;
                }
                catch (Exception ex)
                {
                    Logging.Warn("Problem obtaining data from source: " + ex);
                    result = null;
                }
            }

            // Save the deserialized star system to our short term star system cache for reference
            if (result != null)
            {
                starSystemCache.Add(result);
            }

            return(result);
        }
Exemplo n.º 3
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.º 4
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);
        }
Exemplo n.º 5
0
        private StarSystem DeserializeStarSystem(string systemName, string data, ref bool needToUpdate)
        {
            if (systemName == string.Empty || data == string.Empty)
            {
                return(null);
            }

            StarSystem result;

            try
            {
                result = JsonConvert.DeserializeObject <StarSystem>(data);
                if (result == null)
                {
                    Logging.Info("Failed to obtain system for " + systemName + " from the SQLiteRepository");
                    needToUpdate = true;
                }
            }
            catch (Exception)
            {
                Logging.Warn("Problem reading data for star system '" + systemName + "' from database, re-obtaining from source.");
                try
                {
                    result = dataProviderService.GetSystemData(systemName);
                    result = dataProviderService.syncFromStarMapService(new List <StarSystem> {
                        result
                    })?.FirstOrDefault();                                                                                   // Synchronize EDSM visits and comments
                    needToUpdate = true;
                }
                catch (Exception ex)
                {
                    Logging.Warn("Problem obtaining data from source: " + ex);
                    result = null;
                }
            }
            return(result);
        }
        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.º 7
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.º 8
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.º 9
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);
        }