Skip to content

Commit

Permalink
SimpleWeatherService: Generate forecast data
Browse files Browse the repository at this point in the history
  • Loading branch information
vkareh committed Feb 7, 2024
1 parent 45a795e commit 3648ffe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
20 changes: 14 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,19 +802,27 @@ class Framework {
}

void generate_weather_data(bool clear) {
static int iconId = -1;
if (clear) {
systemTask.nimble().weather().SetCurrentWeather(0, 0, 0);
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> 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<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> 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() {
Expand Down
23 changes: 22 additions & 1 deletion sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ void SimpleWeatherService::Init() {
}

void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId) {
SimpleWeatherService::Location cityName;
SimpleWeatherService::Location cityName = (SimpleWeatherService::Location)"New York";
cityName[32] = '\0';
currentWeather = SimpleWeatherService::CurrentWeather((uint64_t)timestamp, temperature, temperature, temperature, SimpleWeatherService::Icons(iconId), std::move(cityName));
printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId);
}

void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> 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;
Expand Down Expand Up @@ -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;
}
8 changes: 7 additions & 1 deletion sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SimpleWeatherService {
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);

Expand Down Expand Up @@ -81,11 +80,18 @@ class SimpleWeatherService {
int16_t minTemperature;
int16_t maxTemperature;
Icons iconId;

bool operator==(const Day& other) const;
};

std::array<Day, MaxNbForecastDays> days;

bool operator==(const Forecast& other) const;
};

void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId);
void SetForecast(uint64_t timestamp, std::array<Forecast::Day, MaxNbForecastDays> days);

std::optional<CurrentWeather> Current() const;
std::optional<Forecast> GetForecast() const;

Expand Down

0 comments on commit 3648ffe

Please sign in to comment.