InfiniTime.git

commit ef2c43156928b659d02a4ac9412ee947d0cc4d27

Author: Jean-François Milants <jf@codingfield.com>

Simple Weather Service

Code improvements : icon fields are now typed as Icons, move the location string when creating a new instance of CurrentWeather, fix SimpleWeatherService::CurrentWeather::operator== (location was missing from the comparison).

 src/components/ble/SimpleWeatherService.cpp | 16 +++++++++-------
 src/components/ble/SimpleWeatherService.h | 14 +++++++-------


diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp
index 61ad49267ed0cf6f5b3ecd24662e98d974a6ddad..2ba26321cdcc9e6346e67a64cef8e3ecd7608f04 100644
--- a/src/components/ble/SimpleWeatherService.cpp
+++ b/src/components/ble/SimpleWeatherService.cpp
@@ -19,6 +19,7 @@
 #include "components/ble/SimpleWeatherService.h"
 
 #include <algorithm>
+#include <array>
 #include <cstring>
 #include <nrf_log.h>
 
@@ -32,15 +33,15 @@     return *(reinterpret_cast(data));
   }
 
   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[13], 32);
     cityName[32] = '\0';
-    return SimpleWeatherService::CurrentWeather {ToUInt64(&dataBuffer[2]),
+    return SimpleWeatherService::CurrentWeather (ToUInt64(&dataBuffer[2]),
                                                  dataBuffer[10],
                                                  dataBuffer[11],
                                                  dataBuffer[12],
-                                                 dataBuffer[13 + 32],
-                                                 cityName};
+                                                 SimpleWeatherService::Icons{dataBuffer[13 + 32]},
+                                                 std::move(cityName));
   }
 
   SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
@@ -50,7 +51,7 @@     std::array days;
     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 {dataBuffer[11 + (i * 3)], dataBuffer[12 + (i * 3)], SimpleWeatherService::Icons{dataBuffer[13 + (i * 3)]}};
     }
     return SimpleWeatherService::Forecast {timestamp, nbDays, days};
   }
@@ -147,5 +148,6 @@ }
 
 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/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h
index 890497d7f2ee4173dc1303a2e54e926df889015a..561917eb6a46b9beb0fee8b26c4397b61bffaea2 100644
--- a/src/components/ble/SimpleWeatherService.h
+++ b/src/components/ble/SimpleWeatherService.h
@@ -61,20 +61,20 @@         Smog = 8, // Mist
         Unknown = 255
       };
 
+      using Location = std::array<char, 33>;  // 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)
+                       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;
@@ -82,7 +82,7 @@         uint8_t temperature;
         uint8_t minTemperature;
         uint8_t maxTemperature;
         Icons iconId;
-        char location[33]; // 32 char + \0 (end of string)
+        Location location;
 
         bool operator==(const CurrentWeather& other) const;
       };
@@ -94,7 +94,7 @@
         struct Day {
           uint8_t minTemperature;
           uint8_t maxTemperature;
-          uint8_t iconId;
+          Icons iconId;
         };
 
         std::array<Day, MaxNbForecastDays> days;