public void StartListening(GapDetection onGapDetection) { if (client != null) { return; } client = new HttpClient { BaseAddress = new Uri(cameraId) }; client.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); customEvent = onGapDetection; GapDetectionEvent += customEvent; // and notify the listener try { gapDetectionTimer = ThreadPoolTimer.CreatePeriodicTimer(BackgoundListen, timerPeriod); } catch (Exception e) { Debug.WriteLine($"{e.Message}"); } }
public void StopListening() { try { if (client == null) { return; } GapDetectionEvent -= customEvent; customEvent = null; client = null; gapDetectionTimer?.Cancel(); } catch (Exception e) { Debug.WriteLine($"{e.Message}"); } }
public async void StartListening(GapDetection detectionEvent) { if (gapDetectionEvent != null) { return; } try { var connectionString = new EventHubsConnectionStringBuilder( new Uri(GapConfig.EventHubsCompatibleEndpoint), GapConfig.EventHubsCompatiblePath, GapConfig.IotHubSasKeyName, GapConfig.IotHubSasKey); eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString()); // Create a PartitionReciever for each partition on the hub. var runtimeInfo = await eventHubClient.GetRuntimeInformationAsync(); var d2cPartitions = runtimeInfo.PartitionIds; // TODO: listening on partition 0 only. receivers = d2cPartitions .Select(d2c => eventHubClient.CreateReceiver("$Default", d2c, EventPosition.FromEnqueuedTime(DateTime.Now))) .ToList(); gapTimer = ThreadPoolTimer.CreatePeriodicTimer(BackgroundListen, timerPeriod); GapDetectionEvent += detectionEvent; gapDetectionEvent = detectionEvent; } catch (Exception e) { Debug.WriteLine(e.ToString()); } }
public void StopListening() { try { if (gapDetectionEvent == null) { return; } gapDetectionEvent -= gapDetectionEvent; receivers.ForEach(r => r.Close()); eventHubClient.Close(); eventHubClient = null; gapDetectionEvent = null; gapTimer?.Cancel(); } catch (Exception e) { Debug.WriteLine($"{e.Message}"); } }