Esempio n. 1
0
        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, config.MeasureNameFilter);

            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());

                    List <SensorDataContract> alertsToSQl = new List <SensorDataContract>();

                    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());

                                string eventJSON = OutputResults(key, historicData[key].LastOrDefault(), alert);
                                alertEventHub.Send(
                                    new EventData(Encoding.UTF8.GetBytes(eventJSON)));

                                alertLastTime        = alert.Time;
                                alertLastTimes[@key] = alertLastTime;

                                if (historicData[key].Length > 0)
                                {
                                    alertsToSQl.Add(historicData[key].Last());
                                    if (_blobWriter != null)
                                    {
                                        _blobWriter.WriteLine(eventJSON);
                                    }
                                }
                            }
                        }
                    }

                    if (_sqlOutputRepository != null)
                    {
                        _sqlOutputRepository.ProcessEvents(alertsToSQl);
                    }
                    if (_blobWriter != null)
                    {
                        _blobWriter.Flush();
                    }
                }
                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();
        }