Skip to content

Commit

Permalink
Integration of Simple Weather Service (#135)
Browse files Browse the repository at this point in the history
* Integration of Simple Weather Service

Replace the old WeatherService by the new Simple Weather Service.=

* Update InfiniTime to after SimpleWeatherService got merged

---------

Co-authored-by: Reinhold Gschweicher <[email protected]>
  • Loading branch information
JF002 and NeroBurner authored Dec 23, 2023
1 parent 39b9b17 commit d6cc458
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 786 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ target_sources(infinisim PUBLIC
sim/components/ble/NavigationService.cpp
sim/components/ble/NimbleController.h
sim/components/ble/NimbleController.cpp
sim/components/ble/weather/WeatherService.h
sim/components/ble/weather/WeatherService.cpp
sim/components/ble/SimpleWeatherService.h
sim/components/ble/SimpleWeatherService.cpp
sim/components/brightness/BrightnessController.h
sim/components/brightness/BrightnessController.cpp
sim/components/firmwarevalidator/FirmwareValidator.h
Expand Down
2 changes: 1 addition & 1 deletion InfiniTime
Submodule InfiniTime updated 66 files
+1 −1 .vscode/c_cpp_properties.json
+2 −2 CMakeLists.txt
+69 −0 doc/SimpleWeatherService.md
+4 −1 doc/ble.md
+1 −1 doc/buildAndProgram.md
+90 −32 doc/code/Apps.md
+7 −3 docker/Dockerfile
+13 −47 src/CMakeLists.txt
+3 −3 src/components/ble/NimbleController.h
+160 −0 src/components/ble/SimpleWeatherService.cpp
+141 −0 src/components/ble/SimpleWeatherService.h
+0 −385 src/components/ble/weather/WeatherData.h
+0 −614 src/components/ble/weather/WeatherService.cpp
+0 −169 src/components/ble/weather/WeatherService.h
+2 −2 src/components/datetime/DateTimeController.cpp
+2 −2 src/components/gfx/Gfx.cpp
+1 −1 src/components/settings/Settings.h
+56 −2 src/displayapp/Apps.h
+56 −0 src/displayapp/Controllers.h
+85 −66 src/displayapp/DisplayApp.cpp
+6 −0 src/displayapp/DisplayApp.h
+9 −0 src/displayapp/DisplayAppRecovery.cpp
+6 −0 src/displayapp/DisplayAppRecovery.h
+59 −0 src/displayapp/UserApps.h
+0 −14 src/displayapp/WatchFaces.h
+2 −2 src/displayapp/fonts/fonts.json
+4 −0 src/displayapp/screens/Alarm.cpp
+22 −8 src/displayapp/screens/Alarm.h
+16 −8 src/displayapp/screens/ApplicationList.cpp
+10 −29 src/displayapp/screens/ApplicationList.h
+0 −133 src/displayapp/screens/Clock.cpp
+0 −60 src/displayapp/screens/Clock.h
+11 −0 src/displayapp/screens/HeartRate.h
+13 −0 src/displayapp/screens/InfiniPaint.h
+11 −0 src/displayapp/screens/Metronome.h
+12 −0 src/displayapp/screens/Motion.h
+13 −0 src/displayapp/screens/Music.h
+5 −3 src/displayapp/screens/Navigation.cpp
+13 −0 src/displayapp/screens/Navigation.h
+13 −0 src/displayapp/screens/Paddle.h
+13 −0 src/displayapp/screens/Steps.h
+3 −3 src/displayapp/screens/StopWatch.cpp
+62 −45 src/displayapp/screens/StopWatch.h
+3 −0 src/displayapp/screens/Symbols.h
+4 −4 src/displayapp/screens/SystemInfo.cpp
+0 −1 src/displayapp/screens/Tile.cpp
+1 −1 src/displayapp/screens/Timer.cpp
+46 −33 src/displayapp/screens/Timer.h
+1 −1 src/displayapp/screens/Twos.cpp
+12 −1 src/displayapp/screens/Twos.h
+18 −0 src/displayapp/screens/WatchFaceAnalog.h
+23 −0 src/displayapp/screens/WatchFaceCasioStyleG7710.h
+21 −0 src/displayapp/screens/WatchFaceDigital.h
+22 −0 src/displayapp/screens/WatchFaceInfineat.h
+11 −21 src/displayapp/screens/WatchFacePineTimeStyle.cpp
+25 −6 src/displayapp/screens/WatchFacePineTimeStyle.h
+21 −0 src/displayapp/screens/WatchFaceTerminal.h
+0 −221 src/displayapp/screens/Weather.cpp
+0 −45 src/displayapp/screens/Weather.h
+46 −0 src/displayapp/screens/WeatherSymbols.h
+2 −2 src/displayapp/screens/settings/SettingDisplay.cpp
+7 −2 src/displayapp/screens/settings/SettingWatchFace.cpp
+8 −15 src/displayapp/screens/settings/SettingWatchFace.h
+6 −6 src/drivers/SpiMaster.cpp
+1 −1 src/drivers/Watchdog.cpp
+3 −0 src/systemtask/SystemTask.cpp
8 changes: 4 additions & 4 deletions sim/components/ble/AlertNotificationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ using namespace Pinetime::Controllers;
//constexpr ble_uuid16_t AlertNotificationService::ansCharUuid;
//constexpr ble_uuid128_t AlertNotificationService::notificationEventUuid;

int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
auto anService = static_cast<AlertNotificationService*>(arg);
return anService->OnAlert(conn_handle, attr_handle, ctxt);
}
//int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
//auto anService = static_cast<AlertNotificationService*>(arg);
// return anService->OnAlert(conn_handle, attr_handle, ctxt);
//}

void AlertNotificationService::Init() {
// int res;
Expand Down
2 changes: 1 addition & 1 deletion sim/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
// alertNotificationClient {systemTask, notificationManager},
// currentTimeService {dateTimeController},
musicService {systemTask},
weatherService {systemTask, dateTimeController},
weatherService {dateTimeController},
navService {systemTask} {
// batteryInformationService {batteryController},
// immediateAlertService {systemTask, notificationManager},
Expand Down
6 changes: 3 additions & 3 deletions sim/components/ble/NimbleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "components/ble/NavigationService.h"
//#include "components/ble/ServiceDiscovery.h"
//#include "components/ble/MotionService.h"
#include "components/ble/weather/WeatherService.h"
#include "components/ble/SimpleWeatherService.h"
#include "components/fs/FS.h"
//#include "components/ble/FSService.h"

Expand Down Expand Up @@ -78,7 +78,7 @@ namespace Pinetime {
Pinetime::Controllers::AlertNotificationService& alertService() {
return anService;
};
Pinetime::Controllers::WeatherService& weather() {
Pinetime::Controllers::SimpleWeatherService& weather() {
return weatherService;
};

Expand Down Expand Up @@ -111,7 +111,7 @@ namespace Pinetime {
// AlertNotificationClient alertNotificationClient;
// CurrentTimeService currentTimeService;
MusicService musicService;
WeatherService weatherService;
SimpleWeatherService weatherService;
NavigationService navService;
// BatteryInformationService batteryInformationService;
// ImmediateAlertService immediateAlertService;
Expand Down
110 changes: 110 additions & 0 deletions sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "components/ble/SimpleWeatherService.h"

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

using namespace Pinetime::Controllers;

namespace {
enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown };

uint64_t ToUInt64(const uint8_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) {
SimpleWeatherService::Location cityName;
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::Icons {dataBuffer[16 + 32]},
std::move(cityName));
}

SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2]));

std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> 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 {ToInt16(&dataBuffer[11 + (i * 5)]),
ToInt16(&dataBuffer[13 + (i * 5)]),
SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}};
}
return SimpleWeatherService::Forecast {timestamp, nbDays, days};
}

