private void WebView_Navigated(object sender, WebNavigatedEventArgs e) { try { //Action onFinish = new Action(() => { Navigation.PushAsync(new Page1()); Navigation.RemovePage(this); }); Action onFinish = new Action(() => { buttonContinue.IsVisible = true; }); var thread = new Thread(async() => { await Task.Delay(30 * 1000); //wait 30 seconds before start measuring TestMeViewModel b = new TestMeViewModel(); b.Progress = 0; MeasurementHandler.GetStressResult(-1, b); while (b.IsFinished == false) { if (b.StressResult.StartsWith("Error")) { //TODO: alert user and try again return; } } //int repetitionTime = MeasurementHandler.measureRepetitionTime; //DependencyService.Get<ISchedule>().ScheduleMeasurement(repetitionTime); //Schedule measurement every 6 minutes Xamarin.Forms.Device.BeginInvokeOnMainThread(onFinish); }); thread.Start(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public async Task <bool> readRRSensor(TestMeViewModel b, int sec) { Activity activity = Droid.MainActivity.instance; _contactSensor.StartReadings(); while (_bandState == null) { } //get the band state if (_bandState != BandContactState.Worn) { _bandState = null; //TODO: notify the user by sending a notification return(false); } _distancerSensor.StartReadings(); while (currentMotionTyp == null) { } if (currentMotionTyp == MotionType.Walking || currentMotionTyp == MotionType.Jogging || currentMotionTyp == MotionType.Running) { b.StressResult = "Error: can't measure during sports activity"; Log.Error("motion", "user is " + currentMotionTyp.ToString()); return(false); } _rrIntervalsReadings.Clear(); //RequestConsent(); _rrSensor.StartReadings(); await Task.Delay(sec * 1000); _rrSensor.StopReadings(); _contactSensor.StopReadings(); return(true); }
public TestMe(bool isSignup) { InitializeComponent(); BindingContext = new TestMeViewModel(); this.isSignup = isSignup; if (isSignup) { StartMeasure(-1); //auto start } }
public static async Task GetStressResult(int pseudo, TestMeViewModel b) { List <double> rrIntervals = null; bool bluetoothWasOn = false; if (pseudo < 0) { //turn on bluetooth if needed bluetoothWasOn = DependencyService.Get <IBluetooth>().TurnOn(); if (!bluetoothWasOn) { await Task.Delay(3 * 1000); } bool isBandConnected = DependencyService.Get <IBand>().ConnectToBand(b).Result; if (!isBandConnected) { b.StressResult = "Error: band isn't connected"; if (!bluetoothWasOn) { DependencyService.Get <IBluetooth>().TurnOff(); } return; //can't connect to band } b.StressResult = "Reading..."; await DependencyService.Get <IBand>().RequestConsent(); await DependencyService.Get <IBand>().readRRSensor(b, testTimeInSec); rrIntervals = DependencyService.Get <IBand>().RRIntervalReadings(); } if (rrIntervals != null && rrIntervals.Count == 0) { b.StressResult = "Error: can't read heart rate. make sure your band is worn and connected."; b.Progress = 1; if (!bluetoothWasOn) { DependencyService.Get <IBluetooth>().TurnOff(); } return; } Uri measurementUri = BuildMeasurementUri(rrIntervals, pseudo); bool success = await AzureFunctionAddMeasurement(measurementUri, b); if (!success) { StoreIntervalsToLocalMemory(measurementUri); } b.Progress = 1; if (!bluetoothWasOn) { DependencyService.Get <IBluetooth>().TurnOff(); } }
public async Task <bool> ConnectToBand(TestMeViewModel b) { try { if (_client == null) { //get all paired devices (we only care about the 1st) IBandInfo[] devices = BandClientManager.Instance.GetPairedBands(); if (devices.Length == 0) { if (b != null) { b.IsConnected = false; } Activity activity = Droid.MainActivity.instance; activity.RunOnUiThread(() => { Toast.MakeText(Droid.MainActivity.context, "make sure your band is paired with this device and try again", ToastLength.Long).Show(); }); return(false); } _client = BandClientManager.Instance.Create(Droid.MainActivity.context, devices[0]); } else if (_client.ConnectionState == ConnectionState.Connected) { if (b != null) { b.IsConnected = true; } return(true); } //connecting to device ConnectionState connectionState = await _client.ConnectTaskAsync(); if (b != null) { b.IsConnected = _client.IsConnected; } //update TestMeViewModel InitSensors(b); //register sensors listeners return(_client.IsConnected); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
private void StartMeasure(int pseudo) { TestMeViewModel b = (TestMeViewModel)BindingContext; b.PNN50 = 0; //don't make the UI thread wait var thread = new Thread( () => { MeasurementHandler.GetStressResult(pseudo, b); }); b.Progress = 0; thread.Start(); double msPass = 0; int testTime = MeasurementHandler.testTimeInSec; Device.StartTimer(TimeSpan.FromMilliseconds(50), () => { if (b.Progress < 1) { msPass += 50; b.Progress = msPass <= testTime * 1000 ? msPass / (testTime * 1000) : (msPass - 1) / msPass; if (b.StressResult.StartsWith("Error")) { return(false); } return(true); } if (isSignup) { if (b.StressResult.StartsWith("you")) //succeeded { Device.BeginInvokeOnMainThread(async() => { await Navigation.PushAsync(new Pages.SignupStressTest()); Navigation.RemovePage(this); //user can't go back}); }); return(false); } return(true); } else { return(false); } }); }
public async Task <int> getHR(TestMeViewModel b) { await ConnectToBand(b); InitSensors(b); Activity activity = Droid.MainActivity.instance; _contactSensor.StartReadings(); while (_bandState == null) { } if (_bandState != BandContactState.Worn) { _bandState = null; return(-1); } _hrSensor.StartReadings(); return(0); }
static async Task <bool> AzureFunctionAddMeasurement(Uri measurementUri, TestMeViewModel b) { var httpClient = new HttpClient(); try { b.StressResult = "Calculating..."; String result = await httpClient.GetStringAsync(measurementUri); result = result.Substring(1, result.Length - 2); //remove the " at the beginning and end b.StressResult = result; return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); b.StressResult = "Error: could not connect to Azure. Make sure your phone is connected to the Internet"; return(false); } }
public static async Task <bool> ResendIntervals() { //get the previous failed-to-send intervals uri Queue <Uri> previousCalls; if (Application.Current.Properties.ContainsKey("previousCalls")) { String serializedStack = Application.Current.Properties["previousCalls"].ToString(); previousCalls = JsonConvert.DeserializeObject <Queue <Uri> >(serializedStack); } else { previousCalls = new Queue <Uri>(); } //resend intervals Uri currentMeasurement; TestMeViewModel b = new TestMeViewModel(); while (previousCalls.Count > 0) { currentMeasurement = previousCalls.First(); //get the first without Dequeue bool success = await AzureFunctionAddMeasurement(currentMeasurement, b); if (success) { previousCalls.Dequeue(); //remove first and continue } else { break; //failed to send intervals again } } //store Application.Current.Properties["previousCalls"] = JsonConvert.SerializeObject(previousCalls); await Application.Current.SavePropertiesAsync(); return(previousCalls.Count == 0); //returns true if all previous intervals successfully sent }
//assumes the band is connected public async Task <int> getGSR(TestMeViewModel b, int sec) { Activity activity = Droid.MainActivity.instance; _contactSensor.StartReadings(); while (_bandState == null) { } if (_bandState != BandContactState.Worn) { _bandState = null; return(-1); } _gsrDone = false; _gsrReadings.Clear(); b.GsrList = "#"; _gsrSensor.StartReadings(); await Task.Delay(sec * 1000); _gsrDone = true; _contactSensor.StopReadings(); return(0); }
public void InitSensors(TestMeViewModel b) { if (!_client.IsConnected) { return; } _hrSensor = _hrSensor ?? _client.SensorManager.CreateHeartRateSensor(); _gsrSensor = _gsrSensor ?? _client.SensorManager.CreateGsrSensor(); _rrSensor = _rrSensor ?? _client.SensorManager.CreateRRIntervalSensor(); _contactSensor = _contactSensor ?? _client.SensorManager.CreateContactSensor(); _distancerSensor = _distancerSensor ?? _client.SensorManager.CreateDistanceSensor(); if (_contactSensor == null || _hrSensor == null || _gsrSensor == null || _rrSensor == null || _distancerSensor == null) { return; } Activity activity = Droid.MainActivity.instance; //register contact listener _contactSensor.ReadingChanged += (sender, e) => { var contactEvent = e.SensorReading; _bandState = contactEvent.ContactState; }; //register heart rate listener _hrSensor.ReadingChanged += (sender, e) => { activity.RunOnUiThread(() => { var heartRateEvent = e.SensorReading; //_hrReadings.Add(heartRateEvent.HeartRate); if (heartRateEvent.Quality == HeartRateQuality.Locked) { _hrSensor.StopReadings(); _contactSensor.StopReadings(); // if (b != null) { b.HR = _hrReadings[_hrReadings.Count - 1]; } //update ViewModel } if (_bandState != BandContactState.Worn) //user took off the band while reading { _hrSensor.StopReadings(); _contactSensor.StopReadings(); _bandState = null; return; } }); }; //register gsr listener _gsrSensor.ReadingChanged += (sender, e) => { activity.RunOnUiThread(() => { var gsrEvent = e.SensorReading; _gsrReadings.Add(gsrEvent.Resistance); if (b != null) { b.GsrList = gsrEvent.Resistance.ToString(); } if (_gsrDone) { _gsrSensor.StopReadings(); _contactSensor.StopReadings(); return; } }); }; //register RR Intervals listener _rrSensor.ReadingChanged += (sender, e) => { if (_bandState != BandContactState.Worn) //user took off the band while reading { _rrSensor.StopReadings(); _contactSensor.StopReadings(); _bandState = null; b.StressResult = "Error: band is not worn."; return; } var rrEvent = e.SensorReading; _rrIntervalsReadings.Add(rrEvent.Interval); }; _distancerSensor.ReadingChanged += (sender, e) => { currentMotionTyp = e.SensorReading.MotionType; _distancerSensor.StopReadings(); }; }