FileTopicWriter.cpp
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 #include "FileTopicWriter.h"
23 
27 
28 #include <fstream>
29 #include <sstream>
30 #include <string>
31 
32 namespace armarx
33 {
34 
35  FileTopicWriter::FileTopicWriter(const std::filesystem::path& path) :
36  filepath(path)
37  {
38  std::ofstream* o = new std::ofstream(path.string().c_str());
39  if (!o->is_open())
40  {
41  ARMARX_ERROR_S << "Could not open '" << path.string() << "'!";
42  }
43  stream = o;
44  }
45 
46  FileTopicWriter::FileTopicWriter(std::ostream* stream) :
47  stream(stream)
48  {
49 
50  }
51 
53  {
54  if (!filepath.empty())
55  {
56  delete stream;
57  }
58  }
59 
60  std::filesystem::path FileTopicWriter::getFilepath() const
61  {
62  return filepath;
63  }
64 
66  {
67  JSONObjectPtr json = new JSONObject();
68  json->setString("topic", topicData.topicName);
69  json->setString("op", topicData.operationName);
70  json->setDouble("t", topicData.timestamp.toMicroSecondsDouble());
71  json->setString("data", TopicUtil::Encode64(std::string(topicData.inParams.begin(), topicData.inParams.end())));
72 
73  (*stream) << json->asString() << std::endl;
74  }
75 
76 } // namespace armarx
JSONObject.h
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
armarx::FileTopicWriter::getFilepath
std::filesystem::path getFilepath() const
Definition: FileTopicWriter.cpp:60
armarx::TopicUtil::TopicData::inParams
std::vector< Ice::Byte > inParams
Definition: TopicUtil.h:45
IceInternal::Handle< JSONObject >
armarx::TopicUtil::TopicData
Definition: TopicUtil.h:34
FileTopicWriter.h
armarx::FileTopicWriter::~FileTopicWriter
~FileTopicWriter() override
Definition: FileTopicWriter.cpp:52
armarx::TopicUtil::TopicData::operationName
std::string operationName
Definition: TopicUtil.h:44
armarx::FileTopicWriter::FileTopicWriter
FileTopicWriter(const std::filesystem::path &path)
Definition: FileTopicWriter.cpp:35
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::TopicUtil::TopicData::topicName
std::string topicName
Definition: TopicUtil.h:42
ExpressionException.h
armarx::TopicUtil::TopicData::timestamp
IceUtil::Time timestamp
Definition: TopicUtil.h:43
armarx::TopicUtil::Encode64
std::string Encode64(const std::string &val)
Definition: TopicUtil.cpp:34
Logging.h
armarx::FileTopicWriter::write
void write(const TopicUtil::TopicData &topicData) override
Definition: FileTopicWriter.cpp:65
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28