-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainComponent.cpp
136 lines (115 loc) · 4.37 KB
/
MainComponent.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
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
#include <iostream>
#include <chrono>
#include <thread>
#include <random>
#include <ctime>
#include <string>
#include <sstream>
#include <csignal>
#include "Logger.hpp"
#include "MqttConnector.hpp"
#include "PressureSensor.hpp"
#include "HumiditySensor.hpp"
#include "Camera.hpp"
static int exitCode = 0;
static Logger logger("MainComponent");
std::string doubleToString(double decimalNumber)
{
std::ostringstream strs;
strs << decimalNumber;
return strs.str();
}
void signalHandler(int signalNumber)
{
logger << INFO << "System correctly shutdown with exit code = " << signalNumber << ENDL;
exitCode = signalNumber;
exit(exitCode);
}
int main(int argc, char* argv[])
{
high_resolution_clock::time_point startTimer = high_resolution_clock::now();
Logger::clearLogFile();
std::cout << "Program is starting..." << std::endl;
std::unique_ptr<MqttConnector> connector(new MqttConnector());
std::unique_ptr<Camera> camera(new Camera());
std::unique_ptr<PressureSensor> pressureSensor(new PressureSensor());
std::unique_ptr<HumiditySensor> humiditySensor(new HumiditySensor());
std::cout << "making test photo..." << std::endl;
if(camera->makePhoto())
{
std::cout << "[SUCCESS] camera is working correctly" << std::endl;
}
else
std::cout << "[ERROR] you should check connections, camera is not working" << std::endl;
std::string tempString, pressString, humString;
int gettingResult = 0;
std::signal(SIGTERM, signalHandler);
std::signal(SIGINT, signalHandler);
std::cout << "measuring starts" << std::endl;
while (true)
{
if (!Camera::isRecording)
{
gettingResult = pressureSensor->getData();
if (gettingResult < 0)
{
logger << ERROR << "Something go wrong. Trying to get temp from second sensor..." << ENDL;
pressString = "0.0";
tempString = "0.0";
gettingResult = humiditySensor->getData();
if (gettingResult < 0 && pressureSensor->getStatus() == Status::Disable
&& humiditySensor->getStatus() == Status::Disable)
{
exitCode = 1;
logger << ERROR << "Sensors are not working. Please check connections."
<< " Program will shutdown!" << ENDL;
exit(exitCode);
}
else if (gettingResult < 0)
{
goto jumpToLoopNextStep;
}
else
{
tempString = doubleToString(humiditySensor->getTemperature());
humString = doubleToString(humiditySensor->getHumidity());
}
}
else
{
pressString = doubleToString(pressureSensor->getPressure());
tempString = doubleToString(pressureSensor->getTemperature());
gettingResult = humiditySensor->getData();
if (gettingResult < 0)
{
humString = "0.0";
goto jumpToLoopNextStep;
}
else{
humString = doubleToString(humiditySensor->getHumidity());
}
}
jumpToLoopNextStep:
connector->publish("SENSORS/PRESSURE", pressString);
connector->publish("SENSORS/TEMPERATURE", tempString);
connector->publish("SENSORS/HUMIDITY", humString);
}
else{
std::string picture = camera->recording();
if (!picture.empty())
connector->publish("SENSORS/CAMERA_PIC", picture);
}
high_resolution_clock::time_point endTimer = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(endTimer - startTimer);
std::cout << "Duration: " << time_span.count() << " seconds." << std::endl;
if (time_span.count() / 3600 >= 3)
{
// clearing syslog file after 3 hours
Logger::clearLogFile();
logger << INFO << "Previous logs have been cleared because the saving time has been exceeded." << ENDL;
startTimer = high_resolution_clock::now();
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return exitCode;
}