SMACC2
Loading...
Searching...
No Matches
cb_event_detect.cpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#include <algorithm>
18
19#include <boost/regex.hpp>
20
21namespace cl_gcalcli
22{
23
24CbEventDetect::CbEventDetect(const std::string & pattern, bool use_regex, int minutes_before)
25: pattern_(pattern),
26 use_regex_(use_regex),
27 minutes_before_(minutes_before),
28 listener_(nullptr),
29 client_(nullptr),
30 triggered_(false)
31{
32}
33
35{
36 RCLCPP_INFO(
37 getLogger(),
38 "[CbEventDetect] Waiting for event matching pattern '%s' (regex=%s, minutes_before=%d)",
39 pattern_.c_str(), use_regex_ ? "true" : "false", minutes_before_);
40
41 // Get the client and listener component
43 if (client_)
44 {
46 }
47
48 if (!listener_)
49 {
50 RCLCPP_ERROR(getLogger(), "[CbEventDetect] CpCalendarEventListener not available");
51 this->postFailureEvent();
52 return;
53 }
54
55 // Reset triggered events to allow re-detection
57
58 // Configure the watch
59 EventWatch watch;
60 watch.pattern = pattern_;
61 watch.use_regex = use_regex_;
63 watch.trigger_on_start = true;
64 watch.trigger_on_end = false;
65 watch.continuous = false;
66
67 listener_->addWatch(watch);
68
69 // Connect to the event started signal
71}
72
73void CbEventDetect::onExit() { RCLCPP_DEBUG(getLogger(), "[CbEventDetect] Exiting"); }
74
76{
77 if (triggered_)
78 {
79 return; // Already triggered
80 }
81
82 // Check if this event matches our pattern
83 bool matches = false;
84 if (use_regex_)
85 {
86 try
87 {
88 boost::regex regex(pattern_, boost::regex::icase);
89 matches = boost::regex_search(event.title, regex);
90 }
91 catch (const boost::regex_error &)
92 {
93 matches = false;
94 }
95 }
96 else
97 {
98 std::string title_lower = event.title;
99 std::string pattern_lower = pattern_;
100 std::transform(title_lower.begin(), title_lower.end(), title_lower.begin(), ::tolower);
101 std::transform(pattern_lower.begin(), pattern_lower.end(), pattern_lower.begin(), ::tolower);
102 matches = title_lower.find(pattern_lower) != std::string::npos;
103 }
104
105 if (matches)
106 {
107 triggered_ = true;
108 detected_event_ = event;
109
110 RCLCPP_INFO(getLogger(), "[CbEventDetect] Event detected: %s", event.title.c_str());
111
112 this->postSuccessEvent();
113 }
114}
115
116} // namespace cl_gcalcli
std::optional< CalendarEvent > detected_event_
CpCalendarEventListener * listener_
CbEventDetect(const std::string &pattern, bool use_regex=false, int minutes_before=0)
Construct with pattern configuration.
void onEventStarted(const CalendarEvent &event)
Callback when an event starts.
CpCalendarEventListener * getEventListener()
void resetTriggeredEvents()
Reset triggered event tracking (allows re-triggering)
smacc2::SmaccSignalConnection onEventStarted(void(T::*callback)(const CalendarEvent &), T *object)
void addWatch(const EventWatch &watch)
Add a watch pattern for event detection.
virtual rclcpp::Logger getLogger() const
void requiresClient(SmaccClientType *&storage)
Represents a Google Calendar event.
Definition types.hpp:40
Event watch configuration for CpCalendarEventListener.
Definition types.hpp:129
bool trigger_on_start
Post event when event starts.
Definition types.hpp:140
bool use_regex
True = regex matching, False = exact string matching.
Definition types.hpp:134
bool trigger_on_end
Post event when event ends.
Definition types.hpp:143
bool continuous
Keep watching (true) or one-shot (false)
Definition types.hpp:146
int minutes_before
Trigger N minutes before event starts (0 = at start time)
Definition types.hpp:137
std::string pattern
Pattern to match event titles (regex or exact)
Definition types.hpp:131