Skip to content

Commit 30abc55

Browse files
committed
SimpleWeatherService: Generate forecast data
1 parent bb18300 commit 30abc55

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

main.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,19 +802,27 @@ class Framework {
802802
}
803803

804804
void generate_weather_data(bool clear) {
805-
static int iconId = -1;
806805
if (clear) {
807806
systemTask.nimble().weather().SetCurrentWeather(0, 0, 0);
807+
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
808+
systemTask.nimble().weather().SetForecast(0, days);
808809
return;
809810
}
810811
auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
811812
srand((int)timestamp);
812-
int temperature = (rand() % 81 - 40) * 100;
813-
iconId++;
814-
if (iconId > 8) {
815-
iconId = 0;
813+
814+
// Generate current weather data
815+
int16_t temperature = (rand() % 81 - 40) * 100;
816+
systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, rand() % 9);
817+
818+
// Generate forecast data
819+
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
820+
for (int i = 0; i < Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
821+
days[i] = Pinetime::Controllers::SimpleWeatherService::Forecast::Day {
822+
(int16_t)(temperature - rand() % 10 * 100), (int16_t)(temperature + rand() % 10 * 100), Pinetime::Controllers::SimpleWeatherService::Icons(rand() % 9)
823+
};
816824
}
817-
systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, iconId);
825+
systemTask.nimble().weather().SetForecast((uint64_t)timestamp, days);
818826
}
819827

820828
void handle_touch_and_button() {

sim/components/ble/SimpleWeatherService.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t tempera
7777
printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId);
7878
}
7979

80+
void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days) {
81+
forecast = SimpleWeatherService::Forecast {timestamp, SimpleWeatherService::MaxNbForecastDays, days};
82+
for (int i = 0; i < SimpleWeatherService::MaxNbForecastDays; i++) {
83+
printf("forecast: day=%d. min=%d, max=%d icon=%d\n", i, days[i].minTemperature, days[i].maxTemperature, days[i].iconId);
84+
}
85+
}
86+
8087
int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
8188

8289
return 0;
@@ -115,3 +122,17 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService
115122
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature &&
116123
std::strcmp(this->location.data(), other.location.data()) == 0;
117124
}
125+
126+
bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const {
127+
return this->iconId == other.iconId &&
128+
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature;
129+
}
130+
131+
bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const {
132+
for (int i = 0; i < this->nbDays; i++) {
133+
if (this->days[i] != other.days[i]) {
134+
return false;
135+
}
136+
}
137+
return this->timestamp == other.timestamp && this->nbDays == other.nbDays;
138+
}

sim/components/ble/SimpleWeatherService.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class SimpleWeatherService {
2727
explicit SimpleWeatherService(const DateTime& dateTimeController);
2828

2929
void Init();
30-
void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId);
3130

3231
int OnCommand(struct ble_gatt_access_ctxt* ctxt);
3332

@@ -81,11 +80,18 @@ class SimpleWeatherService {
8180
int16_t minTemperature;
8281
int16_t maxTemperature;
8382
Icons iconId;
83+
84+
bool operator==(const Day& other) const;
8485
};
8586

8687
std::array<Day, MaxNbForecastDays> days;
88+
89+
bool operator==(const Forecast& other) const;
8790
};
8891

92+
void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId);
93+
void SetForecast(uint64_t timestamp, std::array<Forecast::Day, MaxNbForecastDays> days);
94+
8995
std::optional<CurrentWeather> Current() const;
9096
std::optional<Forecast> GetForecast() const;
9197

0 commit comments

Comments
 (0)