forked from BeeeOn/gateway-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualSensorValue.cpp
132 lines (117 loc) · 3.24 KB
/
VirtualSensorValue.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
/**
* @file VirtualSensorValue.cpp
* @Author BeeeOn team
* @date
* @brief
*/
#include <memory>
#include <unistd.h>
#include "Aggregator.h"
#include "VirtualSensorValue.h"
using namespace std;
using namespace Poco;
using Poco::AutoPtr;
using Poco::Logger;
using Poco::Timer;
using namespace Poco::Util;
VirtualSensorValue::VirtualSensorValue() :
is_actuator(false),
from(0),
to(0),
step(0),
change_time(0),
switch_time(0),
generator_type(""),
actual_value(0),
module_id(0),
timer(NULL),
log(Poco::Logger::get("Adaapp-VSv"))
{
log.setLevel("trace"); // set default lowest level
value_change_mutex.reset(new Poco::FastMutex);
}
void VirtualSensorValue::print() {
log.information("actuator :" + std::string(is_actuator ? "TRUE" : "FALSE"));
log.information("g. type :" + generator_type);
log.information("module_id:" + toStringFromInt(module_id));
log.information("from :" + toStringFromFloat(from));
log.information("to :" + toStringFromFloat(to));
log.information("step :" + toStringFromFloat(step));
log.information("actual :" + toStringFromFloat(actual_value));
}
void VirtualSensorValue::setNewVal(float val) {
if(!is_actuator)
return;
value_change_mutex->lock(); // anyone can change value untill process is complete
log.information("VirtualSensorValue request: set value to "+std::to_string(val));
log.information(" ---> wait for switch time : "+std::to_string(switch_time)+" s");
for (int i = 0; i < switch_time; i++) { // sleep for switch time, then set new value
if (quit_global_flag)
break;
sleep(1);
}
if (!quit_global_flag)
{
actual_value = val;
log.information("VirtualSensorValue request done: new value is "+std::to_string(val));
}
value_change_mutex->unlock();
}
void VirtualSensorValue::nextVal(Poco::Timer&) {
value_change_mutex->lock();
float next_val = actual_value;
float f = from;
float t = to;
if (f > t)
swap(f,t);
if (generator_type == "linear") {
next_val += step;
if (next_val > t) {
next_val = t;
this->timer->stop(); // Final value hasbeen reached, no need to generate more events
return;
}
else if (next_val < f) {
next_val = f;
this->timer->stop();
return;
}
}
if (generator_type == "triangle") {
next_val += step;
if (next_val > t) {
next_val = t;
step *= -1; // Reverse to create the saw
}
else if (next_val < f) {
next_val = f;
step *= -1;
}
}
else if (generator_type == "random") {
next_val = (rand() % (int) (t-f)) + f ;
}
for (int i = 0; i < switch_time; i++) { // sleep for switch time, then set new value
if (quit_global_flag)
break;
sleep(1);
}
if (quit_global_flag)
return;
actual_value = next_val;
value_change_mutex->unlock();
}
void VirtualSensorValue::startTimer() {
timer = new Poco::Timer(change_time*1000, change_time*1000); // *1000 because of conversion to ms
timer->start(TimerCallback<VirtualSensorValue>(*this, &VirtualSensorValue::nextVal));
}
void VirtualSensorValue::setLoggingInfo(AutoPtr<IniFileConfiguration> cfg) {
setLoggingLevel(log, cfg); /* Set logging level from configuration file*/
setLoggingChannel(log, cfg); /* Set where to log (console, file, etc.)*/
}
VirtualSensorValue::~VirtualSensorValue() {
if (timer){
timer->stop();
delete timer;
}
}