Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleWeatherService: Add sunrise and sunset data #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading