Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve weather service #1890

Closed
wants to merge 7 commits into from

Conversation

FintasticMan
Copy link
Member

@FintasticMan FintasticMan commented Oct 14, 2023

This pull request adds a number of changes to WeatherService and WeatherData that improve its functionality and stability. These changes are partially based on @faxe1008's work in #1822, @JF002's work in #1860 and my work in #1882.

The change I made to #1822 is to not use a template function for the general GetCurrent function, but rather let the GetCurrent* functions do the cast themselves. This reduces the number of functions created in total.

One of the big improvements is the addition of an event ID. This ID will allow companion apps to update values they have already sent with new information, allowing them to operate statelessly.
This is a BREAKING CHANGE to the WeatherService API, meaning that companion apps will need to update to add an EventID field to the CBOR data for every event that they send. If this PR gets approved, I will make prepare patches for all the companion apps that implement WeatherService to make the change smoother.

I propose that we create a list of standardised IDs for events that most companion apps will implement, which will allow the user to switch companion apps and have the weather data update seamlessly.
Companion apps will then also be able to create their own IDs outside of the standard ones that won't be affected by other companions.

One change I would still like to make is to move away from using std::strings and switch to using std::array, to reduce the uncertainty of how long the string might be.

@JF002 @Avamander @faxe1008 could you please give some feedback on these changes.

Fixes #1786, #1788 and #1877.

faxe1008 and others added 5 commits October 14, 2023 17:22
Before further parsing of the packet, check timestamp and expires field
right after it has been decoded to avoid unnecessary allocs/frees. Also
perform tidying up of the vector before attempting to add any new
elements.
This means that there is a higher likelihood that the returned event is
the most up-to-date current event.
It chooses the event with the lowest expiry, and between events with the
same expiry, it chooses the one with the latest timestamp.
This allows us to use new functionalities like "if constexpr" and
std::optional.
@github-actions
Copy link

Build size and comparison to main:

Section Size Difference
text 377568B 768B
data 940B 0B
bss 63420B 0B

@FintasticMan FintasticMan linked an issue Oct 14, 2023 that may be closed by this pull request
1 task
@FintasticMan FintasticMan added the weather Bugs and PRs related to Weather label Oct 14, 2023
src/components/ble/weather/WeatherService.cpp Show resolved Hide resolved
/**
* Unique ID for this event
*/
uint16_t eventID;
Copy link
Collaborator

@Avamander Avamander Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more and more coming to the conclusion that this would in practice behave as a "mutability flag" of sorts.

So could we maybe instead of an ID use a boolean "overwrite this with the ones coming in with the same flag"? We can keep it an uint8_t and use a single bit for starters?

Meaning currently companions will probably agree that e.g. 1 will be next upcoming temperature, 2 will be next upcoming precipitation and so on. But this makes it hard for more advanced companions to push say "next week" without interfering with other companions. (Though I don't know if this should really be our problem. Is it with BLE FS, Music or notifications?)

This way we could avoid the previously mentioned caveats by just marking events "replaceable" and it provides one "immediate" slot for each event, kinda.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag could also be set a default and we could handle it being missing from the CBOR packet. This way we can also avoid a breaking API change.

src/components/ble/weather/WeatherService.cpp Outdated Show resolved Hide resolved
src/components/ble/weather/WeatherService.cpp Show resolved Hide resolved
src/components/ble/weather/WeatherService.cpp Show resolved Hide resolved
int64_t tmpAltitude = 0;
QCBORDecode_GetInt64InMapSZ(&decodeContext, "Altitude", &tmpAltitude);
if (tmpAltitude < -32768 || tmpAltitude >= 32767) {
auto optAltitude = Get<int16_t>(&decodeContext, "Altitude");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact, this data could be used in the TImer app to adjust the time it takes to boil water for eggs for example.

JF002 and others added 2 commits October 23, 2023 00:53
Add a sane max number of elements in the timeline
Create events in their own factory methods (CreateXXXEvent())
Encapsulate QCBOR deserialization in template functions Get<>().

Co-authored-by: FintasticMan <[email protected]>
Make sure there is only 1 of each event with the same type and ID.

This is a BREAKING CHANGE for the WeatherService API, and as such a new
EventID field will need to be added to the CBOR data sent by companion
apps.
@JF002
Copy link
Collaborator

JF002 commented Dec 23, 2023

Thanks to everyone involved in this PR. I'm closing it in favor to #1924 since I decided to rework the weather service so we can fix the issues more easily.

@JF002 JF002 closed this Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weather Bugs and PRs related to Weather
Projects
None yet
5 participants