Skip to content

Commit

Permalink
SimpleWeatherService: Add sunrise and sunset data
Browse files Browse the repository at this point in the history
  • Loading branch information
vkareh committed Sep 24, 2024
1 parent 7989538 commit f906b1d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
31 changes: 28 additions & 3 deletions sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuf
SimpleWeatherService::Location cityName;
std::memcpy(cityName.data(), &dataBuffer[16], 32);
cityName[32] = '\0';
uint64_t sunrise = 0;
uint64_t sunset = 0;
if (dataBuffer[1] > 0) {
sunrise = ToUInt64(&dataBuffer[49]);
sunset = ToUInt64(&dataBuffer[57]);
}
return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]),
ToInt16(&dataBuffer[10]),
ToInt16(&dataBuffer[12]),
ToInt16(&dataBuffer[14]),
SimpleWeatherService::Icons {dataBuffer[16 + 32]},
std::move(cityName));
std::move(cityName),
sunrise,
sunset);
}

SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
Expand Down Expand Up @@ -73,7 +81,7 @@ void SimpleWeatherService::Init() {
void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId) {
SimpleWeatherService::Location cityName;
cityName[32] = '\0';
currentWeather = SimpleWeatherService::CurrentWeather((uint64_t)timestamp, temperature, temperature, temperature, SimpleWeatherService::Icons(iconId), std::move(cityName));
currentWeather = SimpleWeatherService::CurrentWeather((uint64_t)timestamp, temperature, temperature, temperature, SimpleWeatherService::Icons(iconId), std::move(cityName), (uint64_t)timestamp, (uint64_t)timestamp);
printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId);
}

Expand Down Expand Up @@ -117,10 +125,27 @@ std::optional<SimpleWeatherService::Forecast> SimpleWeatherService::GetForecast(
return {};
}

bool SimpleWeatherService::IsNight() const {
if (currentWeather) {
auto currentTime = dateTimeController.CurrentDateTime().time_since_epoch();

auto sunriseSecond = std::chrono::seconds {currentWeather->sunrise};
auto sunsetSecond = std::chrono::seconds {currentWeather->sunset};

auto sunrise = std::chrono::duration_cast<std::chrono::seconds>(sunriseSecond);
auto sunset = std::chrono::duration_cast<std::chrono::seconds>(sunsetSecond);

return currentTime < sunrise || currentTime > sunset;
}

return false;
}

bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService::CurrentWeather& other) const {
return this->iconId == other.iconId && this->temperature == other.temperature && this->timestamp == other.timestamp &&
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature &&
std::strcmp(this->location.data(), other.location.data()) == 0;
std::strcmp(this->location.data(), other.location.data()) == 0 &&
this->sunrise == other.sunrise && this->sunset == other.sunset;
}

bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const {
Expand Down
12 changes: 10 additions & 2 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ class SimpleWeatherService {
int16_t minTemperature,
int16_t maxTemperature,
Icons iconId,
Location&& location)
Location&& location,
uint64_t sunrise,
uint64_t sunset)
: timestamp {timestamp},
temperature {temperature},
minTemperature {minTemperature},
maxTemperature {maxTemperature},
iconId {iconId},
location {std::move(location)} {
location {std::move(location)},
sunrise {sunrise},
sunset {sunset} {
}

uint64_t timestamp;
Expand All @@ -68,6 +72,8 @@ class SimpleWeatherService {
int16_t maxTemperature;
Icons iconId;
Location location;
uint64_t sunrise;
uint64_t sunset;

bool operator==(const CurrentWeather& other) const;
};
Expand Down Expand Up @@ -95,6 +101,8 @@ class SimpleWeatherService {
std::optional<CurrentWeather> Current() const;
std::optional<Forecast> GetForecast() const;

bool IsNight() const;

static int16_t CelsiusToFahrenheit(int16_t celsius) {
return celsius * 9 / 5 + 3200;
}
Expand Down

0 comments on commit f906b1d

Please sign in to comment.