From 8f32d1be5b2ab95fe202c4cbb63a2dd4edeaa416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sat, 23 Dec 2023 21:07:33 +0100 Subject: [PATCH] Integration of Simple Weather Service Update SimpleWeatherService following changes in InfiniTime. --- sim/components/ble/SimpleWeatherService.cpp | 31 +++++++++++++-------- sim/components/ble/SimpleWeatherService.h | 31 +++++++++++---------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp index d291397..01fc792 100644 --- a/sim/components/ble/SimpleWeatherService.cpp +++ b/sim/components/ble/SimpleWeatherService.cpp @@ -1,6 +1,7 @@ #include "components/ble/SimpleWeatherService.h" #include +#include #include #include @@ -10,19 +11,24 @@ namespace { enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown }; uint64_t ToUInt64(const uint8_t* data) { - return *(reinterpret_cast(data)); + return data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24) + (static_cast(data[4]) << 32) + + (static_cast(data[5]) << 40) + (static_cast(data[6]) << 48) + (static_cast(data[7]) << 56); +} + +int16_t ToInt16(const uint8_t* data) { + return data[0] + (data[1] << 8); } SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuffer) { - char cityName[33]; - std::memcpy(&cityName[0], &dataBuffer[13], 32); + SimpleWeatherService::Location cityName; + std::memcpy(cityName.data(), &dataBuffer[16], 32); cityName[32] = '\0'; - return SimpleWeatherService::CurrentWeather {ToUInt64(&dataBuffer[2]), - dataBuffer[10], - dataBuffer[11], - dataBuffer[12], - dataBuffer[13 + 32], - cityName}; + return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]), + ToInt16(&dataBuffer[10]), + ToInt16(&dataBuffer[12]), + ToInt16(&dataBuffer[14]), + SimpleWeatherService::Icons {dataBuffer[16 + 32]}, + std::move(cityName)); } SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { @@ -32,7 +38,9 @@ SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { 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 {dataBuffer[11 + (i * 3)], dataBuffer[12 + (i * 3)], dataBuffer[13 + (i * 3)]}; + days[i] = SimpleWeatherService::Forecast::Day {ToInt16(&dataBuffer[11 + (i * 5)]), + ToInt16(&dataBuffer[13 + (i * 5)]), + SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}}; } return SimpleWeatherService::Forecast {timestamp, nbDays, days}; } @@ -97,5 +105,6 @@ 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; + this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature && + std::strcmp(this->location.data(), other.location.data()) == 0; } diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h index 3c9ce92..be0d141 100644 --- a/sim/components/ble/SimpleWeatherService.h +++ b/sim/components/ble/SimpleWeatherService.h @@ -45,28 +45,29 @@ class SimpleWeatherService { Unknown = 255 }; + using Location = std::array; // 32 char + \0 (end of string) + struct CurrentWeather { CurrentWeather(uint64_t timestamp, - uint8_t temperature, - uint8_t minTemperature, - uint8_t maxTemperature, - uint8_t iconId, - const char* location) + int16_t temperature, + int16_t minTemperature, + int16_t maxTemperature, + Icons iconId, + Location&& location) : timestamp {timestamp}, temperature {temperature}, minTemperature {minTemperature}, maxTemperature {maxTemperature}, - iconId {iconId} { - std::memcpy(this->location, location, 32); - this->location[32] = 0; + iconId {iconId}, + location {std::move(location)} { } uint64_t timestamp; - uint8_t temperature; - uint8_t minTemperature; - uint8_t maxTemperature; + int16_t temperature; + int16_t minTemperature; + int16_t maxTemperature; Icons iconId; - char location[33]; // 32 char + \0 (end of string) + Location location; bool operator==(const CurrentWeather& other) const; }; @@ -76,9 +77,9 @@ class SimpleWeatherService { uint8_t nbDays; struct Day { - uint8_t minTemperature; - uint8_t maxTemperature; - uint8_t iconId; + int16_t minTemperature; + int16_t maxTemperature; + Icons iconId; }; std::array days;