/// <summary> /// This method processes the sensor data /// </summary> /// <param name="readingSession">The reading session</param> public void ProcessSensorData(MeasureReadingSession readingSession) { if (readingSession == null) { Log.Debug(LOG_TAG, GetType() + ".ProcessSensorData: readingSession is NULL"); return; } Sensor currentSensor; User currentUser = this.userRepository.GetCurrentUser(); this.userRepository.ChangeName(currentUser, "Josoa"); currentUser = this.userRepository.GetCurrentUser(); // if the sensor extracted from the reading session is New if (!this.sensorRepository.Exists(readingSession.SensorId)) { Log.Debug(LOG_TAG, GetType() + ".ProcessSensorData: register new sensor. readingSession.SensorId=[" + readingSession.SensorId + "]"); // first, find the current sensor currentSensor = this.sensorRepository.GetCurrentSensor(); // if the sensor exits, we "remove" it if (currentSensor != null) { Log.Debug(LOG_TAG, GetType() + ".ProcessSensorData: removing sensor=[" + currentSensor.Id + "]"); this.sensorRepository.Remove(currentSensor); } // then, we register the new sensor Sensor toRegister = new Sensor(); toRegister.Id = readingSession.SensorId; this.sensorRepository.RegisterNewSensor(toRegister); currentSensor = this.sensorRepository.GetCurrentSensor(); } else { currentSensor = this.sensorRepository.GetCurrentSensor(); Log.Debug(LOG_TAG, GetType() + ".ProcessSensorData: updating existing sensor=[" + currentSensor.Id + "]"); } GlucoseMeasure lastGlucoseMeasure = readingSession.CurrentMeasure; //Update the device battery level this.userRepository.UpdateDeviceBatteryPercentage(currentUser, readingSession.BatteryLevel); //Update the last value checked time this.userRepository.UpdateLastValueTimeStamp(currentUser, lastGlucoseMeasure.RealDateTimeOffset); //Send notification to update battery and data in drive page this.eventAggregator.GetEvent <DrivePageDataUpdateEvent>().Publish(); }
/// <summary> /// This method processes the reading data session /// </summary> /// <param name="readingSession">The values of the reading session</param> public void HandleReadingSession(MeasureReadingSession readingSession) { Log.Debug(LOG_TAG, GetType() + ".ReportReadingSession:" + readingSession.ToString()); if (!Utils.IsValidTDE(this.appSettings.TDE)) { return; } Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { ProcessSensorData(readingSession); ProcessMeasuresData(this.userRepository.GetCurrentUser().Id, readingSession); ProcessAveragesValues(); Analytics.TrackEvent(AnalyticsEvent.GlucoseMeasured); }); }
/// <summary> /// This method processes the glucose measures /// </summary> /// <param name="userId">The userId</param> /// <param name="freshMeasures">The list of the fresh measures</param> public void ProcessMeasuresData(string userId, MeasureReadingSession readingSession) { var currentUser = this.userRepository.GetCurrentUser(); var userSettings = this.userSettingsRepository.GetCurrentUserSettings(); bool isCreation = false; // first, we check the age of the sensor var currentSensor = this.sensorRepository.GetCurrentSensor(); long newSensorAge = readingSession.TrendMeasures[readingSession.TrendMeasures.Count - 1].SensorTime; long savedSensorAge = currentSensor.Age; if (newSensorAge > savedSensorAge) { Log.Debug(LOG_TAG, GetType() + ".ProcessMeasuresData: Sensor age has advanced. Try to insert measures" + savedSensorAge); savedSensorAge = newSensorAge; this.sensorRepository.UpdateSensorAge(currentSensor, savedSensorAge); } else if (newSensorAge == savedSensorAge) { Log.Debug(LOG_TAG, GetType() + ".ProcessMeasuresData: Sensor age has not advanced. sensorAge=[" + savedSensorAge + "]"); return; // do not try to insert again } else { Log.Debug(LOG_TAG, GetType() + ".ProcessMeasuresData: Sensor age has gone backwards!!! " + savedSensorAge); savedSensorAge = newSensorAge; this.sensorRepository.UpdateSensorAge(currentSensor, savedSensorAge); } GlucoseMeasure lastInsertedMeasure = this.glucoseMeasureRepository.GetLastMeasureByUser(userId); // create each GlucoseMeasure foreach (GlucoseMeasure measure in readingSession.GetAllMeasures()) { if (lastInsertedMeasure != null && lastInsertedMeasure.Timestamp > measure.Timestamp) { continue; } // possibly, the trend measures interval could be one minute // we only want 5 minutes interval if (measure.SensorTime % appSettings.MEASURE_NOTIFICATION_INTERVAL != 0) { continue; } isCreation = true; // creation first measure.Id = Guid.NewGuid().ToString(); this.glucoseMeasureRepository.Create(measure, userId); // then updates // measureoffset this.glucoseMeasureRepository.UpdateMeasureOffset(measure, userSettings.MeasureOffset, appSettings); // In The medical zone //medical zone this.glucoseMeasureRepository.ChangeIsInTheMedicalZone(measure, InTheMedicalZone(measure)); Log.Debug(LOG_TAG, GetType() + ".ProcessMeasuresData: Creating measure " + measure.ToString()); } GlucoseMeasure lastGlucoseMeasure = readingSession.CurrentMeasure; //Last glucose measure additionnal setup if (lastGlucoseMeasure.Id == null) { //Add to the last glucose measure an generatedId lastGlucoseMeasure.Id = Guid.NewGuid().ToString(); //Save the last glucose measure into the database this.glucoseMeasureRepository.Create(lastGlucoseMeasure, userId); } if (lastGlucoseMeasure.UserId == null) { //Add to the last glucose measure an userId this.glucoseMeasureRepository.ChangeUserId(lastGlucoseMeasure, userId); } //Check if the measure is in the medical zone this.glucoseMeasureRepository.ChangeIsInTheMedicalZone(lastGlucoseMeasure, InTheMedicalZone(lastGlucoseMeasure)); //Apply the offset lastGlucoseMeasure = this.glucoseMeasureRepository.UpdateMeasureOffset(lastGlucoseMeasure, userSettings.MeasureOffset, appSettings); Log.Debug(LOG_TAG, GetType() + ".ProcessMeasuresData: lastMeasure = " + lastGlucoseMeasure.ToString()); var toPublish = this.appSettings.HANDLE_MGDL_MEASURE ? lastGlucoseMeasure.GlucoseLevelMGDL : lastGlucoseMeasure.GlucoseLevelMMOL; // Spread the current last glucose measure this.eventAggregator.GetEvent <LastMeasureReceivedEvent>().Publish(toPublish); }