protected override void StopListening() { // disconnect the contact event for non-contact probes. this probe has already been marked as non-running, so // there's no risk of a race condition in which the contact state changes to worn and the change event attempts // to restart this probe after it has been stopped (we check Running during the change event). if (!(this is MicrosoftBandContactProbe)) { MicrosoftBandContactProbe contactProbe = Protocol.Probes.Single(probe => probe is MicrosoftBandContactProbe) as MicrosoftBandContactProbe; contactProbe.ContactStateChanged -= ContactStateChanged; } StopReadings(); // only cancel the static health test if none of the band probes should be running. if (BandProbesThatShouldBeRunning.Count == 0) { CancelHealthTest(); } // disconnect the client if no band probes are actually running. if (BandProbesThatAreRunning.Count == 0 && (BandClient?.IsConnected ?? false)) { try { SensusServiceHelper.Get().Logger.Log("All Band probes have stopped. Disconnecting client.", LoggingLevel.Normal, GetType()); BandClient.DisconnectAsync().Wait(); BandClient = null; } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to disconnect client: " + ex.Message, LoggingLevel.Normal, GetType()); } } }
protected override void StartListening() { // we expect this probe to start successfully, but an exception may occur if no bands are paired with the device of if the // connection with a paired band fails. so schedule a static repeating callback to check on all band probes and restart them // if needed/possible. this is better than a non-static callback for each band probe because there are many band probes and // the callbacks would be redundant, frequent, and power-hungry. lock (HEALTH_TEST_LOCKER) { // only schedule the callback if we haven't done so already. the callback should be global across all band probes. if (HEALTH_TEST_CALLBACK == null) { // the band health test is static, so it has no domain other than sensus. HEALTH_TEST_CALLBACK = new ScheduledCallback(TestBandClientAsync, "BAND-HEALTH-TEST", null, null, HEALTH_TEST_TIMEOUT); SensusContext.Current.CallbackScheduler.ScheduleRepeatingCallback(HEALTH_TEST_CALLBACK, HEALTH_TEST_DELAY, HEALTH_TEST_DELAY, false); } } // hook up the contact event for non-contact probes. need to do this before the calls below because they might throw // a band connect exception. such an exception will leave the probe in a running state in anticipation that the user // might pair a band later. the band health test will start readings later for all band probes, but it will not use // Start to do so (it will simply start the readings). so this is our only chance to hook up the contact event. if (!(this is MicrosoftBandContactProbe)) { MicrosoftBandContactProbe contactProbe = Protocol.Probes.Single(probe => probe is MicrosoftBandContactProbe) as MicrosoftBandContactProbe; contactProbe.ContactStateChanged += ContactStateChanged; } ConnectClient(); Configure(BandClient); StartReadings(); }