Added a getter method for the tmp_dir.
[ruwai.git] / software / c++ / ruwaicom / include / recorder.h
blobddd8741435600cb11d99087da0a206dc0b13c543
1 /*--------------------------------------------------------------------------*/
2 // LICENSE
3 //
4 // This file is part of ruwai.
5 //
6 // If you use ruwai_parser in any program or publication, please inform and
7 // acknowledge its author Stefan Mertl (stefan@mertl-research.at).
8 //
9 // ruwai is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /*--------------------------------------------------------------------------*/
23 #ifndef RECORDER_H
24 #define RECORDER_H
26 #include <iostream>
27 #include <vector>
28 #include <map>
29 #include <mutex>
30 #include <thread>
31 #include <chrono>
33 #include <cmath>
34 #include <cstdlib>
35 #include <cstring>
36 #include <ctime>
37 #include <errno.h>
38 #include <syslog.h>
40 #define BOOST_NO_CXX11_SCOPED_ENUMS // Bug fix for an linking error caused by boost::filesystem::copy_file
41 #define BOOST_NO_SCOPED_ENUMS // This define has to be used for boost versions below 1.51. BBB has 1.49
42 #include <boost/format.hpp>
43 #include <boost/filesystem.hpp>
44 #include <boost/algorithm/string.hpp>
45 #undef BOOST_NO_CXX11_SCOPED_ENUMS
46 #undef BOOST_NO_SCOPED_ENUMS
48 //#include <sys/types.h>
49 //#include <sys/stat.h>
50 //#include <unistd.h>
51 //#include <stdio.h>
53 #include <libmseed/libmseed.h>
55 #include "rw_protocol.h"
57 //! The supported GPSfix types.
58 typedef enum gpsfix_e {
59 FIX_NO = 0,
60 FIX_DEAD_RECKONING_ONLY = 1,
61 FIX_2D = 2,
62 FIX_3D = 3,
63 FIX_GPS_AND_DEAD_RECKONING = 4,
64 FIX_TIME_ONLY = 5
65 } gpsfix_t;
68 typedef struct timestamp_s {
69 bool utc_available;
70 bool utc_timebase;
71 bool gps_fix_ok;
72 gpsfix_t gps_fix;
73 uint32_t sec_slts;
74 hptime_t time;
75 } timestamp_t;
77 void record_handler (char *record, int reclen, void *ptr);
79 class Recorder {
80 public:
81 Recorder(std::string serial_number, unsigned int total_channels, std::vector<unsigned int> record_channels, unsigned int sps, std::string sd_mnt, std::string tmp_dir, unsigned int file_length);
82 void add_sample(rw_smp_24bit_t paket);
83 void add_timestamp_sample(rw_smp_24bit_tsp_t paket);
84 void handle_timestamp_sample(rw_smp_24bit_tsp_t paket);
85 void pack_samplebuffer(void);
86 bool check_output_dir_structure(void);
87 void copy_files(std::vector<std::string> files);
88 timestamp_t get_last_timestamp(void);
89 std::string get_tmp_dir(void);
91 private:
92 std::string serial_number; // The serial number of the ruwai Arduino stack.
93 unsigned int sps; // The sampling rate of the data.
94 unsigned int n_channels; // The number of channels to record.
95 std::vector<unsigned int> record_channels; // The channel number to record to file.
96 unsigned int write_limit; // The size of the sample_buffer when to copy the samples to the ms_trace.
98 std::mutex mut_sample_buffer;
99 std::mutex mut_timestamps;
100 std::mutex mut_last_timestamp;
104 std::string sd_mnt_point; // The name of the SD card mount point.
105 std::string data_dir; // The name of the folder where to store the miniseed files.
106 std::string tmp_dir; // The name of the folder where to store the data files which are currently used for recording.
108 std::vector <std::vector <std::int32_t> > sample_buffer; // The buffers holding the samples.
109 std::vector <std::vector <int> > timestamp_pos; // The timestamp positions in the sample vector.
110 std::vector <timestamp_t> timestamps; // The timestamp values.
111 std::vector <MSTrace *> traces; // The miniseed traces. One for each channel.
113 //MSTraceGroup *mstg;
115 //hptime_t span_start;
116 //hptime_t span_end;
117 unsigned int file_length; // The lenght of the data files in seconds.
118 double tow_sub_ms_scale; // The scaling factor of the tow_sub_ms value of the GPS timing paket.
120 // The state of the recorder.
121 bool state_first_timestamp_received;
122 bool state_gps_fix_init;
123 //gpsfix_t state_gps_fix;
124 //bool state_gps_fix_ok;
125 int sec_slts_cnt;
126 timestamp_t last_timestamp;
129 void init_msr(MSRecord* msr, unsigned int channel_number);
130 void set_msr_timeflags(MSRecord* msr, std::vector <timestamp_t>& timestamps);
131 bool msr_fits_to_end(MSTrace* mst, MSRecord* msr);
132 std::string get_filename(BTime start_time, char* channel);
133 std::string write_trace_to_file(MSTrace* mst, bool flush, MSRecord* msr_template);
136 #endif /* RECORDER_H */