From 30abc55b6c46b33cbcd8541ed92357179d9c22b1 Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Thu, 1 Feb 2024 10:56:34 -0500 Subject: [PATCH] SimpleWeatherService: Generate forecast data --- main.cpp | 20 ++++++++++++++------ sim/components/ble/SimpleWeatherService.cpp | 21 +++++++++++++++++++++ sim/components/ble/SimpleWeatherService.h | 8 +++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 938b468..3e57a1b 100644 --- a/main.cpp +++ b/main.cpp @@ -802,19 +802,27 @@ public: } void generate_weather_data(bool clear) { - static int iconId = -1; if (clear) { systemTask.nimble().weather().SetCurrentWeather(0, 0, 0); + std::array days; + systemTask.nimble().weather().SetForecast(0, days); return; } auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); srand((int)timestamp); - int temperature = (rand() % 81 - 40) * 100; - iconId++; - if (iconId > 8) { - iconId = 0; + + // Generate current weather data + int16_t temperature = (rand() % 81 - 40) * 100; + systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, rand() % 9); + + // Generate forecast data + std::array days; + for (int i = 0; i < Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { + days[i] = Pinetime::Controllers::SimpleWeatherService::Forecast::Day { + (int16_t)(temperature - rand() % 10 * 100), (int16_t)(temperature + rand() % 10 * 100), Pinetime::Controllers::SimpleWeatherService::Icons(rand() % 9) + }; } - systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, iconId); + systemTask.nimble().weather().SetForecast((uint64_t)timestamp, days); } void handle_touch_and_button() { diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp index ffc881c..024545b 100644 --- a/sim/components/ble/SimpleWeatherService.cpp +++ b/sim/components/ble/SimpleWeatherService.cpp @@ -77,6 +77,13 @@ void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t tempera printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId); } +void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array days) { + forecast = SimpleWeatherService::Forecast {timestamp, SimpleWeatherService::MaxNbForecastDays, days}; + for (int i = 0; i < SimpleWeatherService::MaxNbForecastDays; i++) { + printf("forecast: day=%d. min=%d, max=%d icon=%d\n", i, days[i].minTemperature, days[i].maxTemperature, days[i].iconId); + } +} + int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { return 0; @@ -115,3 +122,17 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature && std::strcmp(this->location.data(), other.location.data()) == 0; } + +bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const { + return this->iconId == other.iconId && + this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature; +} + +bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const { + for (int i = 0; i < this->nbDays; i++) { + if (this->days[i] != other.days[i]) { + return false; + } + } + return this->timestamp == other.timestamp && this->nbDays == other.nbDays; +} diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h index 43d9567..027518d 100644 --- a/sim/components/ble/SimpleWeatherService.h +++ b/sim/components/ble/SimpleWeatherService.h @@ -27,7 +27,6 @@ public: explicit SimpleWeatherService(const DateTime& dateTimeController); void Init(); - void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); int OnCommand(struct ble_gatt_access_ctxt* ctxt); @@ -81,11 +80,18 @@ public: int16_t minTemperature; int16_t maxTemperature; Icons iconId; + + bool operator==(const Day& other) const; }; std::array days; + + bool operator==(const Forecast& other) const; }; + void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); + void SetForecast(uint64_t timestamp, std::array days); + std::optional Current() const; std::optional GetForecast() const;