forked from OpenStratos/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.cc
More file actions
153 lines (119 loc) · 4.3 KB
/
threads.cc
File metadata and controls
153 lines (119 loc) · 4.3 KB
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
#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) +"-"+
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) +"-"+
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) +"-"+
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)
{
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");
fgets(gpu_response, 11, gpu_temp_process);
pclose(gpu_temp_process);
temp_logger.log("CPU: "+to_string(stoi(cpu_temp_str)/1000.0)+" GPU: "+
string(gpu_response).substr(5, 4));
cpu_command_process = popen("grep 'cpu ' /proc/stat", "r");
fgets(cpu_command, 100, cpu_command_process);
pclose(cpu_command_process);
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);
// 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]))));
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().get_status())
{
GSM::get_instance().get_battery_status(main_battery, gsm_battery);
logger.log("Main: "+ to_string(main_battery));
logger.log("GSM: "+ to_string(gsm_battery));
}
this_thread::sleep_for(3min);
}
}