-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_monitor_cli.cpp
More file actions
138 lines (131 loc) · 4.72 KB
/
temp_monitor_cli.cpp
File metadata and controls
138 lines (131 loc) · 4.72 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
#include <iostream>
#include <string>
#include "ascii_art.h"
#include "config_file_handler.h"
#include "systemd_misc.hpp"
using namespace std;
void showUsage(string programName){
cerr << "Usage: " << programName << " [OPTION] [VALUE]...\n" <<
"CLI tool to monitor the temperatures in the platform\n" <<
"Options:\n" <<
"\t-h, --help\t\t\t\tDisplays usage information\n" <<
"\t-w, --display-welcome\t\t\tDisplays a cool welcome logo :)\n" <<
"\t-d, --display-config\t\t\tShows current config. Use this command to get the Sensor IDs and Command Ids\n" <<
"\t-s, --start-monitor\t\t\tStart monitoring the temperatures\n" <<
"\t-o, --stop-monitor\t\t\tStop monitoring the temperatures\n" <<
"\t-r, --restart-monitor\t\t\tRestart the monitoring service of temperatures\n" <<
"\t-e, --enable-monitor\t\t\tEnable the autostart of the monitoring service\n" <<
"\t--edit-temp-limit-high\t\t\tEdit what is considered a high temperature value for a sensor [SENSOR_ID] [TEMP]\n" <<
"\t--edit-temp-limit-low\t\t\tEdit what is considered a low temperature value [SENSOR_ID] [TEMP]\n" <<
"\t--edit-logging-period\t\t\tEdit the logging period for the sensors [PERIOD]\n" <<
"\t--add-script-on-high-temp\t\tAdd a script to run when the temperature is exceeded [SENSOR_ID] [COMMAND]\n" <<
"\t--remove-script-on-high-temp\t\tRemove a script from running when the temperature is exceeded [SENSOR_ID] [CMD_ID]\n" <<
"\t--add-script-on-low-temp\t\tAdd a script to run when the temperature is below the threshold [SENSOR_ID] [COMMAND]\n" <<
"\t--remove-script-on-low-temp\t\tRemove a script from running when the temperature is below the threshold [SENSOR_ID] [CMD_ID]" << endl;
}
int main (int argc, char* argv[]) {
if (argc > 4 || argc == 1)
{
showUsage(argv[0]);
return 1;
}
ConfigFileHandler cfh;
string arg = argv[1];
if (arg == "-h" || arg == "--help")
{
showUsage(argv[0]);
} else if (arg == "-w" || arg == "--display-welcome")
{
cout << rimacWelcome << endl;
cout << "\tFrom CR with love <3" << endl << endl;
} else if (arg == "-d" || arg == "--display-config")
{
shared_ptr<System> mySystem = cfh.parse_config_file();
cfh.pretty_print_config(mySystem);
SYSTEMD_MISC::status_service();
} else if (arg == "-s" || arg == "--start-monitor")
{
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::start_service();
} else if (arg == "-o" || arg == "--stop-monitor")
{
SYSTEMD_MISC::stop_service();
} else if (arg == "-r" || arg == "--restart-monitor")
{
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
} else if (arg == "-e" || arg == "--enable-monitor")
{
SYSTEMD_MISC::enable_service();
} else if (arg == "--edit-temp-limit-high")
{
if (argc != 4)
{
showUsage(argv[0]);
return 1;
}
int sensorId = stoi(argv[2]);
int newThreshold = stoi(argv[3]);
cfh.edit_threshold_high(sensorId, newThreshold);
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
} else if (arg == "--edit-temp-limit-low")
{
if (argc != 4)
{
showUsage(argv[0]);
return 1;
}
int sensorId = stoi(argv[2]);
int newThreshold = stoi(argv[3]);
cfh.edit_threshold_low(sensorId, newThreshold);
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
} else if (arg == "--edit-logging-period")
{
if (argc != 3)
{
showUsage(argv[0]);
return 1;
}
int newPeriod = stoi(argv[2]);
cfh.edit_logging_period(newPeriod);
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
}else if (arg == "--add-script-on-high-temp")
{
if (argc != 4)
{
showUsage(argv[0]);
return 1;
}
int sensorId = stoi(argv[2]);
string cmd = argv[3];
cfh.add_script_high_temp(sensorId, cmd);
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
} else if (arg == "--remove-script-on-high-temp")
{
/* code */
} else if (arg == "--add-script-on-low-temp")
{
if (argc != 4)
{
showUsage(argv[0]);
return 1;
}
int sensorId = stoi(argv[2]);
string cmd = argv[3];
cfh.add_script_low_temp(sensorId, cmd);
SYSTEMD_MISC::daemon_reload();
SYSTEMD_MISC::restart_service();
} else if (arg == "--remove-script-on-low-temp")
{
/* code */
} else
{
showUsage(argv[0]);
return 1;
}
return 0;
}