public static async Task Run([TimerTrigger("0 0 18 * * *")] TimerInfo myTimer, TraceWriter log)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
            DependencyInjection.ConfigureInjection(log);

            using (var scope = DependencyInjection.Container.BeginLifetimeScope())
            {
                FreezingAlgorithme  algorithme          = scope.Resolve <FreezingAlgorithme>();
                DeviceService       deviceService       = scope.Resolve <DeviceService>();
                NotificationService notificationService = scope.Resolve <NotificationService>();
                FreezeService       freezeService       = scope.Resolve <FreezeService>();
                AlarmService        alarmService        = scope.Resolve <AlarmService>();

                OpenWeatherMapClient weatherClient = scope.Resolve <OpenWeatherMapClient>();

                IList <Alarm> alarms = new List <Alarm>();

                try
                {
                    log.Info("Get latest telemtry");
                    Dictionary <Device, Telemetry> telemetries = deviceService.GetLatestTelemetryByDevice();
                    foreach (var item in telemetries)
                    {
                        OwmCurrentWeather current = await weatherClient.GetCurrentWeather(item.Key.Position.Latitude, item.Key.Position.Longitude);

                        OwmForecastWeather forecast = await weatherClient.GetForecastWeather(item.Key.Position.Latitude, item.Key.Position.Longitude);

                        log.Info($"Execute Algorithme (device {item.Key.Id})");
                        FreezeForecast freeze = await algorithme.Execute(item.Value, item.Key, current.Weather, forecast.Forecast, forecast.StationPosition);

                        if (freeze == null)
                        {
                            log.Error($"Unable to calculate the freeze probability (no forecast)");
                            continue;
                        }
                        // TODO : complete process
                        Dictionary <DateTime, FreezingProbability> averageFreezePrediction12h = freezeService.CalculAverageFreezePrediction12h(freeze.FreezingProbabilityList);
                        log.Info($"Create alarms");
                        alarmService.CreateFreezeAlarm(item.Key.Id, item.Key.SiteId, averageFreezePrediction12h);
                        log.Info($"Insert Freeze in Db");
                        freezeService.CreateFreezeAndThawByDevice(item.Key.Id, averageFreezePrediction12h);
                    }

                    notificationService.SendNotifications(alarms);
                    log.Info($"Notifications sent at: {DateTime.Now}");
                }
                catch (AlgorithmeException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    log.Error(e.Message, e);
                    throw;
                }
            }
        }
Example #2
0
        public void Owm_GetCurrentWeather()
        {
            var weather = client.GetCurrentWeather(45.1822, 5.7275).Result;

            Check.That(weather.Weather).IsNotNull()
            .And.IsInstanceOf <OwmWeather>();
            Check.That(weather.StationPosition).IsNotNull()
            .And.IsInstanceOf <OwmStationPosition>();
        }