weather: Refactor temperature type for type safety
There is now a Temperature struct in the weather service, which holds the internal representation. There is also a temperature struct in the Applications namespace, which holds the temperature in either Celsius or Fahrenheit.
This commit is contained in:
@@ -42,9 +42,9 @@ namespace {
|
||||
std::memcpy(cityName.data(), &dataBuffer[16], 32);
|
||||
cityName[32] = '\0';
|
||||
return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]),
|
||||
ToInt16(&dataBuffer[10]),
|
||||
ToInt16(&dataBuffer[12]),
|
||||
ToInt16(&dataBuffer[14]),
|
||||
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[10])},
|
||||
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[12])},
|
||||
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[14])},
|
||||
SimpleWeatherService::Icons {dataBuffer[16 + 32]},
|
||||
std::move(cityName));
|
||||
}
|
||||
@@ -56,8 +56,8 @@ namespace {
|
||||
const uint8_t nbDaysInBuffer = dataBuffer[10];
|
||||
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
|
||||
for (int i = 0; i < nbDays; i++) {
|
||||
days[i] = SimpleWeatherService::Forecast::Day {ToInt16(&dataBuffer[11 + (i * 5)]),
|
||||
ToInt16(&dataBuffer[13 + (i * 5)]),
|
||||
days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature {ToInt16(&dataBuffer[11 + (i * 5)])},
|
||||
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[13 + (i * 5)])},
|
||||
SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}};
|
||||
}
|
||||
return SimpleWeatherService::Forecast {timestamp, nbDays, days};
|
||||
@@ -154,13 +154,14 @@ std::optional<SimpleWeatherService::Forecast> SimpleWeatherService::GetForecast(
|
||||
}
|
||||
|
||||
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 &&
|
||||
return this->iconId == other.iconId && this->temperature.temp == other.temperature.temp && this->timestamp == other.timestamp &&
|
||||
this->maxTemperature.temp == other.maxTemperature.temp && this->minTemperature.temp == other.maxTemperature.temp &&
|
||||
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;
|
||||
return this->iconId == other.iconId && this->maxTemperature.temp == other.maxTemperature.temp &&
|
||||
this->minTemperature.temp == other.maxTemperature.temp;
|
||||
}
|
||||
|
||||
bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const {
|
||||
|
||||
@@ -61,13 +61,17 @@ namespace Pinetime {
|
||||
Unknown = 255
|
||||
};
|
||||
|
||||
struct Temperature {
|
||||
int16_t temp;
|
||||
};
|
||||
|
||||
using Location = std::array<char, 33>; // 32 char + \0 (end of string)
|
||||
|
||||
struct CurrentWeather {
|
||||
CurrentWeather(uint64_t timestamp,
|
||||
int16_t temperature,
|
||||
int16_t minTemperature,
|
||||
int16_t maxTemperature,
|
||||
Temperature temperature,
|
||||
Temperature minTemperature,
|
||||
Temperature maxTemperature,
|
||||
Icons iconId,
|
||||
Location&& location)
|
||||
: timestamp {timestamp},
|
||||
@@ -79,9 +83,9 @@ namespace Pinetime {
|
||||
}
|
||||
|
||||
uint64_t timestamp;
|
||||
int16_t temperature;
|
||||
int16_t minTemperature;
|
||||
int16_t maxTemperature;
|
||||
Temperature temperature;
|
||||
Temperature minTemperature;
|
||||
Temperature maxTemperature;
|
||||
Icons iconId;
|
||||
Location location;
|
||||
|
||||
@@ -93,8 +97,8 @@ namespace Pinetime {
|
||||
uint8_t nbDays;
|
||||
|
||||
struct Day {
|
||||
int16_t minTemperature;
|
||||
int16_t maxTemperature;
|
||||
Temperature minTemperature;
|
||||
Temperature maxTemperature;
|
||||
Icons iconId;
|
||||
|
||||
bool operator==(const Day& other) const;
|
||||
@@ -108,10 +112,6 @@ namespace Pinetime {
|
||||
std::optional<CurrentWeather> Current() const;
|
||||
std::optional<Forecast> GetForecast() const;
|
||||
|
||||
static int16_t CelsiusToFahrenheit(int16_t celsius) {
|
||||
return celsius * 9 / 5 + 3200;
|
||||
}
|
||||
|
||||
private:
|
||||
// 00050000-78fc-48fe-8e23-433b3a1942d0
|
||||
static constexpr ble_uuid128_t BaseUuid() {
|
||||
|
||||
Reference in New Issue
Block a user