コード例 #1
0
        public async Task <AirPollution> UpdateAirPollutionCache(int cityId)
        {
            var airPollutionFromJson = await _weatherApiService.GetAirPollutionnAsync(cityId);

            var airPollutionToReplace = new AirPollution(airPollutionFromJson);

            return(ReplaceAndRetrunIfExpiredOrMissing <AirPollution>(_airPolution, cityId, airPollutionToReplace));
        }
コード例 #2
0
ファイル: TwitterPush.cs プロジェクト: MihaMarkic/Cyanometer
        public async Task PushAsync(AirPollution pollution, Measurement chief, DateTime date, CancellationToken ct)
        {
            if (!settings.IsTwitterEnabled)
            {
                return;
            }
            logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Updating twitter").Commit();
            try
            {
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Loading previous tweet").Commit();
                string tweetPath = Path.Combine(lastDataDirectory, "tweet.txt");
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Loading credentials").Commit();
                if (fileService.FileExists(tweetPath))
                {
                    try
                    {
                        lastStatus = fileService.GetAllText(tweetPath);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError().WithCategory(LogCategory.AirQuality).WithMessage("Failed loading last tweet's text").WithException(ex).Commit();
                    }
                }
                string text = $"Onesnaženost je {PollutionToSlovene(pollution)}.";
                if (pollution != AirPollution.Low)
                {
                    text += $" Glavni krivec je {chief}.";
                }
                text += $" Zadnja meritev ob {date:HH:mm}";
                if (!string.Equals(lastStatus, text))
                {
                    var token = Tokens.Create(settings.TwitterConsumerKey, settings.TwitterConsumerSecret,
                                              settings.TwitterAccessToken, settings.TwitterAccessTokenSecret);
                    var response = await token.Statuses.UpdateAsync(status : text, cancellationToken : ct);

                    lastStatus = text;
                    try
                    {
                        await fileService.WriteFileAsync(tweetPath, lastStatus, ct);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError().WithCategory(LogCategory.AirQuality).WithMessage("Failed saving last tweet's text").WithException(ex).Commit();
                    }
                    logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Twitter updated status to '{text}' with response {response}").Commit();
                }
                else
                {
                    logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Status didn't change, won't push.").Commit();
                }
            }
            catch (Exception ex)
            {
                logger.LogError().WithCategory(LogCategory.AirQuality).WithMessage("Failed updating twitter").WithException(ex).Commit();
            }
        }
コード例 #3
0
        public AirQualityPollutionCalculated Calculate(AirQualityData data)
        {
            var          pollutions          = CalculatePollution(data);
            var          calculatedPollution = CalculateMaxPollution(pollutions);
            AirPollution pollution           = calculatedPollution.Pollution;
            Measurement  chief         = GetChiefPolluter(pollutions);
            string       pollutionInfo = $"Max pollution is {pollution} with index {calculatedPollution.Index:0} coming from {chief}";

            logger.LogInformation(pollutionInfo);
            logger.LogInformation("Air quality processor done");
            return(new AirQualityPollutionCalculated(data, pollution, chief));
        }
コード例 #4
0
        public static Lights PollutionToLights(AirPollution pollution)
        {
            switch (pollution)
            {
            case AirPollution.Low:
                return(Lights.Eight);

            case AirPollution.Mid:
                return(Lights.Seven);

            case AirPollution.High:
                return(Lights.Six);

            default:
                return(Lights.Five);
            }
        }
コード例 #5
0
ファイル: TwitterPush.cs プロジェクト: MihaMarkic/Cyanometer
        public static string PollutionToSlovene(AirPollution pollution)
        {
            switch (pollution)
            {
            case AirPollution.Mid:
                return("srednja");

            case AirPollution.High:
                return("visoka");

            case AirPollution.VeryHigh:
                return("zelo visoka");

            default:
                return("nizka");
            }
        }
コード例 #6
0
 public AirQualityPollutionCalculated(AirQualityData data, AirPollution pollutionWeight, Measurement chiefPollutant)
 {
     Data            = data;
     PollutionWeight = pollutionWeight;
     ChiefPollutant  = chiefPollutant;
 }
コード例 #7
0
        public async Task <bool> LoopAsync(CancellationToken ct)
        {
            AirQualityPersisted state;

            logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Checking for state at {lastDataPath}").Commit();
            if (file.FileExists(lastDataPath))
            {
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Loading state").Commit();
                XElement root = XElement.Parse(file.GetAllText(lastDataPath));
                state = arso.XElementToPersisted(root);
            }
            else
            {
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Creating new state").Commit();
                state = new AirQualityPersisted();
            }
            DateTime?latestDate = state.NewestDate;
            var      data       = await arso.GetIndexAsync(ct);

            arso.UpdatePersisted(data, state);
            XElement element = arso.PersistedToXElement(state);

            logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Saving air quality state").Commit();
            await file.WriteFileAsync(lastDataPath, element.ToString(), ct);

            var          pollutions          = CalculatePollutions(data.Date, state);
            var          calculatedPollution = CalculateMaxPollution(pollutions);
            AirPollution pollution           = calculatedPollution.Pollution;
            Measurement  chief         = GetChiefPolluter(pollutions);
            string       pollutionInfo = $"Max pollution is {pollution} with index {calculatedPollution.Index:0} coming from {chief}";

            logger.LogInfo().WithCategory(LogCategory.AirQuality)
            .WithMessage(pollutionInfo).Commit();
            if (settings.AirQualityLightsEnabled)
            {
                Lights light = PollutionToLights(pollution);
                if (pollution != AirPollution.Low)
                {
                    light |= ChiefPolluterToLights(chief);
                }
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Calculated pollution is {pollution} using light {light}").Commit();
                shiftRegister.EnableLight(light);
            }
            else
            {
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Lights are disabled").Commit();
            }
            DateTime?newestDate = state.NewestDate;

            if (settings.AirQualityUploadEnabled)
            {
                await twitterPush.PushAsync(pollution, chief, state.NewestDate ?? DateTime.MinValue, ct);
            }
            else
            {
                logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Notification upload is disabled").Commit();
            }
            //ExceptionlessClient.Default.SubmitLog(nameof(AirQualityProcessor), pollutionInfo, Exceptionless.Logging.LogLevel.Info);
            logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage("Air quality processor done").Commit();
            return(true);
        }