This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
threads.cc
197 lines (162 loc) · 5.14 KB
/
threads.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "threads.h"
#include <cstdlib>
#include <vector>
#include <sstream>
#include <string>
#include <thread>
#include <sys/time.h>
#include <sys/sysinfo.h>
#include "logger/Logger.h"
#include "camera/Camera.h"
#include "gsm/GSM.h"
using namespace std;
using namespace os;
void os::system_thread_fn(State& state)
{
struct timeval timer;
gettimeofday(&timer, NULL);
struct tm * now = gmtime(&timer.tv_sec);
Logger cpu_logger("data/logs/system/CPU."+ to_string(now->tm_year+1900) +"-"+ to_string(now->tm_mon+1) +"-"+
to_string(now->tm_mday) +"."+ to_string(now->tm_hour) +"-"+ to_string(now->tm_min) +"-"+
to_string(now->tm_sec) +".log", "CPU");
Logger ram_logger("data/logs/system/RAM."+ to_string(now->tm_year+1900) +"-"+ to_string(now->tm_mon+1) +"-"+
to_string(now->tm_mday) +"."+ to_string(now->tm_hour) +"-"+ to_string(now->tm_min) +"-"+
to_string(now->tm_sec) +".log", "RAM");
Logger temp_logger("data/logs/system/Temp."+ to_string(now->tm_year+1900) +"-"+ to_string(now->tm_mon+1) +"-"+
to_string(now->tm_mday) +"."+ to_string(now->tm_hour) +"-"+ to_string(now->tm_min) +"-"+
to_string(now->tm_sec) +".log", "Temp");
FILE *gpu_temp_process, *cpu_command_process;
char gpu_response[11];
char cpu_command[100];
struct sysinfo info;
while (state != SHUT_DOWN)
{
if (get_available_disk_space() < 2000000000)
{
Camera::get_instance().stop();
}
ifstream cpu_temp_file("/sys/class/thermal/thermal_zone0/temp");
string cpu_temp_str((istreambuf_iterator<char>(cpu_temp_file)),
istreambuf_iterator<char>());
cpu_temp_file.close();
gpu_temp_process = popen("/opt/vc/bin/vcgencmd measure_temp", "r");
if (fgets(gpu_response, 11, gpu_temp_process) != NULL) {
try
{
temp_logger.log("CPU: "+to_string(stoi(cpu_temp_str)/1000.0)+
" GPU: "+string(gpu_response).substr(5, 4));
}
catch (const invalid_argument& ia) {}
}
pclose(gpu_temp_process);
cpu_command_process = popen("grep 'cpu ' /proc/stat", "r");
if (fgets(cpu_command, 100, cpu_command_process) != NULL)
{
const string cpu_command_str = string(cpu_command);
stringstream ss(cpu_command_str);
string data;
vector<string> s_data;
// We put all fields in a vector
while(getline(ss, data, ' '))
{
s_data.push_back(data);
}
if (s_data.size() >= 6)
{
try {
// Note that s_data[1] is ""
cpu_logger.log(to_string((stof(s_data[2])+stof(s_data[4]))/
(stof(s_data[2])+stof(s_data[4])+stof(s_data[5]))));
}
catch (const invalid_argument& ia) {}
}
}
pclose(cpu_command_process);
sysinfo(&info);
ram_logger.log(to_string(((double) info.freeram)/info.totalram));
this_thread::sleep_for(30s);
}
}
void os::picture_thread_fn(State& state)
{
struct timeval timer;
gettimeofday(&timer, NULL);
struct tm * now = gmtime(&timer.tv_sec);
Logger logger("data/logs/camera/Pictures."+ to_string(now->tm_year+1900) +"-"+ to_string(now->tm_mon) +"-"+
to_string(now->tm_mday) +"."+ to_string(now->tm_hour) +"-"+ to_string(now->tm_min) +"-"+
to_string(now->tm_sec) +".log", "Pictures");
logger.log("Waiting for launch...");
while (state != GOING_UP)
{
this_thread::sleep_for(10s);
}
logger.log("Launched, waiting 2 minutes for first picture...");
this_thread::sleep_for(2min);
while (state == GOING_UP)
{
logger.log("Taking picture...");
if ( ! Camera::get_instance().take_picture(generate_exif_data()))
{
logger.log("Error taking picture. Trying again in 30 seconds...");
}
else
{
logger.log("Picture taken correctly. Next picture in 30 seconds...");
}
this_thread::sleep_for(30s);
logger.log("Taking picture...");
if ( ! Camera::get_instance().take_picture(generate_exif_data()))
{
logger.log("Error taking picture. Next picture in 4 minutes...");
}
else
{
logger.log("Picture taken correctly. Next picture in 4 minutes...");
}
this_thread::sleep_for(4min);
}
logger.log("Going down, no more pictures are being taken, picture thread is closing.");
}
void os::battery_thread_fn(State& state)
{
struct timeval timer;
gettimeofday(&timer, NULL);
struct tm * now = gmtime(&timer.tv_sec);
Logger logger("data/logs/GSM/Battery."+ to_string(now->tm_year+1900) +"-"+ to_string(now->tm_mon) +"-"+
to_string(now->tm_mday) +"."+ to_string(now->tm_hour) +"-"+ to_string(now->tm_min) +"-"+
to_string(now->tm_sec) +".log", "Battery");
double main_battery, gsm_battery;
while (state != SHUT_DOWN)
{
if (GSM::get_instance().is_on())
{
GSM::get_instance().get_battery_status(main_battery, gsm_battery);
logger.log("Main: "+ to_string(main_battery));
logger.log("GSM: "+ to_string(gsm_battery));
}
else
{
for (int i = 0; i < 5; ++i)
{
this_thread::sleep_for(2min);
if (state == SHUT_DOWN)
{
return;
}
}
if ( ! GSM::get_instance().is_on())
{
GSM::get_instance().turn_on();
this_thread::sleep_for(30s);
}
GSM::get_instance().get_battery_status(main_battery, gsm_battery);
logger.log("Main: "+ to_string(main_battery));
logger.log("GSM: "+ to_string(gsm_battery));
if (state == GOING_DOWN || GPS::get_instance().get_altitude() > 1200)
{
GSM::get_instance().turn_off();
}
}
this_thread::sleep_for(3min);
}
}