-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramWheel.cpp
More file actions
148 lines (130 loc) · 4.41 KB
/
Copy pathprogramWheel.cpp
File metadata and controls
148 lines (130 loc) · 4.41 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
#include "programWheel.h"
#include "utils.h"
bool programWheel::startProgramWheel(void) {
// first check if there is a file in /etc/incu8ator/step.cache.
// if yes read in the step number and the elapsed time (this
// can be used to recover the status in case of power supply issue).
std::string res;
if(loadStatus(stepID, el->elapsedSeconds, res)) {
log::inf("Found state.xml");
}
else log::err(res);
// This cannot be used here because of a g++ 4.7.2 bug?
//std::thread t1(el);
// sometimes compiling it with the class object as a functor
// g++ returns with the error of
// /usr/include/c++/4.7/type_traits:1834:9 error: 'std::declval<elapsedTimeThread*>() cannot be used a function'
// so use the start member function as a thread parameter instead...
// http://stackoverflow.com/questions/29966780/overload-resolution-issue-in-generic-vector-class (check the accepted answer)
std::thread t1(&elapsedTimeThread::start, el);
log::inf("elapsedTimeThread detaching now...");
t1.detach();
return true;
}
/**
* Check if the progwheel.xml is available at the path /etc/incu8ator/
* and check if there is a stamp.cache file or not.
* @return false if the XML config file does not exists or true otherwise.
*/
bool programWheel::initProgramWheel(std::string& res) {
result = doc.load_file(progWheelXMLPath);
if(!result) {
res = "progwheel.xml error: ";
res.append(result.description());
return false;
}
if(!processConfig(res)) {
return false;
}
el = new elapsedTimeThread();
return true;
}
bool programWheel::saveStatus(int stepID, double elapsedSeconds) {
pugi::xml_document status;
std::stringstream ss;
ss << "<state><stepID>" << stepID << "</stepID><elapsed>" << elapsedSeconds << "</elapsed></state>";
doc.load_string(ss.str().c_str());
return doc.save_file(statusXMLPath);
}
bool programWheel::loadStatus(int& stepID, double& elapsedSeconds, std::string& res) {
pugi::xml_document status;
pugi::xml_parse_result status_res;
status_res = doc.load_file(statusXMLPath);
if(!status_res) {
res = "state.xml error: ";
res.append(status_res.description());
stepID = 0;
elapsedSeconds = 0.0;
return false;
}
stepID = atoi(doc.child(STATE_NODE_NAME).child(STEPID_NODE_NAME).text().get());
elapsedSeconds = atof(doc.child(STATE_NODE_NAME).child(ELAPSED_NODE_NAME).text().get());
return true;
}
bool programWheel::processConfig(std::string& res) {
if(result) {
double hour, temp, hum;
int i = 1;
std::stringstream ss;
pugi::xml_node next_step;
root = doc.child(ROOT_NODE_NAME);
steps.clear();
next_step = root.child((std::string(STEP_NODE_NAME).append(NumberToString(i)).c_str()));
for (; (i<MAX_STEPS) && (next_step); i++) {
hour = atof(next_step.child(HOUR_NODE_NAME).text().get());
temp = atof(next_step.child(TEMP_NODE_NAME).text().get());
hum = atof(next_step.child(HUM_NODE_NAME).text().get());
Step nextStep = Step(hour, temp, hum, i-1);
steps.push_back(nextStep);
ss.clear();
ss.str(std::string());
ss << "New step was added. id:" << nextStep.id << ", hour:" << nextStep.interval;
log::inf(std::string(ss.str()));
next_step = root.child((std::string(STEP_NODE_NAME).append(NumberToString(i+1)).c_str()));
}
}
else {
res = result.description();
return false;
}
return true;
}
double programWheel::getWantedTemperature(void) {
try {
double wanted = 0.0;
double s = el->getElapsedSeconds();
double h = s/3600;
std::stringstream ss;
ss << "getWantedTemperature::Elapsed time [sec/hours]: " << s << "/" << h << ". Current stepID/interval: " << stepID << "/" << steps.at(stepID).interval;
log::inf(ss.str());
if(h >= steps.at(stepID).interval) {
ss.clear();
ss.str(std::string());
ss << "StepID " << stepID << " has ended after " << s << " seconds.";
log::inf(ss.str());
s = el->elapsedSeconds = 0.0;
stepID+=1;
}
wanted = steps.at(stepID).temp;
// save the current status - stepID and elapsed time in seconds
if(!saveStatus(stepID, s)) {
log::wrn(std::string("Unable to save the status file to ").append(statusXMLPath));
}
return wanted;
}
catch(const std::out_of_range& e) {
el->stop();
// remove the state.xml because the program has done
unlink(statusXMLPath);
return 0.0;
}
}
double programWheel::getWantedHumidity(void) {
return 60.0;
}
double programWheel::getElapsedHours(void) {
return (el?el->getElapsedSeconds()/3600:0.0);
}
double programWheel::getInterval(void) {
return steps.at(stepID).interval;
}