SMACC2
Loading...
Searching...
No Matches
cb_detect_calendar_event.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
25 const std::string & pattern, bool use_regex, int minutes_before)
26: pattern_(pattern),
27 use_regex_(use_regex),
28 minutes_before_(minutes_before),
29 listener_(nullptr),
30 client_(nullptr),
31 triggered_(false)
32{
33}
34
36{
37 RCLCPP_INFO(
38 getLogger(),
39 "[CbDetectCalendarEvent] Waiting for event matching pattern '%s' (regex=%s, minutes_before=%d)",
40 pattern_.c_str(), use_regex_ ? "true" : "false", minutes_before_);
41
42 // Get the client and listener component
44 if (client_)
45 {
47 }
48
49 if (!listener_)
50 {
51 RCLCPP_ERROR(getLogger(), "[CbDetectCalendarEvent] CpCalendarEventListener not available");
52 this->postFailureEvent();
53 return;
54 }
55
56 // Reset triggered events to allow re-detection
58
59 // Configure the watch
60 EventWatch watch;
61 watch.pattern = pattern_;
62 watch.use_regex = use_regex_;
64 watch.trigger_on_start = true;
65 watch.trigger_on_end = false;
66 watch.continuous = false;
67
68 listener_->addWatch(watch);
69
70 // Connect to the event started signal
72}
73
75{
76 RCLCPP_DEBUG(getLogger(), "[CbDetectCalendarEvent] Exiting");
77}
78
80{
81 if (triggered_)
82 {
83 return; // Already triggered
84 }
85
86 // Check if this event matches our pattern
87 bool matches = false;
88 if (use_regex_)
89 {
90 try
91 {
92 boost::regex regex(pattern_, boost::regex::icase);
93 matches = boost::regex_search(event.title, regex);
94 }
95 catch (const boost::regex_error &)
96 {
97 matches = false;
98 }
99 }
100 else
101 {
102 std::string title_lower = event.title;
103 std::string pattern_lower = pattern_;
104 std::transform(title_lower.begin(), title_lower.end(), title_lower.begin(), ::tolower);
105 std::transform(pattern_lower.begin(), pattern_lower.end(), pattern_lower.begin(), ::tolower);
106 matches = title_lower.find(pattern_lower) != std::string::npos;
107 }
108
109 if (matches)
110 {
111 triggered_ = true;
112 detected_event_ = event;
113
114 RCLCPP_INFO(getLogger(), "[CbDetectCalendarEvent] Event detected: %s", event.title.c_str());
115
116 this->postSuccessEvent();
117 }
118}
119
120} // namespace cl_gcalcli
CbDetectCalendarEvent(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.
std::optional< CalendarEvent > detected_event_
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