Skip to content

Commit 008a7b9

Browse files
Control: Add timer to update the default values
Co-authored-by: Lea Weiss <[email protected]>
1 parent fdbb809 commit 008a7b9

File tree

10 files changed

+108
-23
lines changed

10 files changed

+108
-23
lines changed

control/include/input.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ class Input {
1919
static void s_buildSensors(std::unordered_map<int, Sensor *> &sensors);
2020
// Function that builds the conditions according to the bson file
2121
static void s_buildConditions();
22+
// Function to set the path to the BSON file - for using specific path for the tests
23+
static void s_setPathBson();
2224

2325
private:
2426
// Function that read the bson file
25-
static bson_t *s_readData();
27+
static bson_t *s_readData(string fileName);
2628
// Member that contains the bson file data
2729
static bson_t *document;
2830
};

control/include/sensor.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include <map>
77
#include <unordered_map>
88
#include <set>
9-
#include <optional>
9+
#include <thread>
10+
#include <chrono>
11+
#include <atomic>
12+
#include <mutex>
1013
#include "operator_types.h"
1114
#include "global_properties.h"
1215

@@ -21,6 +24,12 @@ class Sensor {
2124
PacketParser *parser;
2225
std::map<std::string, Field> fieldsMap;
2326

27+
// Variables for the timer functions
28+
int timeForUpdate;
29+
std::atomic<int> timerCounter; // Flag to indicate the remaining time in seconds
30+
std::mutex mtx;
31+
std::thread timerThread;
32+
2433
// Contains the current values of various fields and a list of basic conditions associated with each field
2534
std::map<std::string, std::pair<FieldValue, std::vector<BasicCondition *>>> fields;
2635

@@ -35,6 +44,13 @@ class Sensor {
3544
private:
3645
template <typename T>
3746
bool applyComparison(T a, T b, OperatorTypes op);
47+
48+
// Function that running until the timeout is reached and activate the update.
49+
void delayedFunction();
50+
// Function that activate or reset the timer
51+
void startOrResetTimer();
52+
// Function that update the fields to be the default values
53+
void updateDefualtValues();
3854
};
3955

4056
#endif // _SENSOR_H_

control/src/input.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,16 @@ void Input::s_buildConditions()
193193
GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Conditions built successfully");
194194
}
195195

