public void sendEvent(string eventData) { if (!EdsmCredentialsSet()) { return; } // The EDSM responder has a `inBeta` 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", eventData); Thread thread = new Thread(() => { try { Logging.Debug("Sending event to EDSM: " + restClient.BuildUri(request).AbsoluteUri); var clientResponse = restClient.Execute <StarMapLogResponse>(request); StarMapLogResponse response = clientResponse.Data; if (response?.msgnum != 100) { if (response?.msg != null) { Logging.Warn("EDSM responded with " + response?.msg); } else { Logging.Warn(clientResponse?.ErrorMessage); } } Thread.Sleep(200); // Add a buffer to prevent time outs. } catch (ThreadAbortException) { Logging.Debug("Thread aborted"); } catch (Exception ex) { Logging.Warn("Failed to send event to EDSM", ex); } }) { IsBackground = true, Name = "StarMapService send event" }; thread.Start(); }
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); } }