public void CreateFreezingAlarmTest()
        {
            //GIVEN
            Telemetry telemetry = new Telemetry
            {
                Id             = "2",
                DeviceId       = "2",
                OccuredAt      = DateTime.UtcNow,
                BatteryVoltage = 0.9,
                Pressure       = 99800,
                Humidity       = 42,
                Temperature    = 0
            };

            DateTime dateStart = DateTime.UtcNow;
            DateTime dateEnd   = new DateTime();

            Mock <IDeviceRepository> deviceRepo = new Mock <IDeviceRepository>();
            Mock <IAlarmRepository>  alarmRepo  = new Mock <IAlarmRepository>();

            //WHEN
            AlarmService service = new AlarmService(deviceRepo.Object, alarmRepo.Object);

            service.CreateFreezingAlarm(telemetry, "site1", FreezeForecast.FreezingProbability.IMMINENT);

            deviceRepo.Verify(o => o.AddAlarm("2", It.Is <Alarm>(e =>
                                                                 e.AlarmGravity == Alarm.Gravity.Critical &&
                                                                 e.AlarmType == Alarm.Type.FreezeWarning &&
                                                                 e.ShortDescription.ToLower().Contains("gel") &&
                                                                 e.Description.ToLower().Contains("gel"))), Times.Once);
        }
        public static async Task Run([EventHubTrigger("device-data", Connection = "EventHubConnectionString")] string myEventHubMessage, TraceWriter log)
        {
            log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
            DependencyInjection.ConfigureInjection(log);
            IEnumerable <Telemetry> telemetries = null;

            try
            {
                telemetries = FrameParser.Parse(myEventHubMessage);
                if (!telemetries.Any())
                {
                    return;
                }
            }
            catch (FormatException e)
            {
                log.Error($"Error on parsing frame", e);
                return;
            }

            try
            {
                using (var scope = DependencyInjection.Container.BeginLifetimeScope())
                {
                    DeviceService      deviceService    = scope.Resolve <DeviceService>();
                    AlarmService       alarmService     = scope.Resolve <AlarmService>();
                    TelemetryService   telemetryService = scope.Resolve <TelemetryService>();
                    FreezingAlgorithme algorithme       = scope.Resolve <FreezingAlgorithme>();

                    telemetryService.InsertTelemetries(telemetries);
                    string siteId = deviceService.GetSiteId(telemetries.First().DeviceId);
                    log.Info($"Telemetries inserted");

                    Task <FreezeForecast> forecast = algorithme.Execute(telemetries.Last());
                    log.Info($"FreezeForecast executed");

                    log.Info($"Create humidity alarm ...");
                    alarmService.CreateHumidityAlarm(telemetries.Last(), siteId);
                    log.Info($"Create temperature alarm ...");
                    alarmService.CreateTemperatureAlarm(telemetries.Last(), siteId);
                    log.Info($"Create battery alarm ...");
                    alarmService.CreateBatteryAlarm(telemetries.Last(), siteId);

                    FreezeForecast freeze = await forecast;
                    if (freeze.FreezingStart.HasValue && Freeze(freeze.FreezingProbabilityList.FirstOrDefault().Value))
                    {
                        log.Info($"Create freeze alarm (if not already active)...");
                        alarmService.CreateFreezingAlarm(telemetries.Last(), siteId, freeze.FreezingProbabilityList.FirstOrDefault().Value);
                    }
                    else if (!Freeze(freeze.FreezingProbabilityList.FirstOrDefault().Value))
                    {
                        log.Info($"Set latest freeze alarm as inactive ...");
                        alarmService.SetFreezeAlarmAsInactive(telemetries.Last().DeviceId);
                    }

                    log.Info($"Remove CommunicationFail alarm ...");
                    alarmService.CheckForActiveCommunicationFailureAlarms(telemetries.First().DeviceId);

                    log.Info($"Update last communication date ...");
                    deviceService.UpdateLastCommunication(telemetries.Last().DeviceId, DateTime.UtcNow);
                }
            }
            catch (Exception e)
            {
                log.Error($"Error on processing telemetries", e);
                throw;
            }
        }