-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensorconfig.cpp
88 lines (83 loc) · 1.81 KB
/
sensorconfig.cpp
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
#include "sensorconfig.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
/*
* Save a config (currently to a file)
*/
bool
SensorConfig::save(void)
{
bool result = true;
try {
ostringstream ofs;
ofs << "sensor." << setw(5) << setfill('0') << setbase(8) << id << ".cfg" << setbase(10) << ends;
ofstream ofile(ofs.str().c_str(), ios::out | ios::trunc);
ofile << "low_point " << low_point << endl;
ofile << "high_point " << high_point << endl;
ofile << "low_time " << low_time << endl;
ofile << "high_time " << high_time << endl;
ofile << "reference " << reference << endl;
ofile << "mode " << mode << endl;
ofile.close();
}
catch (...) {
cerr << "Failed to save config" <<endl;
result = false;
}
return result;
}
void
SensorConfig::set(string item, int value)
{
if (item == "mode") {
mode = value;
}
else if (item == "low_point") {
low_point = value;
}
else if (item == "high_point") {
high_point = value;
}
else if (item == "low_time") {
low_time = value;
}
else if (item == "high_time") {
high_time = value;
}
else if (item == "reference") {
reference = value;
}
}
/*
* Load a config file for current node
*/
bool
SensorConfig::load(void)
{
bool result = false;
string line;
string item;
int value;
ostringstream ofs;
ofs << "sensor." << setw(5) << setfill('0') << setbase(8) << id << ".cfg" << setbase(10) << ends;
try {
ifstream ifile(ofs.str().c_str());
if (ifile.is_open()) {
while (getline(ifile, line)) {
stringstream _l(line);
_l >> item >> value;
set(item, value);
result = true;
}
ifile.close();
}
}
catch (...) {
cerr << "Failed to load config" << endl;
result = false;
}
return result;
}