public ApiModule(TelemetryLapManagerFactory managerFactory) : base("/api") { _telemetryLapManager = managerFactory.GetManager(); Get["/packet"] = parameters => { var speedDelta = _telemetryLapManager.GetSpeedDelta(); var timeDelta = _telemetryLapManager.GetTimeDelta(); if (speedDelta == "--") { speedDelta = "+0"; } var speedPositive = speedDelta.Substring(0, 1) == "+"; var timePositive = timeDelta > 0; var speedAbs = speedDelta.Substring(1); var model = new DashViewModel { CircuitName = _telemetryLapManager.Circuit.Name, LapType = _telemetryLapManager.LapType ?? "", SpeedDelta = float.Parse(speedAbs), TimeDelta = timeDelta, IsSpeedDeltaPositive = speedPositive, IsTimeDeltaPositive = timePositive, WheelspinRearLeft = _telemetryLapManager.CurrentWheelSpin(WheelspinWheel.RearLeft), WheelspinRearRight = _telemetryLapManager.CurrentWheelSpin(WheelspinWheel.RearRight), WheelspinFrontLeft = _telemetryLapManager.CurrentWheelSpin(WheelspinWheel.FrontLeft), WheelspinFrontRight = _telemetryLapManager.CurrentWheelSpin(WheelspinWheel.FrontRight), Throttle = _telemetryLapManager.CurrentThrottle, Brake = _telemetryLapManager.CurrentBrake, CurrentLap = _telemetryLapManager.CurrentLapTime, LastLap = _telemetryLapManager.LastLapTime, Sector1 = new SectorTimeViewModel(_telemetryLapManager.Sector1), Sector2 = new SectorTimeViewModel(_telemetryLapManager.Sector2), Sector3 = new SectorTimeViewModel(_telemetryLapManager.Sector3) }; if (_telemetryLapManager.FastestLap == null) { model.FastestLap = 0f.AsTimeString(); } else { model.FastestLap = _telemetryLapManager.FastestLap.LapTime.AsTimeString(); } return(Response.AsJson(model)); }; }
// Method that periodically (per invocations through the timer) updates // the displayed telemetry private void UpdateDisplay(Object myObject, EventArgs myEventArgs) { // Suspend the timer timer1.Stop(); // Wait for mutex lock syncMutex.WaitOne(); if (!string.IsNullOrEmpty(cboField.SelectedItem.ToString())) { FieldValueLabel.Text = manager.GetCurrentData(cboField.SelectedItem.ToString()); } // Update the display var delta = manager.GetSpeedDelta(); DeltaLabel.Text = delta; if (delta != "--") { DeltaLabel.ForeColor = delta.Substring(0, 1) == "+" ? Color.Green : Color.Red; } else { DeltaLabel.ForeColor = Color.White; } ComparisonModeLabel.Text = manager.ComparisonMode == ComparisonModeEnum.Reference ? "Reference Lap" : "Fastest Lap"; ComparisonLapLabel.Text = manager.ComparisonLapTime; CurrentLapLabel.Text = manager.CurrentLapTime; LastLapLabel.Text = manager.LastLapTime; float fuelRemaining = Convert.ToSingle(manager.GetCurrentData("FuelRemaining")); if (fuelRemaining > 0) { fuelRemainingLabel.Text = fuelRemaining.ToString("0.00"); if (lastLapFuelUsed > 0) { consumedFuelLabel.Text = lastLapFuelUsed.ToString("0.00"); } else { consumedFuelLabel.Text = "-"; } } else { fuelRemainingLabel.Text = "-"; consumedFuelLabel.Text = "-"; } if (fuelTarget > 0.2) { targetFuelLabel.Text = fuelTarget.ToString("0.00"); } else { targetFuelLabel.Text = "-"; } UpdateTimeDelta(manager.GetTimeDelta()); UpdateThrottleBrake(manager.CurrentThrottle, manager.CurrentBrake); UpdateWheelSpin(manager.CurrentWheelSpin(WheelspinWheel.FrontLeft), manager.CurrentWheelSpin(WheelspinWheel.FrontRight), manager.CurrentWheelSpin(WheelspinWheel.RearLeft), manager.CurrentWheelSpin(WheelspinWheel.RearRight)); UpdateSectors(); TransmissionLabel.BackColor = Color.Transparent; // Release the lock syncMutex.ReleaseMutex(); Application.DoEvents(); // Restart the timer timer1.Start(); }