MessageType GetMessageType(const uint8_t* data) {
auto messageType = static_cast<MessageType>(*data);
if (messageType > MessageType::Unknown) {
return MessageType::Unknown;
}
return messageType;
}

uint8_t GetVersion(const uint8_t* dataBuffer) {
return dataBuffer[1];
}
}

int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) {
return static_cast<Pinetime::Controllers::SimpleWeatherService*>(arg)->OnCommand(ctxt);
}

SimpleWeatherService::SimpleWeatherService(const DateTime& dateTimeController) : dateTimeController(dateTimeController) {
}

void SimpleWeatherService::Init() {
//ble_gatts_count_cfg(serviceDefinition);
//ble_gatts_add_svcs(serviceDefinition);
}

int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {

return 0;
}

std::optional<SimpleWeatherService::CurrentWeather> SimpleWeatherService::Current() const {
if (currentWeather) {
auto currentTime = dateTimeController.UTCDateTime().time_since_epoch();
auto weatherTpSecond = std::chrono::seconds {currentWeather->timestamp};
auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
auto delta = currentTime - weatherTp;

if (delta < std::chrono::hours {24}) {
return currentWeather;
}
}
return {};
}

std::optional<SimpleWeatherService::Forecast> SimpleWeatherService::GetForecast() const {
if (forecast) {
auto currentTime = dateTimeController.UTCDateTime().time_since_epoch();
auto weatherTpSecond = std::chrono::seconds {forecast->timestamp};
auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
auto delta = currentTime - weatherTp;

if (delta < std::chrono::hours {24}) {
return this->forecast;
}
}
return {};
}

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 &&
std::strcmp(this->location.data(), other.location.data()) == 0;
}
125 changes: 125 additions & 0 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <memory>

