5G-MAG Reference Tools - MBMS Modem
MeasurementFileWriter.cpp
Go to the documentation of this file.
1 // 5G-MAG Reference Tools
2 // MBMS Modem Process
3 //
4 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Affero General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Affero General Public License for more details.
15 //
16 // You should have received a copy of the GNU Affero General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #include "MeasurementFileWriter.h"
21 
22 #include <cerrno>
23 #include <chrono>
24 #include <cmath>
25 #include <fstream>
26 #include <memory>
27 #include <numeric>
28 #include <vector>
29 
30 #include "spdlog/spdlog.h"
31 
32 const uint32_t kGpsWaitTimeout = 5000000;
33 const uint32_t kGpsReconnectTimeout = 5000;
34 const uint32_t kGpsWaitMicrosleep = 100;
35 const uint32_t kMaxTimestringSize = 80;
36 
38  : _cfg(cfg) {
39  bool gps_enabled = false;
40  _cfg.lookupValue("modem.measurement_file.gpsd.enabled", gps_enabled);
41 
42  if (gps_enabled) {
43  std::string host = "localhost";
44  _cfg.lookupValue("modem.measurement_file.gpsd.host", host);
45 
46  std::string port = DEFAULT_GPSD_PORT;
47  _cfg.lookupValue("modem.measurement_file.gpsd.port", port);
48 
49  _gps = std::make_unique<gpsmm>(host.c_str(), port.c_str());
50 
51  if (_gps->stream(WATCH_ENABLE | WATCH_JSON) == nullptr) {
52  spdlog::error("No GPSD running, cannot start GPS stream.");
53  } else {
54  spdlog::info("GPS data stream started");
55  }
56 
58  }
59 }
60 
62  _running = false;
63  _gps_reader_thread.join();
64 }
65 
67  while (_running) {
68  if (_gps->is_open()) {
69  if (!_gps->waiting(kGpsWaitTimeout)) {
70  {
71  continue;
72  }
73  }
74 
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);
78  std::string buf;
79  buf.resize(kMaxTimestringSize);
80  strftime(&buf[0], buf.size(), "%FT%T", &ts);
81  _last_gps_time = buf;
82  }
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);
86  }
87  } else {
89  }
90 
91  std::this_thread::sleep_for(std::chrono::microseconds(kGpsWaitMicrosleep));
92  } else {
94  std::this_thread::sleep_for(std::chrono::microseconds(kGpsReconnectTimeout));
95  _gps->stream(WATCH_ENABLE|WATCH_JSON);
96  }
97  }
98 }
99 
101  const std::vector<std::string>& values) {
102  time_t now = time(nullptr);
103  struct tm ts = *localtime(&now);
104  std::string buf;
105  buf.resize(kMaxTimestringSize);
106  strftime(&buf[0], buf.size(), "%FT%T", &ts);
107 
108  std::vector<std::string> cols = {buf, _last_gps_lat,
110 
111  std::string line;
112  for (const auto& col : cols) {
113  {
114  line += col + ";";
115  }
116  }
117  for (const auto& val : values) {
118  {
119  line += val + ";";
120  }
121  }
122 
123  std::string file_loc = "/tmp/modem_measurements.csv";
124  _cfg.lookupValue("modem.measurement_file.file_path", file_loc);
125  std::ofstream file;
126  file.open(file_loc, std::ios_base::app);
127  file << line << std::endl;
128  file.close();
129 }
const uint32_t kGpsWaitMicrosleep
const uint32_t kGpsWaitTimeout
const uint32_t kMaxTimestringSize
const uint32_t kGpsReconnectTimeout
std::unique_ptr< gpsmm > _gps
virtual ~MeasurementFileWriter()
Default destructor.
const libconfig::Config & _cfg
void WriteLogValues(const std::vector< std::string > &values)
Write a line containing the passed values.
MeasurementFileWriter(const libconfig::Config &cfg)
Default constructor.
struct gps_data_t * _gps_data
static Config cfg
Global configuration object.
Definition: main.cpp:165