private void MovingAverageCalculatedEventHandler(object sender, MovingAverageCalculatedEventArgs e) { if (!mStarted) { return; } double?intensityFactor = null; int? trainingStressScore = null; double?npWattsPerKg = null; ulong movingAvgPow4 = (ulong)Math.Pow(e.APwatts, 4); mSumMovingAvgPow4 += movingAvgPow4; mCountMovingAvgPow4 += 1; double avgMovingAvgPow4 = mSumMovingAvgPow4 / (double)mCountMovingAvgPow4; double npWatts = Math.Pow(avgMovingAvgPow4, 0.25); // calculate average w/kg npWattsPerKg = CalculateUserWattsPerKg(npWatts); if (CurrentUserProfile.PowerThreshold > 0) { // Calculate Intensity Factor intensityFactor = Math.Round(npWatts / (double)CurrentUserProfile.PowerThreshold, 2); // Calculate TSS //TimeSpan runningTime = DateTime.Now - m_collectionStartTime; trainingStressScore = (int)Math.Round((e.ElapsedTime.TotalSeconds * npWatts * (double)intensityFactor) / (CurrentUserProfile.PowerThreshold * 3600) * 100, 0); } npWatts = Math.Round(npWatts, 0); // when NP changes, send it and the current overall average power through if ((int)npWatts != this.mCurNPwatts || intensityFactor != this.mCurIntensityFactor || trainingStressScore != this.mCurTrainingStressScore) { this.mCurNPwatts = (int)npWatts; this.mCurNPwattsPerKg = npWattsPerKg; this.mCurTrainingStressScore = trainingStressScore; this.mCurIntensityFactor = intensityFactor; OnNormalizedPowerChangedEvent(new NormalizedPowerChangedEventArgs((int)npWatts, npWattsPerKg, intensityFactor, trainingStressScore)); } }
private void OnMovingAverageCalculatedEvent(MovingAverageCalculatedEventArgs e) { EventHandler <MovingAverageCalculatedEventArgs> handler = MovingAverageCalculatedEvent; if (handler != null) { try { handler(this, e); } catch (Exception ex) { // Don't let downstream exceptions bubble up Logger.LogError(ex, $"Caught in {this.GetType()} (OnMovingAverageCalculatedEvent)"); } } }