From 29ad09f4ef54126831d36fe1b99e794059fc5421 Mon Sep 17 00:00:00 2001 From: FintasticMan Date: Tue, 20 Feb 2024 11:25:02 +0100 Subject: 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. --- src/components/ble/SimpleWeatherService.cpp | 17 +++++++++-------- src/components/ble/SimpleWeatherService.h | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp index 504cad14..a1f0439e 100644 --- a/src/components/ble/SimpleWeatherService.cpp +++ b/src/components/ble/SimpleWeatherService.cpp @@ -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::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 { diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 30cc5c03..ee40fd6f 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -61,13 +61,17 @@ namespace Pinetime { Unknown = 255 }; + struct Temperature { + int16_t temp; + }; + using Location = std::array; // 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 Current() const; std::optional 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() { -- cgit v1.2.3-70-g09d2