Storage of credentials for a single Elite: Dangerous user to access EDSM
 public void Sync(DateTime?since = null)
 {
     Logging.Info("Syncing with EDSM");
     try
     {
         Dictionary <string, StarMapLogInfo> systems  = getStarMapLog(since);
         Dictionary <string, string>         comments = getStarMapComments();
         int total = systems.Count;
         foreach (string system in systems.Keys)
         {
             StarSystem CurrentStarSystem = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(system, false);
             CurrentStarSystem.visits    = systems[system].visits;
             CurrentStarSystem.lastvisit = systems[system].lastVisit;
             if (comments.ContainsKey(system))
             {
                 CurrentStarSystem.comment = comments[system];
             }
             StarSystemSqLiteRepository.Instance.SaveStarSystem(CurrentStarSystem);
         }
         StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();
         starMapConfiguration.lastSync = DateTime.UtcNow;
         starMapConfiguration.ToFile();
         Logging.Info("EDSM sync completed");
     }
     catch (EDSMException edsme)
     {
         Logging.Debug("EDSM error received: " + edsme.Message);
     }
 }
        /// <summary>
        /// Obtain credentials from a file.  If the file name is not supplied the the default
        /// path of Constants.Data_DIR\edsm.json is used
        /// </summary>
        public static StarMapConfiguration FromFile(string filename = null)
        {
            if (filename == null)
            {
                filename = Constants.DATA_DIR + @"\edsm.json";
            }

            StarMapConfiguration configuration = new StarMapConfiguration();

            try
            {
                string credentialsData = File.ReadAllText(filename);
                configuration = JsonConvert.DeserializeObject <StarMapConfiguration>(credentialsData);
            }
            catch (Exception ex)
            {
                Logging.Debug("Failed to read starmap configuration", ex);
            }
            if (configuration == null)
            {
                configuration = new StarMapConfiguration();
            }

            configuration.dataPath = filename;
            return(configuration);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Obtain credentials from a file.  If the file name is not supplied the the default
        /// path of Constants.Data_DIR\edsm.json is used
        /// </summary>
        public static StarMapConfiguration FromFile(string filename = null)
        {
            if (filename == null)
            {
                filename = Constants.DATA_DIR + @"\edsm.json";
            }

            StarMapConfiguration configuration = new StarMapConfiguration();

            if (File.Exists(filename))
            {
                string data = Files.Read(filename);
                if (data != null)
                {
                    try
                    {
                        configuration = JsonConvert.DeserializeObject <StarMapConfiguration>(data);
                    }
                    catch (Exception ex)
                    {
                        Logging.Debug("Failed to read starmap configuration", ex);
                    }
                }
            }
            if (configuration == null)
            {
                configuration = new StarMapConfiguration();
            }

            configuration.dataPath = filename;
            return(configuration);
        }
Exemplo n.º 4
0
        private void SendQueuedEvents(CancellationToken cancellationToken)
        {
            var queue = new List <IDictionary <string, object> >();

            try
            {
                // The `GetConsumingEnumerable` method blocks the thread while the underlying collection is empty
                // If we haven't extracted events to send to EDSM, this will wait / pause background sync until `queuedEvents` is no longer empty.
                foreach (var pendingEvent in queuedEvents.GetConsumingEnumerable(cancellationToken))
                {
                    queue.Add(pendingEvent);
                    if (queue.Count > 0 && queuedEvents.Count == 0)
                    {
                        break;
                    }
                }
                StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();
                SendEventBatch(queue, starMapConfiguration);
            }
            catch (OperationCanceledException)
            {
                // Operation was cancelled. Return any events we've extracted back to the primary queue.
                foreach (var pendingEvent in queue)
                {
                    queuedEvents.Add(pendingEvent);
                }
            }
        }
Exemplo n.º 5
0
        public static void saveStarSystems(List <StarSystem> syncSystems)
        {
            StarSystemSqLiteRepository.Instance.SaveStarSystems(syncSystems);
            StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();

            starMapConfiguration.lastSync = DateTime.UtcNow;
            starMapConfiguration.ToFile();
        }
 private void updateEdsmConfiguration()
 {
     StarMapConfiguration edsmConfiguration = new StarMapConfiguration();
     if (!string.IsNullOrWhiteSpace(edsmApiKeyTextBox.Text))
     {
         edsmConfiguration.apiKey = edsmApiKeyTextBox.Text.Trim();
     }
     if (!string.IsNullOrWhiteSpace(edsmCommanderNameTextBox.Text))
     {
         edsmConfiguration.commanderName = edsmCommanderNameTextBox.Text.Trim();
     }
     edsmConfiguration.ToFile();
     EDDI.Instance.Reload("EDSM responder");
 }
Exemplo n.º 7
0
        private void SendQueuedEvents()
        {
            StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();
            var queue = new List <IDictionary <string, object> >();

            // The `GetConsumingEnumerable` method blocks the thread while the underlying collection is empty
            // If we haven't extracted events to send to Inara, this will wait / pause background sync until `queuedAPIEvents` is no longer empty.
            foreach (var pendingEvent in queuedEvents.GetConsumingEnumerable())
            {
                queue.Add(pendingEvent);
                if (queue.Count > 0 && queuedEvents.Count == 0)
                {
                    break;
                }
            }
            SendEventBatch(queue, starMapConfiguration);
        }
        /// <summary>
        /// Obtain credentials from a file.  If the file name is not supplied the the default
        /// path of Constants.Data_DIR\edsm.json is used
        /// </summary>
        public static StarMapConfiguration FromFile(string filename=null)
        {
            if (filename == null)
            {
                filename = Constants.DATA_DIR + @"\edsm.json";
            }

            StarMapConfiguration credentials = new StarMapConfiguration();
            try
            {
                string credentialsData = File.ReadAllText(filename);
                credentials = JsonConvert.DeserializeObject<StarMapConfiguration>(credentialsData);
            }
            catch {}

            credentials.dataPath = filename;
            return credentials;
        }
        /// <summary>
        /// Obtain credentials from a file.  If the file name is not supplied the the default
        /// path of Constants.Data_DIR\edsm.json is used
        /// </summary>
        public static StarMapConfiguration FromFile(string filename = null)
        {
            if (filename == null)
            {
                filename = Constants.DATA_DIR + @"\edsm.json";
            }

            StarMapConfiguration credentials = new StarMapConfiguration();

            try
            {
                string credentialsData = File.ReadAllText(filename);
                credentials = JsonConvert.DeserializeObject <StarMapConfiguration>(credentialsData);
            }
            catch {}

            credentials.dataPath = filename;
            return(credentials);
        }
Exemplo n.º 10
0
        public void SetEdsmCredentials()
        {
            StarMapConfiguration starMapCredentials = StarMapConfiguration.FromFile();

            if (!string.IsNullOrEmpty(starMapCredentials?.apiKey))
            {
                // Commander name might come from EDSM credentials or from the game and companion app
                string cmdrName = starMapCredentials.commanderName ?? commanderFrontierApiName;
                if (!string.IsNullOrEmpty(cmdrName))
                {
                    apiKey        = starMapCredentials.apiKey?.Trim();
                    commanderName = cmdrName?.Trim();
                }
                else
                {
                    Logging.Warn("EDSM Responder not configured: Commander name not set.");
                }
            }
            else
            {
                Logging.Warn("EDSM Responder not configured: API key not set.");
            }
        }
Exemplo n.º 11
0
        public StarMapService()
        {
            // Set up the star map service
            StarMapConfiguration starMapCredentials = StarMapConfiguration.FromFile();

            if (starMapCredentials != null && starMapCredentials.apiKey != null)
            {
                // Commander name might come from star map credentials or the companion app's profile
                string commanderName = null;
                if (starMapCredentials.commanderName != null)
                {
                    commanderName = starMapCredentials.commanderName;
                }
                if (commanderName != null)
                {
                    instance = new StarMapService(starMapCredentials.apiKey, commanderName);
                    Logging.Info("EDDI access to EDSM is enabled");
                }
            }
            if (instance == null)
            {
                Logging.Info("EDDI access to EDSM is disabled");
            }
        }
Exemplo n.º 12
0
        private void SendEventBatch(List <IDictionary <string, object> > eventData, StarMapConfiguration starMapConfiguration)
        {
            if (!EdsmCredentialsSet())
            {
                return;
            }

            // Filter any stale data
            eventData = eventData
                        .Where(e => JsonParsing.getDateTime("timestamp", e) > starMapConfiguration.lastJournalSync)
                        .ToList();
            if (eventData.Count == 0)
            {
                return;
            }

            // The EDSM responder has a `gameIsBeta` flag that it checks prior to sending data via this method.
            var request = new RestRequest("api-journal-v1", Method.POST);

            request.AddParameter("commanderName", commanderName);
            request.AddParameter("apiKey", apiKey);
            request.AddParameter("fromSoftware", Constants.EDDI_NAME);
            request.AddParameter("fromSoftwareVersion", Constants.EDDI_VERSION);
            request.AddParameter("message", JsonConvert.SerializeObject(eventData).Normalize());
            request.Timeout = JournalTimeoutMilliseconds;

            try
            {
                Logging.Debug("Sending message to EDSM: " + restClient.BuildUri(request).AbsoluteUri);
                var clientResponse          = restClient.Execute <StarMapLogResponse>(request);
                StarMapLogResponse response = clientResponse.Data;

                if (response is null)
                {
                    Logging.Warn(clientResponse.ErrorMessage);
                    ReEnqueueEvents(eventData);
                }
                else if (response.msgnum >= 100 && response.msgnum <= 103)
                {
                    // 100 -  Everything went fine!
                    // 101 -  The journal message was already processed in our database.
                    // 102 -  The journal message was already in a newer version in our database.
                    // 103 -  Duplicate event request (already reported from another software client).
                    starMapConfiguration.lastJournalSync = eventData
                                                           .Select(e => JsonParsing.getDateTime("timestamp", e))
                                                           .Max();
                    starMapConfiguration.ToFile();
                }
                if (response?.msgnum != 100)
                {
                    if (!string.IsNullOrEmpty(response?.msg))
                    {
                        Logging.Warn("EDSM responded with: " + response.msg);
                    }
                    else
                    {
                        Logging.Warn("EDSM responded with: " + JsonConvert.SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Warn("Failed to send event to EDSM", ex);
            }
        }
 public static void obtainEdsmLogs(StarMapConfiguration starMapConfiguration, string commanderName, IProgress<string> progress)
 {
     StarMapService starMapService = new StarMapService(starMapConfiguration.apiKey, commanderName);
     Dictionary<string, StarMapLogInfo> systems = starMapService.getStarMapLog();
     Dictionary<string, string> comments = starMapService.getStarMapComments();
     foreach (string system in systems.Keys)
     {
         progress.Report(system);
         StarSystem CurrentStarSystem = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(system, false);
         CurrentStarSystem.visits = systems[system].visits;
         CurrentStarSystem.lastvisit = systems[system].lastVisit;
         if (comments.ContainsKey(system))
         {
             CurrentStarSystem.comment = comments[system];
         }
         StarSystemSqLiteRepository.Instance.SaveStarSystem(CurrentStarSystem);
     }
 }
Exemplo n.º 14
0
        private void SendEvents(List <IDictionary <string, object> > queue)
        {
            StarMapConfiguration starMapConfiguration = StarMapConfiguration.FromFile();

            SendEventBatch(queue, starMapConfiguration);
        }