1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 | #include "plog/Severity.h"
#include <kooling/component/classifier/interface.h>
#include <kooling/component/osm/interface.h>
#include <kooling/component/segmenter/interface.h>
#include <kooling/component/sensor/interface.h>
#include <kooling/component/trip/interface.h>
#include <kooling/json/json.h>
#include <plog/Init.h>
#include <plog/Log.h>
#include <plog/Appenders/ConsoleAppender.h>
#include <plog/Formatters/TxtFormatter.h>
#include <dotenv-cpp/dotenv.h>
#include <boost/program_options.hpp>
#include <csignal>
#include <cstdlib>
#include <exception>
#include <optional>
#include <filesystem>
#include <fstream>
namespace {
struct workflow
{
workflow() = default;
workflow(const workflow&) = delete;
workflow(workflow&&) = default;
workflow& operator=(const workflow&) = delete;
workflow& operator=(workflow&&) = default;
explicit workflow(kooling::component::sensor::interface::pointer_type sensor)
: d_sensor{ std::move(sensor) }
{}
operator bool() const
{
return static_cast<bool>(d_sensor);
}
void operator()() const
{
if (d_sensor)
{
d_sensor->run();
}
}
void stop() const
{
d_sensor->stop();
}
private:
kooling::component::sensor::interface::pointer_type d_sensor;
};
void init_env()
{
dotenv::init();
}
void init_logging()
{
struct formatter
{
static plog::util::nstring header()<--- The function 'header' is never used.
{
return {};
}
static plog::util::nstring format(const plog::Record& record) // This method returns a string from a record.
{
using namespace plog;
tm t;
util::gmtime_s(&t, &record.getTime().time);
util::nostringstream ss;
ss << t.tm_year + 1900 << "-" << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mon + 1 << PLOG_NSTR("-") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_mday << PLOG_NSTR(" ");
ss << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_hour << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_min << PLOG_NSTR(":") << std::setfill(PLOG_NSTR('0')) << std::setw(2) << t.tm_sec << PLOG_NSTR(".") << std::setfill(PLOG_NSTR('0')) << std::setw(3) << static_cast<int> (record.getTime().millitm) << PLOG_NSTR(" ");
ss << std::setfill(PLOG_NSTR(' ')) << std::setw(5) << std::left << severityToString(record.getSeverity()) << PLOG_NSTR(" ");
ss << PLOG_NSTR("[") << record.getTid() << PLOG_NSTR("] ");
//ss << PLOG_NSTR("[") << record.getFunc() << PLOG_NSTR("@") << record.getLine() << PLOG_NSTR("] ");
ss << record.getMessage() << PLOG_NSTR("\n");
return ss.str();
}
};
static plog::ConsoleAppender<formatter> consoleAppender;
plog::init(plog::verbose, &consoleAppender);
}
std::optional<json_t> make_config(const std::string& file)
{
const std::filesystem::path config_file{ file };
if (!std::filesystem::exists(config_file))
{
PLOG_ERROR << "Config file not found: " << config_file;
return {};
}
try
{
const auto config = json_t::parse(std::ifstream{ config_file });
PLOG_DEBUG
<< "sensor interface '" << config["sensor"]["type"] << "', "
<< "segmenter interface '" << config["segmenter"]["type"] << "', "
<< "classifier interface '" << config["classifier"]["type"] << "', "
<< "osm interface '" << config["osm"]["type"] << "', "
<< "trip interface '" << config["trip"]["type"] << "'";
return std::optional<json_t>{ config };
}
catch (const json_t::exception& e)
{
PLOG_ERROR << "Cannot parse config: " << e.what();
return {};
}
}
workflow make_workflow(const json_t& config)
{
auto trip{ kooling::component::trip::factory().create(config["trip"]) };
if (!trip)
{
PLOG_ERROR << "Could not initialize trip component";
return {};
}
auto osm{ kooling::component::osm::factory().create(config["osm"]) };
if (!osm)
{
PLOG_ERROR << "Could not initialize osm component";
return {};
}
auto classifier{ kooling::component::classifier::factory().create(config["classifier"]) };
if (!classifier)
{
PLOG_ERROR << "Could not initialize classifier component";
return {};
}
classifier->inject(std::move(osm));
classifier->inject(std::move(trip));
auto segmenter{ kooling::component::segmenter::factory().create(config["segmenter"]) };
if (!segmenter)
{
PLOG_ERROR << "Could not initialize segmenter component";
return {};
}
segmenter->inject(std::move(classifier));
auto sensor{ kooling::component::sensor::factory().create(config["sensor"]) };
if (!sensor)
{
PLOG_ERROR << "Could not initialize sensor component";
return {};
}
sensor->inject(std::move(segmenter));
return workflow{ std::move(sensor) };
}
} // unnamed namespace
static const workflow* workflow_ptr{ nullptr };
struct signal_handler_guard
{
explicit signal_handler_guard(const workflow& w)
{
workflow_ptr = &w;
std::signal(SIGINT, [](int) { workflow_ptr->stop(); });
}
~signal_handler_guard()
{
std::signal(SIGINT, SIG_DFL);
workflow_ptr = nullptr;
}
};
int main(int argc, char** argv)
{
init_env();
init_logging();
std::string config_file{};
namespace po = boost::program_options;
po::options_description desc{ "Allowed options" };
desc.add_options()
("help", "produce help message")
("config", po::value<std::string>(&config_file)->required(), "specify config file")
("dry-run", "dry run");
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help"))
{
std::cout << desc << "\n";
return 1;
}
po::notify(vm);
}
catch (std::exception& err)
{
std::cerr << err.what() << "\n";
return 1;
}
if (const auto config{ make_config(config_file) })
{
const std::string severity{ (*config)["log"]["severity"] };
plog::get()->setMaxSeverity(plog::severityFromString(severity.c_str()));
if (const auto workflow{ make_workflow(*config) })
{
if (vm.count("dry-run") || std::getenv("KOOLING_DRY_RUN"))
{
PLOG_DEBUG << "Dry run finished";
}
else
{
signal_handler_guard guard{ workflow };
workflow();<--- Instance of 'workflow' object is destroyed immediately.
}
return 0;
}
}
return 1;
}
|