Skip to content

Commit

Permalink
Integration of Simple Weather Service
Browse files Browse the repository at this point in the history
Update SimpleWeatherService following changes in InfiniTime.
  • Loading branch information
JF002 committed Dec 23, 2023
1 parent 640e1a2 commit 8f32d1b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
31 changes: 20 additions & 11 deletions sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "components/ble/SimpleWeatherService.h"

#include <algorithm>
#include <array>
#include <cstring>
#include <nrf_log.h>

Expand All @@ -10,19 +11,24 @@ namespace {
enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown };

uint64_t ToUInt64(const uint8_t* data) {
return *(reinterpret_cast<const uint64_t*>(data));
return data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24) + (static_cast<uint64_t>(data[4]) << 32) +
(static_cast<uint64_t>(data[5]) << 40) + (static_cast<uint64_t>(data[6]) << 48) + (static_cast<uint64_t>(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) {
Expand All @@ -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};
}
Expand Down Expand Up @@ -97,5 +105,6 @@ 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;
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature &&
std::strcmp(this->location.data(), other.location.data()) == 0;
}
31 changes: 16 additions & 15 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,29 @@ class SimpleWeatherService {
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)
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;
};
Expand All @@ -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<Day, MaxNbForecastDays> days;
Expand Down

0 comments on commit 8f32d1b

Please sign in to comment.