-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_monitor.cpp
More file actions
73 lines (64 loc) · 2.91 KB
/
temp_monitor.cpp
File metadata and controls
73 lines (64 loc) · 2.91 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
#include <chrono>
#include <thread>
#include <sstream>
#include <iomanip>
#include "config_file_handler.h"
#include "temp_monitor.h"
using namespace std;
string Sensor::get_sensor() { return _sensor; }
vector<string> Sensor::get_high_temp_thresh_cmd(){ return _highTempThreshCmds; }
vector<string> Sensor::get_low_temp_thresh_cmd(){ return _lowTempThreshCmds; }
int Sensor::get_high_temp_thresh(){ return _highTempThresh; }
int Sensor::get_low_temp_thresh(){ return _lowTempThresh; }
void Sensor::set_high_temp_thresh(int threshold){ _highTempThresh = threshold; }
void Sensor::set_low_temp_thresh(int threshold) { _lowTempThresh = threshold; }
int System::get_logging_period(){ return _loggingPeriod; }
vector<shared_ptr<Sensor>> System::get_sensors_config(){ return _sensorsConfig; }
void TempMonitor::execute_cmd(string cmd){
system(cmd.c_str());
}
void TempMonitor::monitor() {
while (true)
{
for(shared_ptr<Sensor> sensor: mySystem->get_sensors_config()){
ifstream sensorFile;
string temp;
try
{
sensorFile.open(sensor->get_sensor(), ios::in);
if (!sensorFile.is_open())
{
throw runtime_error("Could not open the sensor file!");
} else {
getline(sensorFile, temp);
stringstream ss;
ss << fixed << setprecision(2) << stof(temp)/1000;
float currentTemp = stof(ss.str());
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
if (currentTemp > sensor->get_high_temp_thresh())
{
cerr << ctime(&now_time) << "\tHigh temperature alert for sensor: " << sensor->get_sensor() << " is: " << currentTemp << "°C" << endl;
for(string cmd : sensor->get_high_temp_thresh_cmd()){
execute_cmd(cmd);
}
} else if (currentTemp < sensor->get_low_temp_thresh())
{
cerr << ctime(&now_time) << "\tLow temperature alert for sensor: " << sensor->get_sensor() << " is: " << currentTemp << "°C" << endl;
for(string cmd : sensor->get_low_temp_thresh_cmd()){
execute_cmd(cmd);
}
}
cout << ctime(&now_time) << "\tCurrent temperature for sensor: " << sensor->get_sensor() << " is: " << currentTemp << "°C" << endl;
}
sensorFile.close();
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
return;
}
}
this_thread::sleep_for(chrono::seconds(mySystem->get_logging_period()));
}
}