20 #include "MeasurementFileWriter.h"
30 #include "spdlog/spdlog.h"
32 const uint32_t kGpsWaitTimeout = 5000000;
33 const uint32_t kGpsReconnectTimeout = 5000;
34 const uint32_t kGpsWaitMicrosleep = 100;
35 const uint32_t kMaxTimestringSize = 80;
39 bool gps_enabled =
false;
40 _cfg.lookupValue(
"measurement_file.gpsd.enabled", gps_enabled);
43 std::string host =
"localhost";
44 _cfg.lookupValue(
"measurement_file.gpsd.host", host);
46 std::string port = DEFAULT_GPSD_PORT;
47 _cfg.lookupValue(
"measurement_file.gpsd.port", port);
49 _gps = std::make_unique<gpsmm>(host.c_str(), port.c_str());
51 if (_gps->stream(WATCH_ENABLE | WATCH_JSON) ==
nullptr) {
52 spdlog::error(
"No GPSD running, cannot start GPS stream.");
54 spdlog::info(
"GPS data stream started");
57 _gps_reader_thread = std::thread{&MeasurementFileWriter::ReadGps,
this};
63 _gps_reader_thread.join();
66 void MeasurementFileWriter::ReadGps() {
68 if (_gps->is_open()) {
69 if (!_gps->waiting(kGpsWaitTimeout)) {
75 if ((_gps_data = _gps->read()) !=
nullptr) {
76 if ((_gps_data->set & TIME_SET) != 0) {
77 struct tm ts = *localtime(&_gps_data->fix.time.tv_sec);
79 buf.resize(kMaxTimestringSize);
80 strftime(&buf[0], buf.size(),
"%FT%T", &ts);
83 if ((_gps_data->set & LATLON_SET) != 0) {
84 _last_gps_lat = std::to_string(_gps_data->fix.latitude);
85 _last_gps_lng = std::to_string(_gps_data->fix.longitude);
88 _last_gps_lat = _last_gps_lng = _last_gps_time =
"";
91 std::this_thread::sleep_for(std::chrono::microseconds(kGpsWaitMicrosleep));
93 _last_gps_lat = _last_gps_lng = _last_gps_time =
"";
94 std::this_thread::sleep_for(std::chrono::microseconds(kGpsReconnectTimeout));
95 _gps->stream(WATCH_ENABLE|WATCH_JSON);
101 const std::vector<std::string>& values) {
102 time_t now = time(
nullptr);
103 struct tm ts = *localtime(&now);
105 buf.resize(kMaxTimestringSize);
106 strftime(&buf[0], buf.size(),
"%FT%T", &ts);
108 std::vector<std::string> cols = {buf, _last_gps_lat,
109 _last_gps_lng, _last_gps_time};
112 for (
const auto& col : cols) {
117 for (
const auto& val : values) {
123 std::string file_loc =
"/tmp/rp_measurements.csv";
124 _cfg.lookupValue(
"measurement_file.file_path", file_loc);
126 file.open(file_loc, std::ios_base::app);
127 file << line << std::endl;