public static void Process() { _EventHubReader.Run(_Config.DeviceSBConnectionString, _Config.DeviceEHName, string.Empty); _EventHubReader.FailureEvent.WaitOne(); }
public static void Process(Configuration config) { var alertEventHub = EventHubClient.CreateFromConnectionString(config.AlertEHConnectionString, config.AlertEHName); Trace.TraceInformation("Starting to receive messages..."); _eventHubReader.Run(config.DeviceEHConnectionString, config.DeviceEHName); var timerInterval = TimeSpan.FromSeconds(1); var alertLastTimes = new Dictionary <string, DateTime>(); TimerCallback timerCallback = state => { var historicData = _eventHubReader.GetHistoricData(); try { var tasks = historicData.ToDictionary(kvp => kvp.Key, kvp => _analyzer.Analyze(kvp.Value)); Task.WaitAll(tasks.Values.ToArray()); foreach (var kvp in tasks) { var key = kvp.Key; var alerts = kvp.Value.Result; DateTime alertLastTime; if (!alertLastTimes.TryGetValue(@key, out alertLastTime)) { alertLastTime = DateTime.MinValue; } foreach (var alert in alerts) { if ((alert.Time - alertLastTime).TotalSeconds >= config.AlertsIntervalSec) { Trace.TraceInformation("Alert - {0}", alert.ToString()); alertEventHub.Send( new EventData(Encoding.UTF8.GetBytes( OutputResults(key, historicData[key].LastOrDefault(), alert)))); alertLastTime = alert.Time; alertLastTimes[@key] = alertLastTime; } } } } catch (Exception e) { #if DEBUG_LOG Trace.TraceError(e.Message); Trace.TraceError(e.ToString()); #endif //throw; } _timer.Change((int)timerInterval.TotalMilliseconds, Timeout.Infinite); }; _timer = new Timer(timerCallback, null, Timeout.Infinite, Timeout.Infinite); _timer.Change(0, Timeout.Infinite); Trace.TraceInformation("Reading events from Event Hub (press ctrl+c to abort)"); var exitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); }; int index = WaitHandle.WaitAny(new[] { exitEvent, _eventHubReader.FailureEvent }); Trace.TraceInformation("Exiting..."); _timer.Change(Timeout.Infinite, Timeout.Infinite); Thread.Sleep(timerInterval); _timer.Dispose(); _eventHubReader.Close(); }