TopicRecorder.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX
17 * @author Mirko Waechter( mirko.waechter at kit dot edu)
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 #pragma once
23 
24 
25 
26 #include "GenericTopicSubscriber.h"
27 #include "TopicWriterInterface.h"
28 
29 #include <ArmarXCore/interface/components/TopicRecorderInterface.h>
32 
33 #include <condition_variable>
34 #include <filesystem>
35 #include <mutex>
36 
37 namespace armarx
38 {
39 
42  {
43  public:
46  {
47  defineRequiredProperty<std::string>("TopicsToLog", "Comma seperated list of topics to log. Use * to record all. To specify the max frequency append a : to the topic name, e.g. \"RobotState:10,Log\" Topic names must not contain : or ,");
48  defineOptionalProperty<std::string>("Outputfile", "topic_recording.bag", "File to write the output to. The file will be overwritten if the file exists");
49  defineOptionalProperty<bool>("TimestampedFilename", true, "If true, a timestamp is added to the filename");
50  defineOptionalProperty<std::string>("StorageMode", "database", "Storage variant to use, currently 'database' (default) and 'file' are available");
51  defineOptionalProperty<bool>("EnableRecording", true, "Immediately start recording after the component is launched.");
52  defineOptionalProperty<int>("Duration", 0, "Limit recording.");
53  }
54  };
55 
56  /**
57  * \page ArmarXCore-TopicRecording Recording and Replaying Topics
58  * It is possible to easily record and replay any topic available in ArmarX.
59  * For recording a topic use the app *TopicRecorderAppRun* like this:
60  * \verbatim
61  $ARMARX_CORE/build/bin/TopicRecorderAppRun --ArmarX.TopicRecorder.Outputfile=test.bag --ArmarX.TopicRecorder.TopicsToLog=* --ArmarX.TopicRecorder.TimestampedFilename=false
62  \endverbatim
63  * This will log any topic that was running, when the recorder was **started** and write it to the file *test.bag*.
64  *
65  * To replay this file, use the GUI \ref ArmarXGui-GuiPlugins-TopicReplayer or use the command line tool *TopicReplayerrAppRun* like this:
66  * \verbatim
67  $ARMARX_CORE/build/bin/TopicReplayerAppRun --ArmarX.TopicReplayer.RecordFile=test.bag
68  \endverbatim
69  * This will replay the recorded topics in the same speed as they were recorded.
70  *
71  * You can add the optional values
72  * \verbatim
73  --ArmarX.TopicRecorder.StorageMode="database"
74  --ArmarX.TopicReplayer.StorageMode="database"
75  \endverbatim
76  * or
77  * \verbatim
78  --ArmarX.TopicRecorder.StorageMode="file"
79  --ArmarX.TopicReplayer.StorageMode="file"
80  \endverbatim
81  * to the Recorder and Replayer to switch between the available storage modes, which currently are *sqlite3* database and simple file storage in JSON format with base64 data-encoding.
82  * If you do not specify the storage mode, the default value 'database' is used.
83  *
84  * If you want to play the topic alongside their normal components you need to redirect their topic output.
85  * You can do this by adding the following property to all or some components:
86  * \verbatim
87  --ArmarX.TopicSuffix=/dev/null
88  \endverbatim
89  * This will add a topic suffix to all topics this component requested with the armarx::IceManager convenience functions
90  * (does not work if the component used Ice directly).
91  */
92 
94  public TopicRecorderInterface
95  {
96  public:
98 
99 
100  void startRecording(const int maxDuration, const Ice::Current& c = Ice::emptyCurrent) override;
101 
102  void stopRecording(const Ice::Current& c = Ice::emptyCurrent) override;
103 
104  void setOutputFilename(const std::string& newFilename, const Ice::Current& c = Ice::emptyCurrent) override
105  {
106  outputFilename = newFilename;
107  }
108 
109 
110  bool isRecording(const Ice::Current& c = Ice::emptyCurrent) override
111  {
112  return isRecordingEnabled;
113  }
114 
115 
116  // PropertyUser interface
118  void wakeUp();
119 
120  // ManagedIceObject interface
121  protected:
122  void onInitComponent() override;
123  void onConnectComponent() override;
124  void onDisconnectComponent() override;
125  void onExitComponent() override;
126  std::string getDefaultName() const override;
127 
128  void write();
129 
130  // Blobject interface
131 
132  protected:
134 
136 
138  std::map<std::string, GenericTopicSubscriberPtr> topicsSubscribers;
139  std::mutex queueMutex;
140  std::condition_variable idleCondition;
141  std::shared_ptr<std::ofstream> log;
142  std::filesystem::path outputfilePath;
143  std::string outputFilename;
144 
146 
148 
149  std::mutex mutex;
150 
151  TopicRecorderListenerInterfacePrx topicRecoderListener;
152 
153 
154  };
155 
156 } // namespace armarx
157 
158 
armarx::TopicRecorderComponent::writer
TopicWriterInterfacePtr writer
Definition: TopicRecorder.h:133
armarx::TopicRecorderComponent
Definition: TopicRecorder.h:93
armarx::TopicRecorderComponent::queueMutex
std::mutex queueMutex
Definition: TopicRecorder.h:139
armarx::TopicRecorderComponent::outputFilename
std::string outputFilename
Definition: TopicRecorder.h:143
armarx::TopicRecorderComponent::maxDuration
int maxDuration
Definition: TopicRecorder.h:145
armarx::TopicRecorderComponent::topicRecoderListener
TopicRecorderListenerInterfacePrx topicRecoderListener
Definition: TopicRecorder.h:151
armarx::TopicRecorderComponent::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: TopicRecorder.cpp:206
armarx::TopicRecorderComponent::mutex
std::mutex mutex
Definition: TopicRecorder.h:149
armarx::TopicRecorderComponent::stopRecording
void stopRecording(const Ice::Current &c=Ice::emptyCurrent) override
Definition: TopicRecorder.cpp:175
armarx::TopicRecorderComponent::topicsSubscribers
std::map< std::string, GenericTopicSubscriberPtr > topicsSubscribers
Definition: TopicRecorder.h:138
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
armarx::TopicRecorderComponent::startRecording
void startRecording(const int maxDuration, const Ice::Current &c=Ice::emptyCurrent) override
Definition: TopicRecorder.cpp:114
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::TopicRecorderComponent::startTime
IceUtil::Time startTime
Definition: TopicRecorder.h:135
RunningTask.h
TopicWriterInterface.h
armarx::TopicRecorderComponent::log
std::shared_ptr< std::ofstream > log
Definition: TopicRecorder.h:141
armarx::TopicRecorderComponent::isRecordingEnabled
bool isRecordingEnabled
Definition: TopicRecorder.h:147
GenericTopicSubscriber.h
armarx::TopicRecorderProperties::TopicRecorderProperties
TopicRecorderProperties(std::string prefix)
Definition: TopicRecorder.h:44
armarx::TopicRecorderComponent::isRecording
bool isRecording(const Ice::Current &c=Ice::emptyCurrent) override
Definition: TopicRecorder.h:110
armarx::TopicWriterInterfacePtr
std::shared_ptr< TopicWriterInterface > TopicWriterInterfacePtr
Definition: TopicWriterInterface.h:36
armarx::TopicRecorderComponent::write
void write()
Definition: TopicRecorder.cpp:216
armarx::TopicRecorderComponent::idleCondition
std::condition_variable idleCondition
Definition: TopicRecorder.h:140
armarx::TopicRecorderComponent::setOutputFilename
void setOutputFilename(const std::string &newFilename, const Ice::Current &c=Ice::emptyCurrent) override
Definition: TopicRecorder.h:104
armarx::TopicRecorderComponent::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: TopicRecorder.cpp:211
armarx::TopicRecorderComponent::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: TopicRecorder.cpp:63
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::TopicRecorderComponent::outputfilePath
std::filesystem::path outputfilePath
Definition: TopicRecorder.h:142
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::TopicRecorderComponent::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: TopicRecorder.cpp:201
armarx::TopicRecorderComponent::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: TopicRecorder.cpp:54
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::TopicRecorderComponent::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: TopicRecorder.cpp:42
armarx::TopicRecorderComponent::wakeUp
void wakeUp()
Definition: TopicRecorder.cpp:48
armarx::TopicRecorderComponent::queueTask
RunningTask< TopicRecorderComponent >::pointer_type queueTask
Definition: TopicRecorder.h:137
armarx::TopicRecorderProperties
Definition: TopicRecorder.h:40
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::TopicRecorderComponent::TopicRecorderComponent
TopicRecorderComponent()
Definition: TopicRecorder.cpp:37