Skip to content

Commit 59bf0bb

Browse files
committed
nodes correctly updated for PIR, sound, air quality and contact sensors
1 parent eca2854 commit 59bf0bb

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/mqtt_agent.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ int MqttAgent::sensor_data_to_dsr(json data)
203203
return 0;
204204
}
205205

206-
string sensor_type_ = data["sensorType"];
206+
string sensor_name_ = data["sensorName"];
207207
string parent_node_name_ = data["parentNode"];
208208

209209
// get or create timestamp
@@ -221,32 +221,33 @@ int MqttAgent::sensor_data_to_dsr(json data)
221221
// Check if parent node exists (if not, we cannot put the sensor in the world!)
222222
auto parent_node_ = G_->get_node(parent_node_name_);
223223
if (!parent_node_.has_value()) {
224-
std::cout << "ERROR: Could not find parent node [" << parent_node_name_ << "] for sensor [" << sensor_type_ << "]. "
224+
std::cout << "ERROR: Could not find parent node [" << parent_node_name_ << "] for sensor [" << sensor_name_ << "]. "
225225
<< "Sensor node not included in the DSR" << std::endl;
226226
return 0;
227227
}
228228

229229
// Then check if sensor node has been created and create it if necessary
230-
auto sensor_node = G_->get_node(sensor_type_);
230+
auto sensor_node = G_->get_node(sensor_name_);
231+
// if there's no node of that type, create node
231232
if (!sensor_node.has_value()) {
232-
sensor_node.emplace(DSR::Node::create<sensor_node_type>(sensor_type_));
233+
sensor_node.emplace(DSR::Node::create<sensor_node_type>(sensor_name_));
233234
if (auto id = G_->insert_node(sensor_node.value()); id.has_value()) {
234-
std::cout << "Inserted sensor node [" << sensor_type_ << "] in the graph."
235+
std::cout << "Inserted sensor node [" << sensor_name_ << "] in the graph."
235236
<< std::endl;
236237
// Set "IN" edge between room and sensor
237238
auto edge = DSR::Edge::create<in_edge_type>(sensor_node.value().id(), parent_node_.value().id());
238239
if (G_->insert_or_assign_edge(edge)) {
239-
std::cout << "Inserted edge between [" << sensor_type_ << "] and ["
240+
std::cout << "Inserted edge between [" << sensor_name_ << "] and ["
240241
<< parent_node_name_ << "]" << std::endl;
241242
}
242243
}
243244
}
244-
245+
245246
// insert type_of_sensor attribute
246-
G_->add_or_modify_attrib_local<type_of_sensor_att>(sensor_node.value(), (std::string)(data["sensorName"]));
247+
G_->add_or_modify_attrib_local<type_of_sensor_att>(sensor_node.value(), (std::string)(data["sensorType"]));
247248

