Merge lp:~thomas-voss/location-service/enable-kml-reporting into lp:location-service

Proposed by Thomas Voß
Status: Merged
Approved by: Thomas Voß
Approved revision: 309
Merged at revision: 307
Proposed branch: lp:~thomas-voss/location-service/enable-kml-reporting
Merge into: lp:location-service
Diff against target: 256 lines (+117/-13)
5 files modified
CMakeLists.txt (+1/-1)
debian/control (+1/-0)
snapcraft.yaml (+1/-0)
src/location/cmds/monitor.cpp (+78/-7)
src/location/cmds/monitor.h (+36/-5)
To merge this branch: bzr merge lp:~thomas-voss/location-service/enable-kml-reporting
Reviewer Review Type Date Requested Status
Simon Fels (community) Approve
Thomas Voß Pending
Review via email: mp+323851@code.launchpad.net

Commit message

Add reporting in KML format to locationd.monitor.

Description of the change

Add reporting in KML format to locationd.monitor.

To post a comment you must log in.
309. By Thomas Voß

Add boost-locale build dep.

Revision history for this message
Simon Fels (morphis) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2017-03-07 10:28:49 +0000
3+++ CMakeLists.txt 2017-05-10 14:36:23 +0000
4@@ -52,7 +52,7 @@
5 include(GNUInstallDirs)
6
7 find_package(PkgConfig)
8-find_package(Boost COMPONENTS filesystem system program_options)
9+find_package(Boost COMPONENTS filesystem locale system program_options)
10 find_package(GLog)
11 find_package(GFlags)
12 find_package(Threads)
13
14=== modified file 'debian/control'
15--- debian/control 2017-03-30 12:47:19 +0000
16+++ debian/control 2017-05-10 14:36:23 +0000
17@@ -19,6 +19,7 @@
18 graphviz,
19 libapparmor-dev,
20 libboost-filesystem-dev,
21+ libboost-locale-dev,
22 libboost-program-options-dev,
23 libboost-system-dev,
24 libdbus-1-dev,
25
26=== modified file 'snapcraft.yaml'
27--- snapcraft.yaml 2017-03-30 12:47:19 +0000
28+++ snapcraft.yaml 2017-05-10 14:36:23 +0000
29@@ -65,6 +65,7 @@
30 - g++
31 - libapparmor-dev
32 - libboost-filesystem-dev
33+ - libboost-locale-dev
34 - libboost-program-options-dev
35 - libboost-system-dev
36 - libc6-dev
37
38=== modified file 'src/location/cmds/monitor.cpp'
39--- src/location/cmds/monitor.cpp 2017-05-05 09:37:12 +0000
40+++ src/location/cmds/monitor.cpp 2017-05-10 14:36:23 +0000
41@@ -25,33 +25,36 @@
42 #include <location/glib/runtime.h>
43 #include <location/runtime.h>
44
45+#include <boost/locale.hpp>
46+
47+#include <iostream>
48 #include <type_traits>
49
50 namespace cli = location::util::cli;
51
52-location::cmds::Monitor::PrintingDelegate::PrintingDelegate(std::ostream& out) : out{out}
53+location::cmds::Monitor::TableOutputDelegate::TableOutputDelegate(std::ostream& out) : out{out}
54 {
55 }
56
57-void location::cmds::Monitor::PrintingDelegate::on_new_position(const Update<Position>& pos)
58+void location::cmds::Monitor::TableOutputDelegate::on_new_position(const Update<Position>& pos)
59 {
60 last_position_update = pos;
61 print_row();
62 }
63
64-void location::cmds::Monitor::PrintingDelegate::on_new_heading(const Update<units::Degrees>& heading)
65+void location::cmds::Monitor::TableOutputDelegate::on_new_heading(const Update<units::Degrees>& heading)
66 {
67 last_heading_update = heading;
68 print_row();
69 }
70
71-void location::cmds::Monitor::PrintingDelegate::on_new_velocity(const Update<units::MetersPerSecond>& velocity)
72+void location::cmds::Monitor::TableOutputDelegate::on_new_velocity(const Update<units::MetersPerSecond>& velocity)
73 {
74 last_velocity_update = velocity;
75 print_row();
76 }
77
78-void location::cmds::Monitor::PrintingDelegate::print_header()
79+void location::cmds::Monitor::TableOutputDelegate::print_header()
80 {
81 out << std::left << std::setw(15) << std::setfill(' ') << "lat.[deg]"
82 << std::left << std::setw(15) << std::setfill(' ') << "lon.[deg]"
83@@ -62,7 +65,7 @@
84 << std::left << std::setw(15) << std::setfill(' ') << "vel.[m/s]" << std::endl;
85 }
86
87-void location::cmds::Monitor::PrintingDelegate::print_row()
88+void location::cmds::Monitor::TableOutputDelegate::print_row()
89 {
90 if (last_position_update)
91 {
92@@ -115,17 +118,73 @@
93
94 }
95
96+location::cmds::Monitor::KmlOutputDelegate::KmlOutputDelegate(std::ostream& out) : out{out}
97+{
98+ // We have to imbue the right locale to make sure that formatting
99+ // of floating point numbers is correct.
100+ out.imbue(std::locale("C"));
101+
102+ out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
103+ << "<kml xmlns=\"http://www.opengis.net/kml/2.2\">"
104+ << "<Folder><open>1</open><name>Recorded positions</name>";
105+}
106+
107+location::cmds::Monitor::KmlOutputDelegate::~KmlOutputDelegate()
108+{
109+ out << "</Folder></kml>";
110+}
111+
112+void location::cmds::Monitor::KmlOutputDelegate::on_new_position(const Update<Position>& pos)
113+{
114+ static constexpr const char* placemark_pattern
115+ {
116+ "<Placemark><name>{1}</name><Point><coordinates>{2},{3},{4}</coordinates></Point></Placemark>"
117+ };
118+
119+ out << boost::locale::format(placemark_pattern)
120+ % pos.when.time_since_epoch().count()
121+ % pos.value.longitude().value()
122+ % pos.value.latitude().value()
123+ % (pos.value.altitude() ? pos.value.altitude()->value() : 0.);
124+}
125+
126+void location::cmds::Monitor::KmlOutputDelegate::on_new_heading(const Update<units::Degrees>&)
127+{
128+ // Empty on purpose.
129+}
130+
131+void location::cmds::Monitor::KmlOutputDelegate::on_new_velocity(const Update<units::MetersPerSecond>&)
132+{
133+ // Empty on purpose.
134+}
135+
136 location::cmds::Monitor::Monitor(const std::shared_ptr<Delegate>& delegate)
137 : CommandWithFlagsAndAction{cli::Name{"monitor"}, cli::Usage{"monitor"}, cli::Description{"monitors the daemon"}},
138 delegate{delegate},
139- bus{dbus::Bus::system}
140+ bus{dbus::Bus::system},
141+ output_format{OutputFormat::table}
142 {
143 flag(cli::make_flag(cli::Name{"bus"}, cli::Description{"bus instance to connect to, defaults to system"}, bus));
144+ flag(cli::make_flag(cli::Name{"output-format"}, cli::Description{"output format in {table, kml}"}, output_format));
145+
146 action([this](const Context& ctxt)
147 {
148 glib::Runtime runtime{glib::Runtime::WithOwnMainLoop{}};
149 runtime.redirect_logging();
150
151+ if (!Monitor::delegate)
152+ {
153+ switch (output_format)
154+ {
155+ case OutputFormat::table:
156+ Monitor::delegate = std::make_shared<TableOutputDelegate>();
157+ break;
158+ case OutputFormat::kml:
159+ Monitor::delegate = std::make_shared<KmlOutputDelegate>();
160+ break;
161+ }
162+ }
163+
164 location::dbus::stub::Service::create(bus, [this, &ctxt](const Result<location::dbus::stub::Service::Ptr>& result) mutable
165 {
166 if (!result)
167@@ -171,3 +230,15 @@
168 return runtime.run();
169 });
170 }
171+
172+std::istream& location::cmds::operator>>(std::istream& in, location::cmds::Monitor::OutputFormat& format)
173+{
174+ std::string s; in >> s;
175+
176+ if (s == "kml")
177+ format = location::cmds::Monitor::OutputFormat::kml;
178+ if (s == "table")
179+ format = location::cmds::Monitor::OutputFormat::table;
180+
181+ return in;
182+}
183
184=== modified file 'src/location/cmds/monitor.h'
185--- src/location/cmds/monitor.h 2017-05-05 09:37:12 +0000
186+++ src/location/cmds/monitor.h 2017-05-10 14:36:23 +0000
187@@ -38,6 +38,13 @@
188 class LOCATION_DLL_PUBLIC Monitor : public util::cli::CommandWithFlagsAndAction
189 {
190 public:
191+ // Format enumerates all known output formats.
192+ enum class OutputFormat
193+ {
194+ table, // Prints a table of values.
195+ kml // Prints incoming updates as kml.
196+ };
197+
198 // Delegate abstracts handling of incoming updates.
199 class Delegate : public util::DoNotCopyOrMove
200 {
201@@ -51,11 +58,11 @@
202 };
203
204 // PrintingDelegate implements Delegate, printing updates to the given output stream.
205- class PrintingDelegate : public Delegate
206+ class TableOutputDelegate : public Delegate
207 {
208 public:
209 // PrintingDelegate initializes a new instance with out.
210- PrintingDelegate(std::ostream& out = std::cout);
211+ TableOutputDelegate(std::ostream& out = std::cout);
212
213 // From Delegate
214 void on_new_position(const Update<Position>& pos) override;
215@@ -72,14 +79,38 @@
216 Optional<Update<units::MetersPerSecond>> last_velocity_update;
217 };
218
219+ class KmlOutputDelegate : public Delegate
220+ {
221+ public:
222+ // KmlDelegate initializes a new instance with out.
223+ KmlOutputDelegate(std::ostream& out = std::cout);
224+ ~KmlOutputDelegate();
225+
226+ // From Delegate
227+ void on_new_position(const Update<Position>& pos) override;
228+ void on_new_heading(const Update<units::Degrees>& heading) override;
229+ void on_new_velocity(const Update<units::MetersPerSecond>& velocity) override;
230+
231+ private:
232+ std::ostream& out;
233+ Optional<Update<Position>> last_position_update;
234+ Optional<Update<units::Degrees>> last_heading_update;
235+ Optional<Update<units::MetersPerSecond>> last_velocity_update;
236+ };
237+
238 // Monitor initializes a new instance.
239- Monitor(const std::shared_ptr<Delegate>& delegate = std::make_shared<PrintingDelegate>());
240+ Monitor(const std::shared_ptr<Delegate>& delegate = std::shared_ptr<Delegate>{});
241
242 private:
243 std::shared_ptr<Delegate> delegate; // All updates are forwarded to a delegate.
244 dbus::Bus bus; // The bus we should connect to.
245+ OutputFormat output_format; // The output format.
246 };
247-}
248-}
249+
250+// operator>> parses format from in.
251+std::istream& operator>>(std::istream& in, Monitor::OutputFormat& format);
252+
253+} // namespace cmds
254+} // namespace location
255
256 #endif // LOCATION_CMDS_MONITOR_H_

Subscribers

People subscribed via source and target branches

to all changes: