// returns null if EDSM says not there, else if returns list of bodies and a flag indicating if from cache.
        // all this is done in a lock inside a task - the only way to sequence the code and prevent multiple lookups in an await structure
        // so we must pass back all the info we can to tell the caller what happened.
        // optional call back is allowed in lock - may not be used.
        // Verified Nov 20

        public static Tuple <List <JournalScan>, bool> GetBodiesList(ISystem sys, bool edsmweblookup = true, Action <List <JournalScan>, ISystem> onnewbodies = null)
        {
            try
            {
                lock (bodylock) // only one request at a time going, this is to prevent multiple requests for the same body
                {
                    // System.Threading.Thread.Sleep(2000); //debug - delay to show its happening
                    // System.Diagnostics.Debug.WriteLine("EDSM Cache check " + sys.EDSMID + " " + sys.SystemAddress + " " + sys.Name);

                    if (DictEDSMBodies != null && sys.EDSMID > 0 && DictEDSMBodies.ContainsKey(sys.EDSMID))  // Cache EDSM bidies during run of EDD.
                    {
                        System.Diagnostics.Debug.WriteLine("Found sys.EDSMID " + sys.EDSMID);
                        return(new Tuple <List <JournalScan>, bool>(DictEDSMBodies[sys.EDSMID], true));
                    }
                    else if (DictEDSMBodiesByID64 != null && sys.SystemAddress != null && sys.SystemAddress > 0 && DictEDSMBodiesByID64.ContainsKey(sys.SystemAddress.Value))
                    {
                        System.Diagnostics.Debug.WriteLine("Found sys.EDSMID64 " + sys.SystemAddress.Value);
                        return(new Tuple <List <JournalScan>, bool>(DictEDSMBodiesByID64[sys.SystemAddress.Value], true));
                    }

                    if (!edsmweblookup)      // must be set for a web lookup
                    {
                        return(null);
                    }

                    System.Diagnostics.Debug.WriteLine("EDSM Web lookup");
                    List <JournalScan> bodies = new List <JournalScan>();

                    EDSMClass edsm = new EDSMClass();

                    JObject jo = null;

                    if (sys.EDSMID > 0)
                    {
                        jo = edsm.GetBodies(sys.EDSMID);  // Colonia
                    }
                    else if (sys.SystemAddress != null && sys.SystemAddress > 0)
                    {
                        jo = edsm.GetBodiesByID64(sys.SystemAddress.Value);
                    }
                    else if (sys.Name != null)
                    {
                        jo = edsm.GetBodies(sys.Name);
                    }

                    if (jo != null && jo["bodies"] != null)
                    {
                        foreach (JObject edsmbody in jo["bodies"])
                        {
                            try
                            {
                                JObject jbody = EDSMClass.ConvertFromEDSMBodies(edsmbody);

                                JournalScan js = new JournalScan(jbody);//TBD, sys.EDSMID);

                                bodies.Add(js);
                            }
                            catch (Exception ex)
                            {
                                BaseUtils.HttpCom.WriteLog($"Exception Loop: {ex.Message}", "");
                                BaseUtils.HttpCom.WriteLog($"ETrace: {ex.StackTrace}", "");
                                Trace.WriteLine($"Exception Loop: {ex.Message}");
                                Trace.WriteLine($"ETrace: {ex.StackTrace}");
                            }
                        }

                        if (sys.EDSMID > 0)
                        {
                            DictEDSMBodies[sys.EDSMID] = bodies;
                        }

                        if (sys.SystemAddress != null && sys.SystemAddress > 0)
                        {
                            DictEDSMBodiesByID64[sys.SystemAddress.Value] = bodies;
                        }

                        System.Diagnostics.Debug.WriteLine("EDSM Web Lookup complete " + sys.Name + " " + bodies.Count);

                        onnewbodies?.Invoke(bodies, sys);        // inside the lock, with new bodies, allow it to do processsing.

                        return(new Tuple <List <JournalScan>, bool>(bodies, false));
                    }

                    if (sys.EDSMID > 0)
                    {
                        DictEDSMBodies[sys.EDSMID] = null;
                    }

                    if (sys.SystemAddress != null && sys.SystemAddress > 0)
                    {
                        DictEDSMBodiesByID64[sys.SystemAddress.Value] = null;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Exception: {ex.Message}");
                Trace.WriteLine($"ETrace: {ex.StackTrace}");
                return(null);
            }

            return(null);
        }
示例#2
0
        public static List <JournalScan> GetBodiesList(long edsmid, bool edsmweblookup = true, long?id64 = null, string sysname = null) // get this edsmid,  optionally lookup web protected against bad json
        {
            try
            {
                if (DictEDSMBodies != null && edsmid > 0 && DictEDSMBodies.ContainsKey(edsmid))  // Cache EDSM bidies during run of EDD.
                {
                    // System.Diagnostics.Debug.WriteLine(".. found EDSM Lookup bodies from cache " + edsmid);
                    return(DictEDSMBodies[edsmid]);
                }
                else if (DictEDSMBodiesByID64 != null && id64 != null && id64 > 0 && DictEDSMBodiesByID64.ContainsKey(id64.Value))
                {
                    return(DictEDSMBodiesByID64[id64.Value]);
                }

                if (!edsmweblookup)      // must be set for a web lookup
                {
                    return(null);
                }

                List <JournalScan> bodies = new List <JournalScan>();

                EDSMClass edsm = new EDSMClass();

                //System.Diagnostics.Debug.WriteLine("EDSM Web Lookup bodies " + edsmid);

                JObject jo = null;

                if (edsmid > 0)
                {
                    jo = edsm.GetBodies(edsmid);  // Colonia
                }
                else if (id64 != null && id64 > 0)
                {
                    jo = edsm.GetBodiesByID64(id64.Value);
                }
                else if (sysname != null)
                {
                    jo = edsm.GetBodies(sysname);
                }

                if (jo != null && jo["bodies"] != null)
                {
                    foreach (JObject edsmbody in jo["bodies"])
                    {
                        try
                        {
                            JObject     jbody = EDSMClass.ConvertFromEDSMBodies(edsmbody);
                            JournalScan js    = new JournalScan(jbody, edsmid);

                            bodies.Add(js);
                        }
                        catch (Exception ex)
                        {
                            BaseUtils.HttpCom.WriteLog($"Exception Loop: {ex.Message}", "");
                            BaseUtils.HttpCom.WriteLog($"ETrace: {ex.StackTrace}", "");
                            Trace.WriteLine($"Exception Loop: {ex.Message}");
                            Trace.WriteLine($"ETrace: {ex.StackTrace}");
                        }
                    }

                    if (edsmid > 0)
                    {
                        DictEDSMBodies[edsmid] = bodies;
                    }

                    if (id64 != null && id64 > 0)
                    {
                        DictEDSMBodiesByID64[id64.Value] = bodies;
                    }

                    return(bodies);
                }

                if (edsmid > 0)
                {
                    DictEDSMBodies[edsmid] = null;
                }

                if (id64 != null && id64 > 0)
                {
                    DictEDSMBodiesByID64[id64.Value] = null;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Exception: {ex.Message}");
                Trace.WriteLine($"ETrace: {ex.StackTrace}");
                return(null);
            }
            return(null);
        }