SMACC2
Loading...
Searching...
No Matches
cp_calendar_poller.hpp
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
15#pragma once
16
17#include <chrono>
18#include <functional>
19#include <mutex>
20#include <string>
21#include <vector>
22
23#include <rclcpp/rclcpp.hpp>
24#include <smacc2/smacc.hpp>
25
27#include <cl_gcalcli/events.hpp>
28#include <cl_gcalcli/types.hpp>
29
30namespace cl_gcalcli
31{
32
44{
45public:
47 virtual ~CpCalendarPoller() = default;
48
49 void onInitialize() override;
50
55 bool refreshAgenda();
56
60 std::vector<CalendarEvent> getEvents() const;
61
67 std::vector<CalendarEvent> findEvents(const std::string & pattern, bool use_regex = false) const;
68
72 std::vector<CalendarEvent> getEventsInWindow(
73 std::chrono::system_clock::time_point start, std::chrono::system_clock::time_point end) const;
74
78 std::vector<CalendarEvent> getActiveEvents() const;
79
83 std::chrono::system_clock::time_point getLastPollTime() const;
84
85 // Signal for agenda updates
86 smacc2::SmaccSignal<void(const std::vector<CalendarEvent> &)> onAgendaUpdated_;
87
88 // Signal connection helper
89 template <typename T>
91 void (T::*callback)(const std::vector<CalendarEvent> &), T * object)
92 {
93 return this->getStateMachine()->createSignalConnection(onAgendaUpdated_, callback, object);
94 }
95
96 // Event posting function - set via onStateOrthogonalAllocation
97 std::function<void(const std::vector<CalendarEvent> &)> postAgendaUpdatedEvent_;
98
102 template <typename TOrthogonal, typename TSourceObject>
104 {
105 postAgendaUpdatedEvent_ = [this](const std::vector<CalendarEvent> & events)
106 {
108 ev->events = events;
109 this->postEvent(ev);
110 };
111 }
112
113protected:
117 void update() override;
118
119private:
123 std::vector<CalendarEvent> parseTsvOutput(const std::string & output);
124
128 std::optional<CalendarEvent> parseTsvLine(const std::string & line);
129
135 std::optional<std::chrono::system_clock::time_point> parseDateTime(
136 const std::string & date_str, const std::string & time_str);
137
141 std::string generateEventId(const CalendarEvent & event);
142
144 std::vector<CalendarEvent> cached_events_;
145 std::chrono::system_clock::time_point last_poll_time_;
146 std::chrono::steady_clock::time_point last_poll_attempt_;
148
149 mutable std::mutex events_mutex_;
150};
151
152} // namespace cl_gcalcli
Component that polls Google Calendar and parses event data.
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 onStateOrthogonalAllocation()
Template method for type-safe event posting setup.
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_
smacc2::SmaccSignalConnection onAgendaUpdated(void(T::*callback)(const std::vector< CalendarEvent > &), T *object)
std::vector< CalendarEvent > parseTsvOutput(const std::string &output)
Parse TSV output into CalendarEvent structures.
virtual ~CpCalendarPoller()=default
std::chrono::system_clock::time_point getLastPollTime() const
Get the time of the last successful poll.
std::vector< CalendarEvent > cached_events_
Component that manages gcalcli connection health.
ISmaccStateMachine * getStateMachine()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection SmaccSignalConnection
Represents a Google Calendar event.
Definition types.hpp:40
Event posted when the agenda is updated.
Definition events.hpp:100