5G-MAG Reference Tools - MBMS Modem
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MeasurementFileWriter Class Reference

Writes measurement data / current reception parameters to a file. More...

#include <MeasurementFileWriter.h>

Collaboration diagram for MeasurementFileWriter:
Collaboration graph

Public Member Functions

 MeasurementFileWriter (const libconfig::Config &cfg)
 Default constructor. More...
 
virtual ~MeasurementFileWriter ()
 Default destructor. More...
 
void WriteLogValues (const std::vector< std::string > &values)
 Write a line containing the passed values. More...
 

Private Member Functions

void ReadGps ()
 

Private Attributes

const libconfig::Config & _cfg
 
std::unique_ptr< gpsmm > _gps
 
struct gps_data_t * _gps_data {}
 
std::thread _gps_reader_thread
 
bool _running = true
 
std::string _last_gps_lat = ""
 
std::string _last_gps_lng = ""
 
std::string _last_gps_time = ""
 

Detailed Description

Writes measurement data / current reception parameters to a file.

Definition at line 35 of file MeasurementFileWriter.h.

Constructor & Destructor Documentation

◆ MeasurementFileWriter()

MeasurementFileWriter::MeasurementFileWriter ( const libconfig::Config &  cfg)
explicit

Default constructor.

Parameters
cfgConfig singleton reference

Definition at line 37 of file MeasurementFileWriter.cpp.

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 }
std::unique_ptr< gpsmm > _gps
const libconfig::Config & _cfg
static Config cfg
Global configuration object.
Definition: main.cpp:165

◆ ~MeasurementFileWriter()

MeasurementFileWriter::~MeasurementFileWriter ( )
virtual

Default destructor.

Definition at line 61 of file MeasurementFileWriter.cpp.

61  {
62  _running = false;
63  _gps_reader_thread.join();
64 }

Member Function Documentation

◆ ReadGps()

void MeasurementFileWriter::ReadGps ( )
private

Definition at line 66 of file MeasurementFileWriter.cpp.

66  {
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 }
const uint32_t kGpsWaitMicrosleep
const uint32_t kGpsWaitTimeout
const uint32_t kMaxTimestringSize
const uint32_t kGpsReconnectTimeout
struct gps_data_t * _gps_data

◆ WriteLogValues()

void MeasurementFileWriter::WriteLogValues ( const std::vector< std::string > &  values)

Write a line containing the passed values.

This methods always writes the first four columns containing:

  • the current timestamp
  • the GPS latitude (if a GPS device was configured)
  • the GPS longitude (if a GPS device was configured)
  • the GPS time (if a GPS device was configured)

followed by the values in the values vector.

Definition at line 100 of file MeasurementFileWriter.cpp.

101  {
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 }

Member Data Documentation

◆ _cfg

const libconfig::Config& MeasurementFileWriter::_cfg
private

Definition at line 65 of file MeasurementFileWriter.h.

◆ _gps

std::unique_ptr<gpsmm> MeasurementFileWriter::_gps
private

Definition at line 67 of file MeasurementFileWriter.h.

◆ _gps_data

struct gps_data_t* MeasurementFileWriter::_gps_data {}
private

Definition at line 68 of file MeasurementFileWriter.h.

◆ _gps_reader_thread

std::thread MeasurementFileWriter::_gps_reader_thread
private

Definition at line 69 of file MeasurementFileWriter.h.

◆ _last_gps_lat

std::string MeasurementFileWriter::_last_gps_lat = ""
private

Definition at line 72 of file MeasurementFileWriter.h.

◆ _last_gps_lng

std::string MeasurementFileWriter::_last_gps_lng = ""
private

Definition at line 73 of file MeasurementFileWriter.h.

◆ _last_gps_time

std::string MeasurementFileWriter::_last_gps_time = ""
private

Definition at line 74 of file MeasurementFileWriter.h.

◆ _running

bool MeasurementFileWriter::_running = true
private

Definition at line 70 of file MeasurementFileWriter.h.


The documentation for this class was generated from the following files: