Author: FintasticMan <finlay.neon.kid@gmail.com>
weather: Fix GetCurrent* functions returning future events (#1879)
src/components/ble/weather/WeatherService.cpp | 27 +++++++++++++------
diff --git a/src/components/ble/weather/WeatherService.cpp b/src/components/ble/weather/WeatherService.cpp index 513bb2a16ba81d1e36546a2ff9d9ffca2434f443..b9a6af556c1e4757dc4b05661e9b40100824b261 100644 --- a/src/components/ble/weather/WeatherService.cpp +++ b/src/components/ble/weather/WeatherService.cpp @@ -404,7 +404,8 @@ std::unique_ptr<WeatherData::Clouds>& WeatherService::GetCurrentClouds() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Clouds && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Clouds && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Clouds>&>(header); } } @@ -415,7 +416,8 @@ std::unique_ptr<WeatherData::Obscuration>& WeatherService::GetCurrentObscuration() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Obscuration && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Obscuration && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Obscuration>&>(header); } } @@ -426,7 +428,8 @@ std::unique_ptr<WeatherData::Precipitation>& WeatherService::GetCurrentPrecipitation() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Precipitation && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Precipitation && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Precipitation>&>(header); } } @@ -437,7 +440,8 @@ std::unique_ptr<WeatherData::Wind>& WeatherService::GetCurrentWind() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Wind && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Wind && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Wind>&>(header); } } @@ -448,7 +452,8 @@ std::unique_ptr<WeatherData::Temperature>& WeatherService::GetCurrentTemperature() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Temperature && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Temperature && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Temperature>&>(header); } } @@ -459,7 +464,8 @@ std::unique_ptr<WeatherData::Humidity>& WeatherService::GetCurrentHumidity() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Humidity && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Humidity && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Humidity>&>(header); } } @@ -470,7 +476,8 @@ std::unique_ptr<WeatherData::Pressure>& WeatherService::GetCurrentPressure() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Pressure && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Pressure && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Pressure>&>(header); } } @@ -481,7 +488,8 @@ std::unique_ptr<WeatherData::Location>& WeatherService::GetCurrentLocation() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::Location && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::Location && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::Location>&>(header); } } @@ -492,7 +500,8 @@ std::unique_ptr<WeatherData::AirQuality>& WeatherService::GetCurrentQuality() { uint64_t currentTimestamp = GetCurrentUnixTimestamp(); for (auto&& header : this->timeline) { - if (header->eventType == WeatherData::eventtype::AirQuality && IsEventStillValid(header, currentTimestamp)) { + if (header->eventType == WeatherData::eventtype::AirQuality && currentTimestamp >= header->timestamp && + IsEventStillValid(header, currentTimestamp)) { return reinterpret_cast<std::unique_ptr<WeatherData::AirQuality>&>(header); } }