public void UpdatePersisted(AirQualityData data, AirQualityPersisted persisted) { if (data.NO2.HasValue) { persisted.NO2 = new AirQualityValue { LastDate = data.Date, Value = data.NO2.Value }; } if (data.PM10.HasValue) { persisted.PM10 = new AirQualityValue { LastDate = data.Date, Value = data.PM10.Value }; } if (data.SO2.HasValue) { persisted.SO2 = new AirQualityValue { LastDate = data.Date, Value = data.SO2.Value }; } if (data.O3.HasValue) { persisted.O3 = new AirQualityValue { LastDate = data.Date, Value = data.O3.Value }; } if (data.CO.HasValue) { persisted.CO = new AirQualityValue { LastDate = data.Date, Value = data.CO.Value }; } }
public XElement PersistedToXElement(AirQualityPersisted data) { return(new XElement("ArsoPersisted", ValueToXElement(nameof(data.PM10), data.PM10), ValueToXElement(nameof(data.SO2), data.SO2), ValueToXElement(nameof(data.O3), data.O3), ValueToXElement(nameof(data.NO2), data.NO2), ValueToXElement(nameof(data.CO), data.CO) )); }
public CalculatedPollution[] CalculatePollutions(DateTime date, AirQualityPersisted state) { logger.LogInfo().WithCategory(LogCategory.AirQuality).WithMessage($"Calculating pollutions").Commit(); CalculatedPollution[] pollution = new CalculatedPollution[] { CalculatePollution(date, Measurement.PM10, state.PM10, CalculatePM10PollutionIndex(state.PM10?.Value ?? 0)), CalculatePollution(date, Measurement.O3, state.O3, CalculateO3PollutionIndex(state.O3?.Value ?? 0)), CalculatePollution(date, Measurement.NO2, state.NO2, CalculateNO2PollutionIndex(state.NO2?.Value ?? 0)), CalculatePollution(date, Measurement.SO2, state.SO2, CalculateSO2PollutionIndex(state.SO2?.Value ?? 0)) }; return(pollution); }
public AirQualityPersisted XElementToPersisted(XElement root) { AirQualityPersisted result = new AirQualityPersisted { NO2 = XElementToNode(root.Element(nameof(result.NO2))), PM10 = XElementToNode(root.Element(nameof(result.PM10))), SO2 = XElementToNode(root.Element(nameof(result.SO2))), O3 = XElementToNode(root.Element(nameof(result.O3))), CO = XElementToNode(root.Element(nameof(result.CO))) }; return(result); }
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); }