private void ProcessStatusFileUpdate(string statusFile, bool updateTimeStamp = false)
        {
            // Read the status from the file and update the UI
            if (String.IsNullOrEmpty(statusFile))
            {
                return;
            }

            string status = "";

            try
            {
                // Read the file - we open in file share mode as E: D will be constantly writing to this file
                if (_statusFileStream == null)
                {
                    _statusFileStream = new FileStream(statusFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                }
                _statusFileStream.Seek(0, SeekOrigin.Begin);

                using (StreamReader sr = new StreamReader(_statusFileStream, Encoding.Default, true, 1000, true))
                    status = sr.ReadToEnd();

                if (!_statusFileStream.CanSeek)
                {
                    // We only close the file if we can't seek (no point in continuously reopening)
                    _statusFileStream.Close();
                    _statusFileStream = null;
                }
            }
            catch  {}
            if (String.IsNullOrEmpty(status))
            {
                return;
            }

            try
            {
                // E: D status.json file does not include milliseconds in the timestamp.  We want milliseconds, so we add our own timestamp
                // This also gives us polling every five seconds in case the commander stops moving (as soon as they move, the new status should be picked up)
                // Turns out milliseconds is pointless as E: D is very unlikely to generate a new status file more than once a second (and/or we won't detect it), but
                // we'll keep them in case this changes in future.
                EDEvent updateEvent;
                if (updateTimeStamp)
                {
                    updateEvent = new EDEvent(status, textBoxCommanderName.Text, DateTime.UtcNow);
                }
                else
                {
                    updateEvent = new EDEvent(status, textBoxCommanderName.Text);
                }

                UpdateUI(updateEvent);
            }
            catch { }
        }
        private void UploadToServer(EDEvent edEvent, bool ping = false)
        {
            if (_udpClient == null && !CreateUdpClient())
            {
                return;
            }

            if (ping)
            {
                if (_lastUploadedEvent == null)
                {
                    return;
                }
                edEvent           = _lastUploadedEvent;
                edEvent.TimeStamp = DateTime.UtcNow;
            }

            try
            {
                edEvent.Commander   = _clientId;
                edEvent.Mischievous = checkBoxAllowMischief.Checked;
                string eventData = edEvent.ToJson();
                edEvent.Commander = _commanderName;
                Byte[] sendBytes = Encoding.UTF8.GetBytes(eventData);
                try
                {
                    if (_udpClient.Send(sendBytes, sendBytes.Length) > 0)
                    {
                        _lastStatusSend    = DateTime.UtcNow;
                        _lastUploadedEvent = edEvent;
                    }
                }
                catch (Exception e)
                {
                    // If we have an error on send, we destroy the UDP client so that it is recreated on next send
                    try
                    {
                        _udpClient.Dispose();
                    }
                    catch { }
                    _udpClient = null;
                }
            }
            catch { }

            if (!_uploadValidated)
            {
                ValidateUploadWorking();
            }
        }
        private void CommanderWatcher_UpdateReceived(object sender, EDEvent edEvent)
        {
            if (!edEvent.HasCoordinates() || edEvent.Commander.Equals(FormTracker.CommanderName))
            {
                return;
            }

            if (checkBoxTrackClosest.Checked && FormTracker.CurrentLocation != null)
            {
                // If we are tracking the closest commander to us, we need to check all updates and change our tracking target as necessary
                // We just check if the distance from this commander is closer than our currently tracked target

                double distanceToCommander = DistanceBetween(FormTracker.CurrentLocation, edEvent.Location());
                if (distanceToCommander == 0) // This is impossible, and just means we haven't got data on the tracked target
                {
                    distanceToCommander = double.MaxValue;
                }
                if (distanceToCommander < _closestCommanderDistance)
                {
                    _closestCommander         = edEvent;
                    _closestCommanderDistance = distanceToCommander;
                }
                else if (edEvent.Commander == _closestCommander.Commander)
                {
                    _closestCommanderDistance = distanceToCommander;
                }

                // Switch tracking to this target
                _targetPosition = edEvent.Location();
                SetTarget(edEvent.Commander);
                return;
            }

            if (!TrackingTarget.Equals(edEvent.Commander))
            {
                return;
            }

            _targetPosition = edEvent.Location();
            DisplayTarget();
            UpdateTracking();
        }
        private void UpdateTrackingTarget(string target)
        {
            Action action;

            TrackingTarget = target;

            if (_targetPosition == null)
            {
                EDEvent commanderEvent = CommanderWatcher.GetCommanderMostRecentEvent(target);
                if (commanderEvent != null)
                {
                    _targetPosition = commanderEvent.Location();
                }
            }

            locatorHUD1.SetTarget(target);
            string bearingInfo = $"Bearing (tracking {target})";

            if (String.IsNullOrEmpty(target))
            {
                bearingInfo = "Bearing (target not set)";
            }

            if (String.IsNullOrEmpty(TrackingTarget))
            {
                if (buttonTrackCommander.Text.Equals("Stop"))
                {
                    action = new Action(() => { buttonTrackCommander.Text = "Track"; });
                    if (buttonTrackCommander.InvokeRequired)
                    {
                        buttonTrackCommander.Invoke(action);
                    }
                    else
                    {
                        action();
                    }
                }
            }
        }
Exemple #5
0
 private void UploadToServer(EDEvent edEvent)
 {
     if (_udpClient == null)
     {
         if (!CreateUdpClient())
         {
             return;
         }
     }
     edEvent.Commander = CommanderGuid(edEvent.Commander).ToString();
     try
     {
         string eventData = edEvent.ToJson();
         Byte[] sendBytes = Encoding.UTF8.GetBytes(eventData);
         try
         {
             _udpClient.Send(sendBytes, sendBytes.Length);
         }
         catch { }
     }
     catch { }
 }
Exemple #6
0
        private void UpdateCommanderStatus(string status)
        {
            EDEvent updateEvent = null;

            try
            {
                updateEvent = EDEvent.FromJson(status);
                string commanderName = _commanderRegistration.CommanderName(updateEvent.Commander);
                if (String.IsNullOrEmpty(commanderName))
                {
                    Log($"Unregistered commander ignored: {updateEvent.Commander}", true);
                    return;
                }
                updateEvent.Commander = commanderName;
            }
            catch (Exception ex)
            {
                LogError($"Error creating event: {ex.Message}");
                return;
            }

            if (_races.Count > 0)
            {
                Task.Run(new Action(() =>
                {
                    foreach (Guid raceGuid in _races.Keys)
                    {
                        if (!_races[raceGuid].Finished)
                        {
                            try
                            {
                                _races[raceGuid].UpdateStatus(updateEvent);
                                Log($"{raceGuid}: Updated {updateEvent.Commander}", true);
                            }
                            catch (Exception ex)
                            {
                                LogError($"{raceGuid}: Unexpected error on update: {ex}");
                            }
                        }
                        else
                        {
                            Log($"{raceGuid}: not updated, race finished", true);
                        }
                    }
                    if (DateTime.UtcNow.Subtract(_lastFinishedRaceCheck).TotalMinutes >= 5)
                    {
                        foreach (Guid raceGuid in _races.Keys)
                        {
                            if (!_races[raceGuid].Finished)
                            {
                                if (_races[raceGuid].CheckIfFinished())
                                {
                                    SaveRaceToDisk(raceGuid);
                                }
                            }
                        }
                        _lastFinishedRaceCheck = DateTime.UtcNow;
                    }
                }));
            }

            try
            {
                lock (_notificationLock)
                {
                    if (_playerStatus.ContainsKey(updateEvent.Commander))
                    {
                        _playerStatus[updateEvent.Commander] = updateEvent;
                        Log($"Processed status update for commander: {updateEvent.Commander}", true);
                    }
                    else
                    {
                        _playerStatus.Add(updateEvent.Commander, updateEvent);
                        Log($"Received status update for new commander: {updateEvent.Commander}", true);
                    }
                }
            }
            catch (Exception ex)
            {
                LogError($"Error processing update:{Environment.NewLine}{ex}{Environment.NewLine}{Environment.NewLine}{status}");
            }
        }
        private void UpdateUI(EDEvent edEvent)
        {
            //if (checkBoxSaveTelemetryFolder.Checked)
            //    SaveToFile(edEvent);

            if (checkBoxUpload.Checked)
            {
                UploadToServer(edEvent);
            }

            if (edEvent.Flags > 0)
            {
                _formFlagsWatcher?.UpdateFlags(edEvent.Flags);
            }

            if ((edEvent.PlanetRadius > 0) && (FormLocator.PlanetaryRadius != edEvent.PlanetRadius))
            {
                FormLocator.PlanetaryRadius = edEvent.PlanetRadius;
            }

            // Update the UI with the event data
            Action action;

            if (!_lastUpdateTime.Equals(edEvent.TimeStamp.ToString("HH:MM:ss")))
            {
                _lastUpdateTime = edEvent.TimeStamp.ToString("HH:MM:ss");
                action          = new Action(() => { labelLastUpdateTime.Text = _lastUpdateTime; });
                if (labelLastUpdateTime.InvokeRequired)
                {
                    labelLastUpdateTime.Invoke(action);
                }
                else
                {
                    action();
                }
            }

            _vehicleTelemetry?.ProcessEvent(edEvent, !checkBoxCaptureSRVTelemetry.Checked);
            if (edEvent.HasCoordinates())
            {
                if (checkBoxUseDirectionOfTravelAsHeading.Checked && (!checkBoxUseSmartHeadingOnlyWhenInSRV.Checked || edEvent.isInSRV()) && (!checkBoxUseSmartHeadingOnlyWhenOver.Checked || (SpeedInMS > (int)numericUpDownUseSmartHeadingOnlyWhenOver.Value)))
                {
                    // We ignore the heading given by E: D, as that is direction we are facing, not travelling
                    // We calculate our direction based on previous location
                    CurrentHeading = _vehicleTelemetry.CurrentHeading;
                }
                else
                {
                    CurrentHeading = edEvent.Heading;
                }

                PreviousLocation          = CurrentLocation.Copy();
                CurrentLocation.Latitude  = edEvent.Latitude;
                CurrentLocation.Longitude = edEvent.Longitude;
                CurrentLocation.Altitude  = edEvent.Altitude;
                if (!String.IsNullOrEmpty(edEvent.BodyName))
                {
                    CurrentLocation.PlanetName = edEvent.BodyName;
                }
                if (edEvent.PlanetRadius > 0)
                {
                    CurrentLocation.PlanetaryRadius = edEvent.PlanetRadius;
                }

                CommanderLocationChanged?.Invoke(null, null);
            }

            action = new Action(() => { labelLastUpdateTime.Text = DateTime.UtcNow.ToString("HH:mm:ss"); });
            if (labelLastUpdateTime.InvokeRequired)
            {
                labelLastUpdateTime.Invoke(action);
            }
            else
            {
                action();
            }
        }
        private void _journalReader_InterestingEventOccurred(object sender, string eventJson)
        {
            EDEvent updateEvent = new EDEvent(eventJson, textBoxCommanderName.Text);

            UpdateUI(updateEvent);
        }