Device: Add MessageFilter to enableMessagePolling()

pull/76/merge
Jonathan Schwartz 2025-07-29 17:15:18 +00:00 committed by Kyle Schwarz
parent 1e5e714f5b
commit c5bce795c6
4 changed files with 12 additions and 5 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ third-party/concurrentqueue/tests
examples/csharp/bin
examples/csharp/obj
test/system
.env

View File

@ -17,7 +17,7 @@ void init_device(pybind11::module_& m) {
.def("close", &Device::close, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("describe", &Device::describe)
.def("disable_message_polling", &Device::disableMessagePolling, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("enable_message_polling", &Device::enableMessagePolling, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("enable_message_polling", &Device::enableMessagePolling, pybind11::arg("filter") = std::nullopt, pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_current_message_count", &Device::getCurrentMessageCount)
.def("get_digital_io", &Device::getDigitalIO, pybind11::arg("type"), pybind11::arg("number"), pybind11::call_guard<pybind11::gil_scoped_release>())
.def("get_gptp_status", &Device::getGPTPStatus, pybind11::arg("timeout") = std::chrono::milliseconds(100), pybind11::call_guard<pybind11::gil_scoped_release>())

View File

@ -105,15 +105,21 @@ std::string Device::describe() const {
return ss.str();
}
bool Device::enableMessagePolling() {
bool Device::enableMessagePolling(std::optional<MessageFilter> filter) {
if(isMessagePollingEnabled()) {// We are already polling
report(APIEvent::Type::DeviceCurrentlyPolling, APIEvent::Severity::Error);
return false;
}
messagePollingCallbackID = com->addMessageCallback(std::make_shared<MessageCallback>([this](std::shared_ptr<Message> message) {
if(!filter.has_value()) {
// If no filter is provided, use a default that includes all messages
filter.emplace(MessageFilter());
filter->includeInternalInAny = true;
}
auto callback = std::make_shared<MessageCallback>(*filter, [this](std::shared_ptr<Message> message) {
pollingContainer.enqueue(message);
enforcePollingMessageLimit();
}));
});
messagePollingCallbackID = com->addMessageCallback(callback);
return true;
}

View File

@ -206,7 +206,7 @@ public:
// Message polling related functions
bool enableMessagePolling();
bool enableMessagePolling(std::optional<MessageFilter> filter = std::nullopt);
bool disableMessagePolling();
bool isMessagePollingEnabled() { return messagePollingCallbackID != 0; };
std::pair<std::vector<std::shared_ptr<Message>>, bool> getMessages();