196+
// Function to set the path to the BSON file - for using specific path for the tests.
197+
void Input::s_setPathBson()
198+
{
199+
// Load the bson file into document
200+
document = s_readData("../test/conditions.bson");
201+
}
202+
196203
// Function that read the bson file
197-
bson_t *Input::s_readData()
204+
bson_t *Input::s_readData(string fileName)
198205
{
199-
string fileName = "../conditions.bson";
200206
ifstream file(fileName, ios::binary);
201207
if (file.is_open()) {
202208
// Get the file size
@@ -234,4 +240,4 @@ bson_t *Input::s_readData()
234240
}
235241

236242
// Load the bson file into document
237-
bson_t *Input::document = s_readData();
243+
bson_t *Input::document = s_readData("../conditions.bson");

control/src/operator_node.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ void OperatorNode::updateTree()
99
// Update the status of the current node
1010
this->updateStatus();
1111

12-
// TODO: Check what would happen if the root was true and we got to it with true
13-
1412
// Check if the status changed
1513
if (prevStatus != this->status) {
1614
// Go over all the parents and update them

control/src/sensor.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
#include "sensor.h"
22
using namespace std;
33

4+
// Function that running until the timeout is reached and activate the update.
5+
void Sensor::delayedFunction()
6+
{
7+
while (timerCounter > 0) {
8+
this_thread::sleep_for(chrono::milliseconds(1000)); // Sleep for 1 second
9+
timerCounter.fetch_sub(1); // Atomically decrement the timer
10+
cout << "Time left: " << timerCounter.load() << " seconds" << endl;
11+
}
12+
updateDefualtValues();
13+
}
14+
15+
// Function that activate or reset the timer
16+
void Sensor::startOrResetTimer()
17+
{
18+
lock_guard<mutex> lock(mtx);
19+
if (timerCounter == 0) {
20+
// Timer is not active, set the timer to 10 seconds and start it
21+
timerCounter = timeForUpdate;
22+
cout << "Starting new timer..." << endl;
23+
timerThread = thread(&Sensor::delayedFunction, this);
24+
timerThread.detach(); // Detach the thread so it can run independently
25+
}
26+
else {
27+
// Timer is already running, just reset it to 10 seconds
28+
timerCounter = timeForUpdate;
29+
cout << "Timer is active, resetting to 10 seconds..." << endl;
30+
}
31+
}
32+
33+
// Function that update the fields to be the default values
34+
void Sensor::updateDefualtValues()
35+
{
36+
for (auto field : fieldsMap) {
37+
updateTrueRoots(field.second.name, field.second.defaultValue, parser->getFieldType(field.second.type));
38+
}
39+
40+
// TODO: check if the default value have to send a message
41+
GlobalProperties &instanceGP = GlobalProperties::getInstance();
42+
for (int cId : instanceGP.trueConditions)
43+
instanceGP.conditions[cId]->activateActions();
44+
}
45+
446
// C-tor initializes the id member variable.
547
Sensor::Sensor(int id, string name, string jsonFilePath) : id(id), name(name)
648
{
49+
timeForUpdate = 10;
50+
timerCounter = 0;
51+
752
parser = new PacketParser(jsonFilePath);
8-
std::vector<Field> tempFields = parser->getFields();
53+
vector<Field> tempFields = parser->getFields();
954

1055
for (auto field : tempFields) {
1156
if (field.type == "bit_field")
@@ -22,11 +67,12 @@ Sensor::Sensor(int id, string name, string jsonFilePath) : id(id), name(name)
2267

2368
void Sensor::handleMessage(void *msg)
2469
{
70+
startOrResetTimer(); // Starts the timer
71+
2572
parser->setBuffer(msg);
2673

2774
for (auto field : fieldsMap) {
2875
string fieldName = field.first;
29-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Processing field: " + fieldName);
3076

3177
updateTrueRoots(fieldName, parser->getFieldValue(fieldName),
3278
parser->getFieldType(field.second.type));
@@ -36,6 +82,8 @@ void Sensor::handleMessage(void *msg)
3682
//Updates the condition status according to the received field and returns the list of the full conditions whose root is true
3783
void Sensor::updateTrueRoots(string field, FieldValue value, FieldType type)
3884
{
85+
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Processing field: " + field);
86+
3987
// Update the field value in the sensor
4088
this->fields[field].first = value;
4189

control/test/sensors_data/camera.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
{
77
"name": "MessageType",
88
"size": 1,
9-
"type": "unsigned_int"
9+
"type": "unsigned_int",
10+
"defaultValue": 1
1011
},
1112
{
1213
"name": "Level",
1314
"size": 3,
14-
"type": "unsigned_int"
15+
"type": "unsigned_int",
16+
"defaultValue": 1
1517
},
1618
{
1719
"name": "ObjectType",
1820
"size": 4,
19-
"type": "unsigned_int"
21+
"type": "unsigned_int",
22+
"defaultValue": 1
2023
}
2124
],
2225
"name": "AlertDetails",
@@ -26,17 +29,20 @@
2629
{
2730
"name": "ObjectDistance",
2831
"size": 32,
29-
"type": "float_fixed"
32+
"type": "float_fixed",
33+
"defaultValue": 100.0
3034
},
3135
{
3236
"name": "CarSpeed",
3337
"size": 32,
34-
"type": "unsigned_int"
38+
"type": "unsigned_int",
39+
"defaultValue": 100
3540
},
3641
{
3742
"name": "ObjectSpeed",
3843
"size": 32,
39-
"type": "unsigned_int"
44+
"type": "unsigned_int",
45+
"defaultValue": 100
4046
}
4147
]
4248
}

control/test/sensors_data/communication.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
{
55
"size": 32,
66
"type": "signed_int",
7-
"name": "Status"
7+
"name": "Status",
8+
"defaultValue": 100
89
},
910
{
1011
"size": 32,
1112
"type": "signed_int",
12-
"name": "Code"
13+
"name": "Code",
14+
"defaultValue": 100
1315
},
1416
{
1517
"size": 32,
1618
"type": "signed_int",
17-
"name": "Flags"
19+
"name": "Flags",
20+
"defaultValue": 100
1821
}
1922
]
2023
}

control/test/sensors_data/speed.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
{
55
"size": 8,
66
"type": "boolean",
7-
"name": "Status"
7+
"name": "Status",
8+
"defaultValue": true
89
},
910
{
1011
"size": 64,
1112
"type": "char_array",
12-
"name": "Msg"
13+
"name": "Msg",
14+
"defaultValue": "average current speed"
1315
},
1416
{
1517
"size": 32,
1618
"type": "signed_int",
17-
"name": "Speed"
19+
"name": "Speed",
20+
"defaultValue": 100
1821
}
1922
]
2023
}

control/test/sensors_data/tirePressure.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
{
55
"size": 32,
66
"type": "float_fixed",
7-
"name": "Temperature"
7+
"name": "Temperature",
8+
"defaultValue": 100
89
},
910
{
1011
"size": 32,
1112
"type": "float_fixed",
12-
"name": "Pressure"
13+
"name": "Pressure",
14+
"defaultValue": 80
1315
}
1416
]
1517
}

control/test/test_simple_build.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ TEST(OrAndCondition, ComplexConditionTest)
118118

119119
int main(int argc, char **argv)
120120
{
121+
Input::s_setPathBson();
121122
::testing::InitGoogleTest(&argc, argv);
122123
return RUN_ALL_TESTS();
123124
}

0 commit comments

Comments
 (0)