248249
// Check type of msg and update the sensor node with the new data
249-
if (data["sensorName"] == "ZPHS01B"){
250+
if (data["sensorType"] == "calidad_aire"){
250251
// Add location (we use a 'room' attribute for this)
251252
G_->add_or_modify_attrib_local<room_att>(sensor_node.value(), (std::string)(data["sensorLocation"]));
252253
// Parse air quality values, update the sensor node and insert it
@@ -265,9 +266,9 @@ int MqttAgent::sensor_data_to_dsr(json data)
265266
G_->add_or_modify_attrib_local<toinflux_att>(sensor_node.value(), (bool)(data["toInfluxDB"]));
266267
G_->add_or_modify_attrib_local<measure_timestamp_att>(sensor_node.value(), (uint64_t)(timestamp_));
267268
G_->update_node(sensor_node.value());
268-
std::cout << "Sensor node [" << sensor_type_ << "] has been updated." << std::endl;
269+
std::cout << "Sensor node [" << sensor_name_ << "] has been updated." << std::endl;
269270
}
270-
else if (data["sensorName"] == "datoRadarRespiracion") {
271+
else if (data["sensorType"] == "datoRadarRespiracion") {
271272
// First check data is valid
272273
if (data["heartrate"] <= 30 || data["breathrate"] <= 10) {
273274
return 0;
@@ -280,7 +281,7 @@ int MqttAgent::sensor_data_to_dsr(json data)
280281
auto edge_measure = DSR::Edge::create<measuring_edge_type>(
281282
sensor_node.value().id(), person_node_.value().id());
282283
if (G_->insert_or_assign_edge(edge_measure)) {
283-
std::cout << "Inserted edge between [" << sensor_type_ << "] and ["
284+
std::cout << "Inserted edge between [" << sensor_name_ << "] and ["
284285
<< person_node_.value().name() << "]" << std::endl;
285286
}
286287
}
@@ -292,9 +293,9 @@ int MqttAgent::sensor_data_to_dsr(json data)
292293
G_->add_or_modify_attrib_local<toinflux_att>(sensor_node.value(), (bool)(data["toInfluxDB"]));
293294
G_->add_or_modify_attrib_local<measure_timestamp_att>(sensor_node.value(), (uint64_t)(timestamp_));
294295
G_->update_node(sensor_node.value());
295-
std::cout << "Sensor node [" << sensor_type_ << "] has been updated." << std::endl;
296+
std::cout << "Sensor node [" << sensor_name_ << "] has been updated." << std::endl;
296297
}
297-
else if (data["sensorName"] == "PIR sensor Adafruit ID 189"){
298+
else if (data["sensorType"] == "PIR"){
298299
// Add location (we use a 'room' attribute for this)
299300
G_->add_or_modify_attrib_local<room_att>(sensor_node.value(), (std::string)(data["sensorLocation"]));
300301
// Parse presence value, update the sensor node and insert it
@@ -303,9 +304,20 @@ int MqttAgent::sensor_data_to_dsr(json data)
303304
G_->add_or_modify_attrib_local<toinflux_att>(sensor_node.value(), (bool)(data["toInfluxDB"]));
304305
G_->add_or_modify_attrib_local<measure_timestamp_att>(sensor_node.value(), (uint64_t)(timestamp_));
305306
G_->update_node(sensor_node.value());
306-
std::cout << "Sensor node [" << sensor_type_ << "] has been updated." << std::endl;
307+
std::cout << "Sensor node [" << sensor_name_ << "] has been updated." << std::endl;
307308
}
308-
else if (data["sensorName"] == "Grove Sound Sensor"){
309+
else if (data["sensorType"] == "Contact"){
310+
// Add location (we use a 'room' attribute for this)
311+
G_->add_or_modify_attrib_local<room_att>(sensor_node.value(), (std::string)(data["sensorLocation"]));
312+
// Parse open value, update the sensor node and insert it
313+
G_->add_or_modify_attrib_local<open_att>(sensor_node.value(), (bool)(data["open"]));
314+
if (data.contains("toInfluxDB"))
315+
G_->add_or_modify_attrib_local<toinflux_att>(sensor_node.value(), (bool)(data["toInfluxDB"]));
316+
G_->add_or_modify_attrib_local<measure_timestamp_att>(sensor_node.value(), (uint64_t)(timestamp_));
317+
G_->update_node(sensor_node.value());
318+
std::cout << "Sensor node [" << sensor_name_ << "] has been updated." << std::endl;
319+
}
320+
else if (data["sensorType"] == "Sound"){
309321
// Add location (we use a 'room' attribute for this)
310322
G_->add_or_modify_attrib_local<room_att>(sensor_node.value(), (std::string)(data["sensorLocation"]));
311323
// Parse volume value, update the sensor node and insert it
@@ -314,10 +326,10 @@ int MqttAgent::sensor_data_to_dsr(json data)
314326
G_->add_or_modify_attrib_local<toinflux_att>(sensor_node.value(), (bool)(data["toInfluxDB"]));
315327
G_->add_or_modify_attrib_local<measure_timestamp_att>(sensor_node.value(), (uint64_t)(timestamp_));
316328
G_->update_node(sensor_node.value());
317-
std::cout << "Sensor node [" << sensor_type_ << "] has been updated." << std::endl;
329+
std::cout << "Sensor node [" << sensor_name_ << "] has been updated." << std::endl;
318330
}
319331
else {
320-
std::cout << "sensor_data_to_dsr ERROR: Sensor " << data["sensorName"] << " not supported by mqtt_agent (yet)" << std::endl;
332+
std::cout << "sensor_data_to_dsr ERROR: Sensor " << data["sensorType"] << " not supported by mqtt_agent (yet)" << std::endl;
321333
return 0;
322334
}
323335

0 commit comments

Comments
 (0)