-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVirtualSensorModule.cpp
153 lines (132 loc) · 3.75 KB
/
VirtualSensorModule.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @file VirtualSensorModule.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include "VirtualSensorModule.h"
using namespace std;
using Poco::AutoPtr;
using Poco::Logger;
using Poco::Runnable;
using Poco::Thread;
using Poco::Util::IniFileConfiguration;
VirtualSensorModule::VirtualSensorModule(IOTMessage _msg, shared_ptr<Aggregator> _agg) :
msg(_msg),
agg(_agg),
log(Poco::Logger::get("Adaapp-VS"))
{
loadSensors(_agg);
int sum = 0;
for (auto s:sensors) {
sum += s.vsc.size();
}
// fixed bug with drained thread pool with more than 16 threads.
if (sum > 16)
Poco::ThreadPool::defaultPool().addCapacity(sum-16+1); // +1 just in case
// Number of unpaired sensors is total amount of sensors (unless there were some sensors paired in previous run)
unpaired_sensors = sensors.size();
}
void VirtualSensorModule::run() {
if (!sensors.size())
return;
// Load number of paired VS from last run
unsigned int paired_sensors = 0;
Poco::File x(vs_paired_path);
if (x.exists()) {
ifstream in;
std::string tmp;
in.open(vs_paired_path);
if (in.good()) {
std::getline(in, tmp);
paired_sensors = min(toNumFromString(tmp), (long long int)sensors.size());
log.information("In last relation was paired " + std::to_string(paired_sensors) + " VS! Activating them. Notice that if there was more VS than in this run, num of VS is decreased.");
}
try{
x.remove();
}
catch (Poco::Exception& ex){
log.error("Error at removing pre-paired sensor count file");
}
}
for (unsigned int i = 0; i < paired_sensors; i++)
pairNewVS();
while(!quit_global_flag)
sleep(1);
}
bool VirtualSensorModule::unpairedSensorsLeft() {
return (unpaired_sensors);
}
void VirtualSensorModule::fromServerCmd(Command cmd) {
if (cmd.state == "update") {
for (VirtualSensor& vs : sensors) {
if (vs.sensor.euid == cmd.euid) {
vs.setWakeUpTime(cmd.time);
}
}
}
else if (cmd.state == "listen") {
pairNewVS();
}
else if (cmd.state == "set") {
for (VirtualSensor& vs : sensors) {
if (vs.sensor.euid == cmd.euid) {
; // It shouldn't happen in case of VS, maybe just for virtual actuator
vs.parseCmdFromServer(cmd);
}
}
}
}
void VirtualSensorModule::pairNewVS() {
if (unpaired_sensors) {
Poco::Thread* t = new Poco::Thread("New VS start");
log.information("Waking up new sensor in time " + toStringFromLongInt(time(0)));
t->start(sensors.at(sensors.size() - (unpaired_sensors--)));
sensors_threads.push_back(t);
// Save number of paired VS for next run
ofstream out;
out.open(vs_paired_path);
if (out.good())
out << sensors_threads.size();
out.close();
sleep(1);
}
else {
log.information("No sensor left for wake up, sleeping for 2 sec.");
sleep(2); // Simulation of expired timeout
}
}
bool VirtualSensorModule::isVirtualDevice(euid_t sensor_id) {
for (VirtualSensor vs : sensors) {
if (vs.sensor.euid == sensor_id)
return true;
}
return false;
}
void VirtualSensorModule::loadSensors(shared_ptr<Aggregator> _agg) {
AutoPtr<IniFileConfiguration> cfg;
try {
cfg = new IniFileConfiguration(string(MODULES_DIR)+string(MOD_VIRTUAL_SENSOR)+".ini");
}
catch (Poco::Exception& ex) {
log.error("Exception with config file reading:\n" + ex.displayText());
exit (EXIT_FAILURE);
}
vs_paired_path = cfg->getString("virtual_sensor.pairing_file", "/var/lib/beeeon/vs_paired.cnt");
unsigned int i = 1;
while (!quit_global_flag) {
try {
sensors.push_back(VirtualSensor(cfg, msg, i, _agg));
} catch (Poco::Exception& ex) {
break;
}
i++;
}
log.information("VSM loaded " + toStringFromInt(sensors.size()) + " sensors, loading complete.");
}
VirtualSensorModule::~VirtualSensorModule() {
for (unsigned int i = 0; i < sensors_threads.size(); i++) {
sensors_threads.at(i)->join();
delete sensors_threads.at(i);
}
}