SMACC2
Loading...
Searching...
No Matches
cp_gcalcli_connection.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
22#include <rclcpp/rclcpp.hpp>
24#include <smacc2/smacc.hpp>
25
26#include <cl_gcalcli/events.hpp>
27#include <cl_gcalcli/types.hpp>
28
29namespace cl_gcalcli
30{
31
43{
44public:
46 virtual ~CpGcalcliConnection() = default;
47
48 void onInitialize() override;
49
53 void configure(const GcalcliConfig & config);
54
59
63 bool isConnected() const;
64
69 bool checkConnection();
70
74 void restartConnection();
75
83 const std::string & args, int timeout_ms = 30000);
84
88 const GcalcliConfig & getConfig() const { return config_; }
89
90 // Signals
94
95 // Signal connection helpers
96 template <typename T>
97 smacc2::SmaccSignalConnection onConnectionLost(void (T::*callback)(), T * object)
98 {
99 return this->getStateMachine()->createSignalConnection(onConnectionLost_, callback, object);
100 }
101
102 template <typename T>
103 smacc2::SmaccSignalConnection onConnectionRestored(void (T::*callback)(), T * object)
104 {
105 return this->getStateMachine()->createSignalConnection(onConnectionRestored_, callback, object);
106 }
107
108 template <typename T>
110 {
112 onAuthenticationRequired_, callback, object);
113 }
114
115 // Event posting function - set via onStateOrthogonalAllocation
116 std::function<void()> postConnectionLostEvent_;
117 std::function<void()> postConnectionRestoredEvent_;
118 std::function<void()> postAuthenticationRequiredEvent_;
119
123 template <typename TOrthogonal, typename TSourceObject>
125 {
126 postConnectionLostEvent_ = [this]()
128
129 postConnectionRestoredEvent_ = [this]()
131
132 postAuthenticationRequiredEvent_ = [this]()
134 }
135
136protected:
140 void update() override;
141
142private:
146 void performHeartbeat();
147
151 void handleConnectionStateChange(bool success, const std::string & output);
152
156 bool isAuthenticationError(const std::string & output) const;
157
162
163 std::chrono::steady_clock::time_point last_heartbeat_time_;
164
166 mutable std::mutex state_mutex_;
167};
168
169} // namespace cl_gcalcli
Component that manages gcalcli connection health.
ConnectionState getConnectionState() const
Get current connection state.
std::function< void()> postConnectionLostEvent_
const GcalcliConfig & getConfig() const
Get the gcalcli configuration.
bool isAuthenticationError(const std::string &output) const
Check if output indicates authentication failure.
smacc2::SmaccSignal< void()> onConnectionLost_
smacc2::SmaccSignal< void()> onAuthenticationRequired_
void restartConnection()
Restart connection after failure.
void update() override
Periodic update for heartbeat (called by SignalDetector)
bool isConnected() const
Check if connected to Google Calendar.
std::function< void()> postAuthenticationRequiredEvent_
smacc2::SmaccSignal< void()> onConnectionRestored_
smacc2::SmaccSignalConnection onConnectionLost(void(T::*callback)(), T *object)
smacc2::client_core_components::CpSubprocessExecutor * subprocess_executor_
smacc2::client_core_components::SubprocessResult executeGcalcli(const std::string &args, int timeout_ms=30000)
Execute a gcalcli command.
virtual ~CpGcalcliConnection()=default
void handleConnectionStateChange(bool success, const std::string &output)
Handle connection state change.
bool checkConnection()
Manually trigger a connection check.
std::function< void()> postConnectionRestoredEvent_
void configure(const GcalcliConfig &config)
Configure the gcalcli connection parameters.
void performHeartbeat()
Perform the heartbeat check.
smacc2::SmaccSignalConnection onConnectionRestored(void(T::*callback)(), T *object)
std::chrono::steady_clock::time_point last_heartbeat_time_
void onStateOrthogonalAllocation()
Template method for type-safe event posting setup.
smacc2::SmaccSignalConnection onAuthenticationRequired(void(T::*callback)(), T *object)
ISmaccStateMachine * getStateMachine()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
Generic subprocess execution component for running CLI tools.
ConnectionState
Connection state for gcalcli.
Definition types.hpp:29
boost::signals2::connection SmaccSignalConnection
Configuration for gcalcli client.
Definition types.hpp:102