-
Notifications
You must be signed in to change notification settings - Fork 176
Publish position and events via MQTT #274
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
Draft
rovo89
wants to merge
16
commits into
main
Choose a base branch
from
feat/events-and-position
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
637d8f0
Rename xbot_rpc to xbot_mqtt
rovo89 41d7011
Generic MQTT publisher
rovo89 aaab91a
Pass area ID to the MowingBehavior
rovo89 e0071d7
Publish some events
rovo89 abaff67
Publish current position frequently for smoother UI updates
rovo89 88cb648
Store event history
rovo89 97f6c1f
Store position history
rovo89 1ca23da
Don't update position history while idle
rovo89 e47c575
Thread-safe shutdown handler in mower_logic
rovo89 f46353f
Helper to detect event publishing
rovo89 d539304
Prevent overriding automatically generated event fields
rovo89 15e3406
Publish position in regular intervals with median filter
rovo89 875856c
Ordered event keys
rovo89 59588cc
Proper events and position history
rovo89 430b447
Publish position as object
rovo89 10fdb7a
Publish attributes with the position
rovo89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #pragma once | ||
|
|
||
| #include <deque> | ||
| #include <fstream> | ||
| #include <mutex> | ||
| #include <string> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| using json = nlohmann::ordered_json; | ||
|
|
||
| class EventHistory { | ||
| public: | ||
| EventHistory() = default; | ||
|
|
||
| void init(size_t max_size) { | ||
| max_size_ = max_size; | ||
| file_path_ = "events.jsonl"; | ||
| loadFromDisk(); | ||
| } | ||
|
|
||
| // Appends a raw JSON string to the ring buffer and persists it. | ||
| // No JSON parsing — stored as-is, parsed only on getAll(). | ||
| void add(const std::string& json_payload) { | ||
| std::lock_guard<std::mutex> lk(mutex_); | ||
| if (buffer_.size() >= max_size_) { | ||
| buffer_.pop_front(); | ||
| } | ||
| buffer_.push_back(json_payload); | ||
| appendToDisk(json_payload); | ||
| } | ||
|
rovo89 marked this conversation as resolved.
|
||
|
|
||
| // Returns all buffered events as a JSON array. Parses on read. | ||
| json getAll() const { | ||
| std::lock_guard<std::mutex> lk(mutex_); | ||
| json arr = json::array(); | ||
| for (const auto& line : buffer_) { | ||
| try { | ||
| arr.push_back(json::parse(line)); | ||
| } catch (...) { | ||
| // skip malformed lines | ||
| } | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| private: | ||
| void loadFromDisk() { | ||
| std::ifstream f(file_path_); | ||
| if (!f.is_open()) return; | ||
| std::string line; | ||
| while (std::getline(f, line)) { | ||
| if (line.empty()) continue; | ||
| if (buffer_.size() >= max_size_) buffer_.pop_front(); | ||
| buffer_.push_back(std::move(line)); | ||
| } | ||
| } | ||
|
|
||
| void appendToDisk(const std::string& json_payload) { | ||
| std::ofstream f(file_path_, std::ios::app); | ||
| if (f.is_open()) { | ||
| f << json_payload << "\n"; | ||
| } | ||
| } | ||
|
|
||
| std::string file_path_; | ||
| size_t max_size_ = 100; | ||
| std::deque<std::string> buffer_; | ||
| mutable std::mutex mutex_; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.