17#include <boost/regex.hpp>
35 RCLCPP_ERROR(
getLogger(),
"[CpCalendarPoller] CpGcalcliConnection component not found!");
42 RCLCPP_INFO(
getLogger(),
"[CpCalendarPoller] Initialized");
50 RCLCPP_WARN(
getLogger(),
"[CpCalendarPoller] Cannot refresh - not connected");
57 std::stringstream args;
58 args <<
"agenda --tsv --nocolor";
59 args <<
" --nodeclined";
62 auto now = std::chrono::system_clock::now();
63 auto end = now + std::chrono::hours(24 * config.agenda_days);
65 auto now_t = std::chrono::system_clock::to_time_t(now);
66 auto end_t = std::chrono::system_clock::to_time_t(end);
68 std::tm now_tm{}, end_tm{};
69 localtime_r(&now_t, &now_tm);
70 localtime_r(&end_t, &end_tm);
72 args <<
" \"" << std::put_time(&now_tm,
"%m/%d/%Y") <<
"\"";
73 args <<
" \"" << std::put_time(&end_tm,
"%m/%d/%Y") <<
"\"";
77 if (result.exit_code != 0 || result.timed_out)
80 getLogger(),
"[CpCalendarPoller] Agenda fetch failed: %s", result.stdout_output.c_str());
93 RCLCPP_DEBUG(
getLogger(),
"[CpCalendarPoller] Refreshed agenda: %zu events", events.size());
112 const std::string & pattern,
bool use_regex)
const
116 std::vector<CalendarEvent> matches;
122 boost::regex regex(pattern, boost::regex::icase);
125 if (boost::regex_search(event.title, regex))
127 matches.push_back(event);
131 catch (
const boost::regex_error & e)
133 RCLCPP_ERROR(
getLogger(),
"[CpCalendarPoller] Invalid regex pattern: %s", e.what());
140 if (event.title.find(pattern) != std::string::npos)
142 matches.push_back(event);
151 std::chrono::system_clock::time_point start, std::chrono::system_clock::time_point end)
const
155 std::vector<CalendarEvent> matches;
159 if (event.start_time < end && event.end_time > start)
161 matches.push_back(event);
172 std::vector<CalendarEvent> active;
175 if (event.isActiveNow())
177 active.push_back(event);
203 auto now = std::chrono::steady_clock::now();
204 auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now -
last_poll_attempt_);
206 if (elapsed >= config.poll_interval)
215 std::vector<CalendarEvent> events;
216 std::istringstream stream(output);
219 while (std::getline(stream, line))
222 if (line.empty() || line.find_first_not_of(
" \t\r\n") == std::string::npos)
228 if (event.has_value())
230 events.push_back(event.value());
240 std::vector<std::string> fields;
241 std::istringstream stream(line);
244 while (std::getline(stream, field,
'\t'))
246 fields.push_back(field);
250 if (fields.size() < 5)
253 getLogger(),
"[CpCalendarPoller] Skipping line with insufficient fields: %s", line.c_str());
261 if (!start.has_value())
264 getLogger(),
"[CpCalendarPoller] Failed to parse start time: %s %s", fields[0].c_str(),
268 event.start_time = start.value();
272 if (!end.has_value())
275 getLogger(),
"[CpCalendarPoller] Failed to parse end time: %s %s", fields[2].c_str(),
279 event.end_time = end.value();
282 event.title = fields[4];
285 if (fields.size() > 5)
event.location = fields[5];
286 if (fields.size() > 6)
event.description = fields[6];
287 if (fields.size() > 7)
event.calendar_name = fields[7];
290 event.is_all_day = (fields[1].empty() || fields[1] ==
"00:00" || fields[1] ==
"12:00am");
299 const std::string & date_str,
const std::string & time_str)
304 std::istringstream date_stream(date_str);
306 int year, month, day;
308 if (date_stream >> year >> sep1 >> month >> sep2 >> day && sep1 ==
'-' && sep2 ==
'-')
311 tm.tm_year = year - 1900;
312 tm.tm_mon = month - 1;
319 date_stream.str(date_str);
320 if (date_stream >> month >> sep1 >> day >> sep2 >> year && sep1 ==
'/' && sep2 ==
'/')
322 tm.tm_mon = month - 1;
324 tm.tm_year = year - 1900;
328 RCLCPP_DEBUG(
getLogger(),
"[CpCalendarPoller] Failed to parse date: %s", date_str.c_str());
334 if (!time_str.empty())
336 std::istringstream time_stream(time_str);
340 if (time_stream >> hour >> colon >> minute && colon ==
':')
347 std::getline(time_stream, rest);
351 size_t start = rest.find_first_not_of(
" ");
352 if (start != std::string::npos)
354 rest = rest.substr(start);
357 if (rest ==
"pm" || rest ==
"PM" || rest ==
"p" || rest ==
"P")
359 if (hour != 12) tm.tm_hour += 12;
361 else if (rest ==
"am" || rest ==
"AM" || rest ==
"a" || rest ==
"A")
363 if (hour == 12) tm.tm_hour = 0;
369 RCLCPP_DEBUG(
getLogger(),
"[CpCalendarPoller] Failed to parse time: %s", time_str.c_str());
377 std::time_t time = std::mktime(&tm);
383 return std::chrono::system_clock::from_time_t(time);
389 auto start_t = std::chrono::system_clock::to_time_t(event.
start_time);
390 std::stringstream ss;
391 ss <<
event.title <<
"_" << start_t <<
"_" <<
event.calendar_name;
std::optional< std::chrono::system_clock::time_point > parseDateTime(const std::string &date_str, const std::string &time_str)
Parse date and time strings into time_point.
smacc2::SmaccSignal< void(const std::vector< CalendarEvent > &)> onAgendaUpdated_
std::vector< CalendarEvent > getEventsInWindow(std::chrono::system_clock::time_point start, std::chrono::system_clock::time_point end) const
Get events happening within a time window.
std::chrono::system_clock::time_point last_poll_time_
bool refreshAgenda()
Force an immediate agenda refresh.
void update() override
Periodic update for polling (called by SignalDetector)
std::vector< CalendarEvent > findEvents(const std::string &pattern, bool use_regex=false) const
Get events matching a title pattern.
void onInitialize() override
CpGcalcliConnection * connection_
std::vector< CalendarEvent > getActiveEvents() const
Get currently active events.
std::vector< CalendarEvent > getEvents() const
Get cached list of calendar events.
std::string generateEventId(const CalendarEvent &event)
Generate a unique ID for an event.
std::optional< CalendarEvent > parseTsvLine(const std::string &line)
Parse a single TSV line into a CalendarEvent.
std::chrono::steady_clock::time_point last_poll_attempt_
std::function< void(const std::vector< CalendarEvent > &)> postAgendaUpdatedEvent_
std::vector< CalendarEvent > parseTsvOutput(const std::string &output)
Parse TSV output into CalendarEvent structures.
std::chrono::system_clock::time_point getLastPollTime() const
Get the time of the last successful poll.
std::vector< CalendarEvent > cached_events_
const GcalcliConfig & getConfig() const
Get the gcalcli configuration.
bool isConnected() const
Check if connected to Google Calendar.
smacc2::client_core_components::SubprocessResult executeGcalcli(const std::string &args, int timeout_ms=30000)
Execute a gcalcli command.
rclcpp::Logger getLogger() const
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
Represents a Google Calendar event.
std::chrono::system_clock::time_point start_time