11#include " sensor.h"
22using 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.
547Sensor::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
2368void 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
3783void 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
0 commit comments