/// <summary> /// Event raised when the bluettoth device notify a value change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnCharacteristicValueChangeEvent(object sender, Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e) { if (this.eventAggregator == null) { eventAggregator = (IEventAggregator)App.Current.Container.Resolve(typeof(IEventAggregator)); } DialogBehaviourHolder beahaviour = FreeStyleLibreUtils.RespondToPacketBehaviour(e.Characteristic.Value); switch (beahaviour.ResponseType) { case PacketResponseType.Accept: eventAggregator.GetEvent <MeasureChangeEvent>().Publish(beahaviour.ReceivedData); this.MeasureServiceState = MeasureServiceState.RECEIVING_DATA; break; case PacketResponseType.AnswerBack: WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, beahaviour.Response); break; case PacketResponseType.Refuse: this.MeasureServiceState = MeasureServiceState.REFUSED_DATA_THEN_WAIT; break; case PacketResponseType.Ignore: default: this.MeasureServiceState = MeasureServiceState.WAITING_DATA; break; } }
/// <summary> /// Update the measure offset for a GlucoseMeasure /// </summary> /// <param name="measure">The measure to update</param> /// <param name="offsetValue">The offset value coming from the calibration</param> /// <param name="appSettings">The appSettings</param> public GlucoseMeasure UpdateMeasureOffset(GlucoseMeasure measure, float calibrationOffset, AppSettings appSettings) { using (var trans = realm.BeginWrite()) { measure.CalibrationOffset = calibrationOffset; measure.GlucoseLevelMGDL = (float)Math.Round((decimal)measure.GlucoseLevelRaw / 10) + calibrationOffset; measure.GlucoseLevelMMOL = (float)FreeStyleLibreUtils.ConvertMGDLToMMolPerLiter(appSettings, measure.GlucoseLevelMGDL); trans.Commit(); } return(measure); }
/// <summary> /// This methods executes the pairing actions /// </summary> private async Task <bool> PairDeviceAsync(Models.Device device) { ComputeScanningPropertiesIndicators(ScanningWizardState.Pairing); availableDevices.Clear(); await Task.Delay(1500); this.HasConnected = await MeasureService.RegisterDeviceAsync(device); await Task.Delay(500); if (HasConnected) { this.HasInitializedDevice = await MeasureService.InitializeBLEServicesAsync(device, MiaoMiaoProtocol.UART_SERVICE_ID); } await Task.Delay(500); if (this.HasInitializedDevice) { this.HasSubscribed = await MeasureService.SubsrcibeCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_TX); } await Task.Delay(500); if (this.HasSubscribed) { // MIAOMIAO PROTOCOL List <byte[]> resetPacket = new List <byte[]>(); resetPacket.Add(FreeStyleLibreUtils.GenerateResetPacket()); this.HasReadFirstMeasure = await MeasureService.WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, resetPacket); } await Task.Delay(1500); bool HasReadCorrectData = true; if (this.HasReadFirstMeasure) { HasReadCorrectData = MeasureService.CurrentState() != Models.Enums.MeasureServiceState.REFUSED_DATA_THEN_WAIT; } return(this.HasConnected && this.HasInitializedDevice && this.HasSubscribed && this.HasReadFirstMeasure && HasReadCorrectData); }
/// <summary> /// This method is in charge of awake the device and connects to it /// NOTE : If an error is raised during its execution, this method calls itself (the called is post delayed) /// </summary> /// <returns></returns> private async Task WakeUpMeasureServiceAsync() { Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: called"); bool needToPostDelayExecution = false; User currentUser = this.userRepository.GetCurrentUser(); if (!currentUser.DeviceIsBounded) { Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: no device bounded. No need to awake the mesure service"); return; } Models.Device device = new Models.Device(currentUser.DeviceId, currentUser.DeviceName); try { bool IsDeviceConnected = true; bool IsInitialized = true; bool IsSuscribed = true; bool IsWritten = true; bool IsRead = true; // step 1 : register device IsDeviceConnected = await this.measureService.RegisterDeviceAsync(device); // step 2 : initializes ble services if (IsDeviceConnected) { IsInitialized = await this.measureService.InitializeBLEServicesAsync(device, MiaoMiaoProtocol.UART_SERVICE_ID); } // step 3 : subscription if (IsInitialized) { IsSuscribed = await this.measureService.SubsrcibeCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_TX); } // step 4 : reset // MIAOMIAO PROTOCOL if (IsSuscribed) { List <byte[]> resetPacket = new List <byte[]>(); resetPacket.Add(FreeStyleLibreUtils.GenerateResetPacket()); IsWritten = await this.measureService.WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, resetPacket); } // step 5 : the state of the service if (IsWritten) { IsRead = this.measureService.CurrentState() != Models.Enums.MeasureServiceState.REFUSED_DATA_THEN_WAIT; } // if there was an error in of the steps, we start over if (!IsDeviceConnected || !IsInitialized || !IsSuscribed || !IsWritten || !IsRead) { needToPostDelayExecution = true; this.measureService.UnregisterDevice(device); Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: awakening is incomplete"); } else { Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: awakening is complete"); } } catch (CharacteristicReadException exception) { Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: " + exception); if (this.measureService != null && device != null) { this.measureService.UnregisterDevice(device); } needToPostDelayExecution = true; } finally { // post delay the execution of this method in X minutes because something went wrong if (needToPostDelayExecution) { await Task.Delay(this.appSettings.MEASURE_SERVICE_RETRY_DEFAULT_TIME * 1000 * 60).ContinueWith(t => Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { WakeUpMeasureServiceAsync(); }) ); } } }