Ejemplo n.º 1
0
        /// <summary>
        /// Reads new session info and raises the SessionInfoUpdated event, regardless of if the session info has changed.
        /// </summary>
        public void RequestSessionInfoUpdate()
        {
            var sessionInfo = sdk.GetSessionInfo();
            var time        = (double)sdk.GetData("SessionTime");
            var sessionArgs = new SessionInfoUpdatedEventArgs(sessionInfo, time);

            this.RaiseEvent(OnSessionInfoUpdated, sessionArgs);
        }
Ejemplo n.º 2
0
        private void OnSessionInfoUpdated(SessionInfoUpdatedEventArgs e)
        {
            var handler = this.SessionInfoUpdated;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Ejemplo n.º 3
0
        private void Loop()
        {
            int lastUpdate = -1;

            while (_IsRunning)
            {
                // Check if we can find the sim
                if (sdk.IsConnected())
                {
                    if (!_IsConnected)
                    {
                        // If this is the first time, raise the Connected event
                        this.RaiseEvent(OnConnected, EventArgs.Empty);
                    }

                    _hasConnected = true;
                    _IsConnected  = true;

                    readMutex.WaitOne(8);

                    int       attempts    = 0;
                    const int maxAttempts = 99;

                    object sessionnum = this.TryGetSessionNum();
                    while (sessionnum == null && attempts <= maxAttempts)
                    {
                        attempts++;
                        sessionnum = this.TryGetSessionNum();
                    }
                    if (attempts >= maxAttempts)
                    {
                        Debug.WriteLine("Session num too many attempts");
                        continue;
                    }

                    // Parse out your own driver Id
                    if (this.DriverId == -1)
                    {
                        _DriverId = (int)sdk.GetData("PlayerCarIdx");
                    }

                    // Get the session time (in seconds) of this update
                    var time = (double)sdk.GetData("SessionTime");

                    // Raise the TelemetryUpdated event and pass along the lap info and session time
                    var telArgs = new TelemetryUpdatedEventArgs(new TelemetryInfo(sdk), time);
                    this.RaiseEvent(OnTelemetryUpdated, telArgs);

                    // Is the session info updated?
                    int newUpdate = sdk.Header.SessionInfoUpdate;
                    if (newUpdate != lastUpdate)
                    {
                        lastUpdate = newUpdate;

                        // Get the session info string
                        var sessionInfo = sdk.GetSessionInfo();

                        // Raise the SessionInfoUpdated event and pass along the session info and session time.
                        var sessionArgs = new SessionInfoUpdatedEventArgs(sessionInfo, time);
                        this.RaiseEvent(OnSessionInfoUpdated, sessionArgs);
                    }
                }
                else if (_hasConnected)
                {
                    // We have already been initialized before, so the sim is closing
                    this.RaiseEvent(OnDisconnected, EventArgs.Empty);

                    sdk.Shutdown();
                    _DriverId     = -1;
                    lastUpdate    = -1;
                    _IsConnected  = false;
                    _hasConnected = false;
                }
                else
                {
                    _IsConnected  = false;
                    _hasConnected = false;
                    _DriverId     = -1;

                    //Try to find the sim
                    sdk.Startup();
                }

                // Sleep for a short amount of time until the next update is available
                if (_IsConnected)
                {
                    if (waitTime <= 0 || waitTime > 1000)
                    {
                        waitTime = 15;
                    }
                    Thread.Sleep(waitTime);
                }
                else
                {
                    // Not connected yet, no need to check every 16 ms, let's try again in some time
                    Thread.Sleep(ConnectSleepTime);
                }
            }

            sdk.Shutdown();
            _DriverId    = -1;
            _IsConnected = false;
        }
Ejemplo n.º 4
0
        private void Loop()
        {
            int lastUpdate = -1;

            while (_IsRunning)
            {
                // Check if we can find the sim
                if (sdk.IsConnected())
                {
                    if (!_IsConnected)
                    {
                        // If this is the first time, raise the Connected event
                        this.RaiseEvent(OnConnected, EventArgs.Empty);
                    }

                    _IsConnected = true;

                    // Get the session info string
                    string sessionInfo = sdk.GetSessionInfo();

                    // Parse out your own driver Id
                    if (this.DriverId == -1)
                    {
                        _DriverId = int.Parse(YamlParser.Parse(sessionInfo, "DriverInfo:DriverCarIdx:"));
                    }

                    // Get the session time (in seconds) of this update
                    var time = (double)sdk.GetData("SessionTime");

                    // Is the session info updated?
                    int newUpdate = sdk.Header.SessionInfoUpdate;
                    if (newUpdate != lastUpdate)
                    {
                        lastUpdate = newUpdate;

                        // Raise the SessionInfoUpdated event and pass along the session info and session time.
                        var sessionArgs = new SessionInfoUpdatedEventArgs(sessionInfo, time);
                        this.RaiseEvent(OnSessionInfoUpdated, sessionArgs);
                    }

                    // Raise the TelemetryUpdated event and pass along the lap info and session time
                    var telArgs = new TelemetryUpdatedEventArgs(new TelemetryInfo(sdk), time);
                    this.RaiseEvent(OnTelemetryUpdated, telArgs);
                }
                else if (sdk.IsInitialized)
                {
                    // We have already been initialized before, so the sim is closing
                    this.RaiseEvent(OnDisconnected, EventArgs.Empty);

                    sdk.Shutdown();
                    _DriverId    = -1;
                    lastUpdate   = -1;
                    _IsConnected = false;
                }
                else
                {
                    _IsConnected = false;
                    _DriverId    = -1;

                    //Try to find the sim
                    sdk.Startup();
                }

                // Sleep for a short amount of time until the next update is available
                if (_IsConnected)
                {
                    if (waitTime <= 0 || waitTime > 1000)
                    {
                        waitTime = 15;
                    }
                    Thread.Sleep(waitTime);
                }
                else
                {
                    // Not connected yet, no need to check every 16 ms, let's try again in a second
                    Thread.Sleep(1000);
                }
            }

            sdk.Shutdown();
            _DriverId    = -1;
            _IsConnected = false;
        }