-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisolatedBox_dataentryqueue.cpp
More file actions
147 lines (108 loc) · 4.55 KB
/
isolatedBox_dataentryqueue.cpp
File metadata and controls
147 lines (108 loc) · 4.55 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
/*****************************************************************//**
* \file isolatedBox_dataentryqueue.cpp
* \brief: Class that simulates a serialization
* of value coming from a temperature sensor
* The thread producer simulates the reading from a
* sensor and put them in a queue.
* The thread consumer takes the value from the queue and
* it keeps the compensation process going
*
* \author F.Morani
* \date May 2023
***********************************************************************/
#include "isolatedBox_dataentryqueue.h"
#include "isolatedBox_printdebug.h"
static bool m_stopProducer;
static bool m_stopConsumer;
ISO_DataEntryQueue::ISO_DataEntryQueue(MonitoringDataQueue * _queue)
{
m_DataQueue = _queue;
if (m_DataQueue != nullptr)
m_DataQueue->clear(); // Clear the list
/// <summary>
/// Iso Box application initiliazed
/// </summary>
/// <param name="_queue"></param>
l_IsoBox.init(25, 50);
}
void ISO_DataEntryQueue::thread_producer()
{
ISO_printDebug::printDebug("ISO_DataEntryQueue::thread_producer");
/// The thread keeps working if the flag is true
while (m_stopProducer == true){
/// Getting timestamp
const auto p1 = std::chrono::system_clock::now();
auto l_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
p1.time_since_epoch()).count();
std::string l_timestamp = std::to_string(l_milliseconds);
std::string l_output = "ISO_DataEntryQueue::thread_producer: TimeStamp - seconds since epoch: "
+ l_timestamp;
ISO_printDebug::printDebug(l_output);
for (int i = 20; i < 60; i++) {
// Insert record in the list
/// Preparing data Monitor
MonitoringTemp l_msgData;
int l_val = i; // rand();
std::string l_valueStn = std::to_string(l_val);
std::string l_type = "Celsius";
insert(l_msgData, l_valueStn, l_type);
std::string l_output;
l_output = "ISO_DataEntryQueue::thread_producer: Add pair: Temp is: " + l_valueStn +
" " + l_type;
ISO_printDebug::printDebug(l_output);
if (m_DataQueue != nullptr)
m_DataQueue->push(l_msgData);
m_counter++;
std::this_thread::sleep_for(std::chrono::seconds(1));
} // end for
} // end while
}
void ISO_DataEntryQueue::thread_consumer()
{
if (m_DataQueue != nullptr){
ISO_printDebug::printDebug("ISO_DataEntryQueue::thread_consumer");
/// The thread keeps working if the flag is true
while (m_stopConsumer == true){
if (m_DataQueue->size() > 0) {
/// Checking the list - Remove and process the first element
MonitoringTemp l_monData = m_DataQueue->pop();
std::string l_output = "ISO_DataEntryQueue::thread_consumer Element retrieved "
+ l_monData.first + " " + l_monData.second;
ISO_printDebug::printDebug(l_output);
std::string l_num = l_monData.first;
temp_t l_temp = ::atof(l_num.c_str());
temp_t l_resComp = l_IsoBox.applyCompensation(l_temp);
if (l_resComp == ISO_DEF_UNDEF_TEMP) {
ISO_printDebug::printDebug("Compensation NOT APPLIED for " + l_num);
}
else {
ISO_printDebug::printDebug("Compensation RESTARTED for " + l_num);
}
const auto p1 = std::chrono::system_clock::now();
l_output = "ISO_DataEntryQueue::thread_consumer: TimeStamp - seconds since epoch: "
+ std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
p1.time_since_epoch()).count());
ISO_printDebug::printDebug(l_output);
std::this_thread::sleep_for(std::chrono::seconds(1));
} // else
} // while
} else {
ISO_printDebug::printDebug("ISO_DataEntryQueue::thread_consumer: could not start ");
}
}
bool ISO_DataEntryQueue::getStopProducerFlag()
{
return m_stopProducer;
}
bool ISO_DataEntryQueue::getStopConsumerFlag()
{
return m_stopConsumer;
}
void ISO_DataEntryQueue::setStopProducerFlag(bool _flag)
{
m_stopProducer = _flag;
}
void ISO_DataEntryQueue::setStopConsumerFlag(bool _flag)
{
m_stopConsumer = _flag;
}