//#define min // workaround: nimble's min/max macros conflict with libstdc++
//#define max
//#include <host/ble_gap.h>
//#include <host/ble_uuid.h>
#include <optional>
#include <cstring>
#include <array>
//#undef max
//#undef min

#include "components/datetime/DateTimeController.h"

int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg);

namespace Pinetime {
namespace Controllers {

class SimpleWeatherService {
public:
explicit SimpleWeatherService(const DateTime& dateTimeController);

void Init();

int OnCommand(struct ble_gatt_access_ctxt* ctxt);

static constexpr uint8_t MaxNbForecastDays = 5;

enum class Icons : uint8_t {
Sun = 0, // ClearSky
CloudsSun = 1, // FewClouds
Clouds = 2, // Scattered clouds
BrokenClouds = 3,
CloudShowerHeavy = 4, // shower rain
CloudSunRain = 5, // rain
Thunderstorm = 6,
Snow = 7,
Smog = 8, // Mist
Unknown = 255
};

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,
Icons iconId,
Location&& location)
: timestamp {timestamp},
temperature {temperature},
minTemperature {minTemperature},
maxTemperature {maxTemperature},
iconId {iconId},
location {std::move(location)} {
}

uint64_t timestamp;
int16_t temperature;
int16_t minTemperature;
int16_t maxTemperature;
Icons iconId;
Location location;

bool operator==(const CurrentWeather& other) const;
};

struct Forecast {
uint64_t timestamp;
uint8_t nbDays;

struct Day {
int16_t minTemperature;
int16_t maxTemperature;
Icons iconId;
};

std::array<Day, MaxNbForecastDays> days;
};

std::optional<CurrentWeather> Current() const;
std::optional<Forecast> GetForecast() const;

private:
// 00050000-78fc-48fe-8e23-433b3a1942d0
//static constexpr ble_uuid128_t BaseUuid() {
// return CharUuid(0x00, 0x00);
//}

// 0005yyxx-78fc-48fe-8e23-433b3a1942d0
//static constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
// return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128},
// .value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, y, x, 0x05, 0x00}};
//}

//ble_uuid128_t weatherUuid {BaseUuid()};

//ble_uuid128_t weatherDataCharUuid {CharUuid(0x00, 0x01)};

//const struct ble_gatt_chr_def characteristicDefinition[2] = {{.uuid = &weatherDataCharUuid.u,
// .access_cb = WeatherCallback,
// .arg = this,
// .flags = BLE_GATT_CHR_F_WRITE,
// .val_handle = &eventHandle},
// {0}};
//const struct ble_gatt_svc_def serviceDefinition[2] = {
// {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &weatherUuid.u, .characteristics = characteristicDefinition},
// {0}};

uint16_t eventHandle {};

const Pinetime::Controllers::DateTime& dateTimeController;

std::optional<CurrentWeather> currentWeather;
std::optional<Forecast> forecast;
};
}
}
Loading

0 comments on commit d6cc458

Please sign in to comment.