Commit c3da3320 authored by Micha Müller's avatar Micha Müller
Browse files

Improvements and fixes for SensorGroupTemplate

parent 9965c8a0
......@@ -35,6 +35,7 @@
#include <vector>
#include <memory>
//TODO cross reference partial template specialization in doxygen docs
/**
* @brief Interface template for sensor group implementations with entities.
*
......@@ -54,7 +55,6 @@ class SensorGroupTemplate : public SensorGroupInterface {
protected:
using S_Ptr = std::shared_ptr<S>;
using E_Ptr = std::shared_ptr<E>;
public:
SensorGroupTemplate(const std::string groupName) :
......@@ -73,6 +73,10 @@ public:
}
virtual ~SensorGroupTemplate() {
if(_keepRunning) {
stop();
}
_sensors.clear();
_baseSensors.clear();
}
......@@ -93,6 +97,9 @@ public:
return *this;
}
void setEntity(E* entity) { _entity = entity; }
E* const getEntity() const { return _entity; }
/**
* @brief Initialize the sensor group.
*
......@@ -132,7 +139,10 @@ public:
return;
}
this->execOnStart();
if (!this->execOnStart()) {
LOG(error) << "Sensorgroup " << _groupName << ": Startup failed.";
return;
}
if (_entity) {
_keepRunning = true;
......@@ -151,9 +161,15 @@ public:
* actions are required during shutdown.
*/
virtual void stop() final override {
if (!_keepRunning) {
LOG(info) << "Sensorgroup " << _groupName << " already stopped.";
return;
}
_keepRunning = false;
//cancel any outstanding readAsync()
_timer->cancel();
wait();
this->execOnStop();
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
......@@ -186,6 +202,7 @@ public:
virtual std::vector<SBasePtr>& getSensors() final override { return _baseSensors; }
//TODO only call this printMethod!
//TODO refactor: use printGroupConfig in derived classes
/**
* @brief Print SensorGroup configuration.
*
......@@ -203,6 +220,12 @@ public:
//print plugin specific group attributes
this->printConfig(ll);
if (_entity) {
LOG_VAR(ll) << " Entity " << _entity->getEntityName();
} else {
LOG_VAR(ll) << " No entity set!";
}
//print associated sensors
LOG_VAR(ll) << " Sensors:";
for(auto s : _sensors) {
......@@ -229,6 +252,7 @@ protected:
_pendingTasks--;
}
//TODO move common logic to interface
///@name Can be overwritten
///@{
/**
......@@ -239,7 +263,7 @@ protected:
* %initGroup() is appropriately called by this template during
* init().
*/
virtual void execOnInit() { /* do nothing if not overwritten */ };
virtual void execOnInit() { /* do nothing if not overwritten */ }
/**
* @brief Implement plugin specific actions to start a group here.
......@@ -248,8 +272,10 @@ protected:
* actions to start polling data (e.g. open a file descriptor),
* this should be implemented here. %startGroup() is appropriately
* called by this template during start().
*
* @return True on success, false otherwise.
*/
virtual void execOnStart() { /* do nothing if not overwritten */ };
virtual bool execOnStart() { return true; }
/**
* @brief Implement plugin specific actions to stop a group here.
......@@ -259,15 +285,15 @@ protected:
* this should be implemented here. %stopGroup() is appropriately
* called by this template during stop().
*/
virtual void execOnStop() { /* do nothing if not overwritten */ };
virtual void execOnStop() { /* do nothing if not overwritten */ }
///@}
std::vector<S_Ptr> _sensors; ///< Sensors associated with this group
std::vector<SBasePtr> _baseSensors; ///< Maintain vector with SensorBase pointers for fast getSensors() implementation
E_Ptr _entity; ///< Entity this group is associated to
E* _entity; ///< Entity this group is associated to
};
//TODO cross reference general template in doxygen docs
/**
* @brief Interface partial template specialization for sensor group
* implementations without entities.
......@@ -303,6 +329,10 @@ public:
}
virtual ~SensorGroupTemplate() {
if(_keepRunning) {
stop();
}
_sensors.clear();
_baseSensors.clear();
}
......@@ -353,7 +383,10 @@ public:
return;
}
this->execOnStart();
if (!this->execOnStart()) {
LOG(error) << "Sensorgroup " << _groupName << ": Startup failed.";
return;
}
_keepRunning = true;
_pendingTasks++;
......@@ -368,9 +401,15 @@ public:
* actions are required during shutdown.
*/
virtual void stop() final override {
if (!_keepRunning) {
LOG(info) << "Sensorgroup " << _groupName << " already stopped.";
return;
}
_keepRunning = false;
//cancel any outstanding readAsync()
_timer->cancel();
wait();
this->execOnStop();
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
......@@ -456,7 +495,7 @@ protected:
* %initGroup() is appropriately called by this template during
* init().
*/
virtual void execOnInit() { /* do nothing if not overwritten */ };
virtual void execOnInit() { /* do nothing if not overwritten */ }
/**
* @brief Implement plugin specific actions to start a group here.
......@@ -465,8 +504,10 @@ protected:
* actions to start polling data (e.g. open a file descriptor),
* this should be implemented here. %startGroup() is appropriately
* called by this template during start().
*
* @return True on success, false otherwise.
*/
virtual void execOnStart() { /* do nothing if not overwritten */ };
virtual bool execOnStart() { return true; }
/**
* @brief Implement plugin specific actions to stop a group here.
......@@ -476,7 +517,7 @@ protected:
* this should be implemented here. %stopGroup() is appropriately
* called by this template during stop().
*/
virtual void execOnStop() { /* do nothing if not overwritten */ };
virtual void execOnStop() { /* do nothing if not overwritten */ }
///@}
std::vector<S_Ptr> _sensors; ///< Sensors associated with this group
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment