Commit b5278b79 authored by Michael Ott's avatar Michael Ott
Browse files

Add configuration parameter for entities and sensor groups to disable them

parent 22b41f9c
...@@ -435,6 +435,12 @@ protected: ...@@ -435,6 +435,12 @@ protected:
} else { } else {
sGroup.setSync(true); sGroup.setSync(true);
} }
} else if (boost::iequals(val.first, "disabled")) {
if (boost::iequals(val.second.data(), "true")) {
sGroup.setDisabled(true);
} else {
sGroup.setDisabled(false);
}
} else if (boost::iequals(val.first, _baseName)) { } else if (boost::iequals(val.first, _baseName)) {
if (!isTemplate) { if (!isTemplate) {
LOG(debug) << " " << _baseName << " " << val.second.data(); LOG(debug) << " " << _baseName << " " << val.second.data();
...@@ -491,11 +497,17 @@ protected: ...@@ -491,11 +497,17 @@ protected:
} }
//read in values inherited from EntityInterface //read in values inherited from EntityInterface
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) { BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "mqttPart")) { if (boost::iequals(val.first, "mqttPart")) {
sEntity.setMqttPart(val.second.data()); sEntity.setMqttPart(val.second.data());
} } else if (boost::iequals(val.first, "disabled")) {
} if (boost::iequals(val.second.data(), "true")) {
sEntity.setDisabled(true);
} else {
sEntity.setDisabled(false);
}
}
}
sensorEntity(sEntity, config); sensorEntity(sEntity, config);
......
...@@ -57,6 +57,7 @@ public: ...@@ -57,6 +57,7 @@ public:
_name(name), _name(name),
_mqttPart(""), _mqttPart(""),
_initialized(false), _initialized(false),
_disabled(false),
_strand(nullptr) {} _strand(nullptr) {}
/** /**
...@@ -70,6 +71,7 @@ public: ...@@ -70,6 +71,7 @@ public:
_name(other._name), _name(other._name),
_mqttPart(other._mqttPart), _mqttPart(other._mqttPart),
_initialized(false), _initialized(false),
_disabled(other._disabled),
_strand(nullptr) {} _strand(nullptr) {}
/** /**
...@@ -90,6 +92,7 @@ public: ...@@ -90,6 +92,7 @@ public:
_name = other._name; _name = other._name;
_mqttPart = other._mqttPart; _mqttPart = other._mqttPart;
_initialized = false; _initialized = false;
_disabled = other._disabled;
_strand = nullptr; _strand = nullptr;
return *this; return *this;
...@@ -99,6 +102,7 @@ public: ...@@ -99,6 +102,7 @@ public:
///@{ ///@{
const std::string& getName() const { return _name; } const std::string& getName() const { return _name; }
const std::string& getMqttPart() const { return _mqttPart; } const std::string& getMqttPart() const { return _mqttPart; }
const bool& isDisabled() const { return _disabled; }
const std::unique_ptr<strand>& getStrand() const { return _strand; } const std::unique_ptr<strand>& getStrand() const { return _strand; }
///@} ///@}
...@@ -115,6 +119,7 @@ public: ...@@ -115,6 +119,7 @@ public:
_mqttPart.erase(_mqttPart.size()-1); _mqttPart.erase(_mqttPart.size()-1);
} }
} }
void setDisabled(const bool disabled) { _disabled = disabled; }
///@} ///@}
/** /**
...@@ -145,6 +150,7 @@ public: ...@@ -145,6 +150,7 @@ public:
*/ */
void printConfig(LOG_LEVEL ll) { void printConfig(LOG_LEVEL ll) {
LOG_VAR(ll) << " " << "Entity " << _name; LOG_VAR(ll) << " " << "Entity " << _name;
LOG_VAR(ll) << eInd << "Disabled: " << (_disabled ? std::string("true") : std::string("false"));
if (_mqttPart != "") { if (_mqttPart != "") {
LOG_VAR(ll) << eInd << "MQTT part: " << _mqttPart; LOG_VAR(ll) << eInd << "MQTT part: " << _mqttPart;
} }
...@@ -173,6 +179,7 @@ protected: ...@@ -173,6 +179,7 @@ protected:
std::string _mqttPart; ///< Partial MQTT topic identifying this entity std::string _mqttPart; ///< Partial MQTT topic identifying this entity
bool _initialized; ///< An entity should only be initialized once bool _initialized; ///< An entity should only be initialized once
bool _disabled;
std::unique_ptr<strand> _strand; ///< Provides serialized handler execution to avoid race conditions std::unique_ptr<strand> _strand; ///< Provides serialized handler execution to avoid race conditions
LOGGER lg; ///< Logging instance LOGGER lg; ///< Logging instance
......
...@@ -59,6 +59,7 @@ public: ...@@ -59,6 +59,7 @@ public:
_mqttPart(""), _mqttPart(""),
_sync(true), _sync(true),
_keepRunning(false), _keepRunning(false),
_disabled(false),
_minValues(1), _minValues(1),
_interval(1000), _interval(1000),
_pendingTasks(0), _pendingTasks(0),
...@@ -70,6 +71,7 @@ public: ...@@ -70,6 +71,7 @@ public:
_mqttPart(other._mqttPart), _mqttPart(other._mqttPart),
_sync(other._sync), _sync(other._sync),
_keepRunning(false), _keepRunning(false),
_disabled(other._disabled),
_minValues(other._minValues), _minValues(other._minValues),
_interval(other._interval), _interval(other._interval),
_pendingTasks(0), _pendingTasks(0),
...@@ -83,6 +85,7 @@ public: ...@@ -83,6 +85,7 @@ public:
_mqttPart = other._mqttPart; _mqttPart = other._mqttPart;
_sync = other._sync; _sync = other._sync;
_keepRunning = false; _keepRunning = false;
_disabled = other._disabled;
_minValues = other._minValues; _minValues = other._minValues;
_interval = other._interval; _interval = other._interval;
_pendingTasks.store(0); _pendingTasks.store(0);
...@@ -96,6 +99,7 @@ public: ...@@ -96,6 +99,7 @@ public:
const std::string& getGroupName() const { return _groupName; } const std::string& getGroupName() const { return _groupName; }
const std::string& getMqttPart() const { return _mqttPart; } const std::string& getMqttPart() const { return _mqttPart; }
bool getSync() const { return _sync; } bool getSync() const { return _sync; }
bool isDisabled() const { return _disabled; }
unsigned getMinValues() const { return _minValues; } unsigned getMinValues() const { return _minValues; }
unsigned getInterval() const { return _interval; } unsigned getInterval() const { return _interval; }
///@} ///@}
...@@ -114,6 +118,7 @@ public: ...@@ -114,6 +118,7 @@ public:
} }
} }
void setSync(bool sync) { _sync = sync; } void setSync(bool sync) { _sync = sync; }
void setDisabled(const bool disabled) { _disabled = disabled; }
void setMinValues(unsigned minValues) { _minValues = minValues; } void setMinValues(unsigned minValues) { _minValues = minValues; }
void setInterval(unsigned interval) { _interval = interval; } void setInterval(unsigned interval) { _interval = interval; }
///@} ///@}
...@@ -197,6 +202,7 @@ public: ...@@ -197,6 +202,7 @@ public:
*/ */
virtual void printConfig(LOG_LEVEL ll) { virtual void printConfig(LOG_LEVEL ll) {
LOG_VAR(ll) << " Sensor Group: " << _groupName; LOG_VAR(ll) << " Sensor Group: " << _groupName;
LOG_VAR(ll) << " Disabled: " << (_disabled ? std::string("true") : std::string("false"));
if (_mqttPart != "") { if (_mqttPart != "") {
LOG_VAR(ll) << " MQTT part: " << _mqttPart; LOG_VAR(ll) << " MQTT part: " << _mqttPart;
} }
...@@ -296,6 +302,7 @@ protected: ...@@ -296,6 +302,7 @@ protected:
std::string _mqttPart; ///< MQTT part identifying this group std::string _mqttPart; ///< MQTT part identifying this group
bool _sync; ///< Should the timer (i.e. the read cycle of this groups) be synchronized with other groups? bool _sync; ///< Should the timer (i.e. the read cycle of this groups) be synchronized with other groups?
bool _keepRunning; ///< Continue with next reading cycle (i.e. set timer again after reading)? bool _keepRunning; ///< Continue with next reading cycle (i.e. set timer again after reading)?
bool _disabled;
unsigned int _minValues; ///< Minimum number of values a sensor should gather before they get pushed (to reduce MQTT overhead) unsigned int _minValues; ///< Minimum number of values a sensor should gather before they get pushed (to reduce MQTT overhead)
unsigned int _interval; ///< Reading interval cycle in milliseconds unsigned int _interval; ///< Reading interval cycle in milliseconds
std::atomic_uint _pendingTasks; ///< Number of currently outstanding read operations std::atomic_uint _pendingTasks; ///< Number of currently outstanding read operations
......
...@@ -162,6 +162,10 @@ public: ...@@ -162,6 +162,10 @@ public:
* actions are required during startup. * actions are required during startup.
*/ */
virtual void start() final override { virtual void start() final override {
if (_disabled) {
return;
}
if (_keepRunning) { if (_keepRunning) {
//we have been started already //we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running."; LOG(info) << "Sensorgroup " << _groupName << " already running.";
...@@ -174,10 +178,12 @@ public: ...@@ -174,10 +178,12 @@ public:
} }
if (_entity) { if (_entity) {
_keepRunning = true; if (!_entity->isDisabled()) {
_pendingTasks++; _keepRunning = true;
_timer->async_wait(_entity->getStrand()->wrap(std::bind(&SensorGroupTemplate::readAsync, this))); _pendingTasks++;
LOG(info) << "Sensorgroup " << _groupName << " started."; _timer->async_wait(_entity->getStrand()->wrap(std::bind(&SensorGroupTemplate::readAsync, this)));
LOG(info) << "Sensorgroup " << _groupName << " started.";
}
} else { } else {
LOG(error) << "No entity set for group " << _groupName << "! Cannot start polling."; LOG(error) << "No entity set for group " << _groupName << "! Cannot start polling.";
} }
...@@ -257,7 +263,7 @@ protected: ...@@ -257,7 +263,7 @@ protected:
*/ */
void readAsync() { void readAsync() {
this->read(); this->read();
if (_timer && _keepRunning) { if (_timer && _keepRunning && !_disabled && !_entity->isDisabled()) {
_timer->expires_at(timestamp2ptime(nextReadingTime())); _timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++; _pendingTasks++;
_timer->async_wait(_entity->getStrand()->wrap(std::bind(&SensorGroupTemplate::readAsync, this))); _timer->async_wait(_entity->getStrand()->wrap(std::bind(&SensorGroupTemplate::readAsync, this)));
...@@ -386,6 +392,10 @@ public: ...@@ -386,6 +392,10 @@ public:
* actions are required during startup. * actions are required during startup.
*/ */
virtual void start() final override { virtual void start() final override {
if (_disabled) {
return;
}
if (_keepRunning) { if (_keepRunning) {
//we have been started already //we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running."; LOG(info) << "Sensorgroup " << _groupName << " already running.";
...@@ -471,7 +481,7 @@ protected: ...@@ -471,7 +481,7 @@ protected:
*/ */
void readAsync() { void readAsync() {
this->read(); this->read();
if (_timer && _keepRunning) { if (_timer && _keepRunning && !_disabled) {
_timer->expires_at(timestamp2ptime(nextReadingTime())); _timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++; _pendingTasks++;
_timer->async_wait(std::bind(&SensorGroupTemplate::readAsync, this)); _timer->async_wait(std::bind(&SensorGroupTemplate::readAsync, this));
......
Markdown is supported
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