Commit 90c38552 authored by Micha Mueller's avatar Micha Mueller
Browse files

Add support for entities in pluginGenerator scripts

parent 8d1cbabf
...@@ -13,8 +13,16 @@ cat << EOF > ${PLUGIN_NAME}Configurator.cpp ...@@ -13,8 +13,16 @@ cat << EOF > ${PLUGIN_NAME}Configurator.cpp
${PLUGIN_NAME}Configurator::${PLUGIN_NAME}Configurator() { ${PLUGIN_NAME}Configurator::${PLUGIN_NAME}Configurator() {
/* /*
* TODO * TODO
* If you want sensor, group or entity to be named differently in the config file, you can change it here * If you want sensor or group to be named differently in the config file, you can change it here
*/ */
EOF
if [ "$enableEntities" = true ]
then
echo " _entityName = \"${ENTITY_NAME_LWC}\";" >> ${PLUGIN_NAME}Configurator.cpp
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
_groupName = "group"; _groupName = "group";
_baseName = "sensor"; _baseName = "sensor";
} }
...@@ -40,6 +48,42 @@ void ${PLUGIN_NAME}Configurator::sensorGroup(${PLUGIN_NAME}SensorGroup& s, CFG_V ...@@ -40,6 +48,42 @@ void ${PLUGIN_NAME}Configurator::sensorGroup(${PLUGIN_NAME}SensorGroup& s, CFG_V
} }
} }
EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
void ${PLUGIN_NAME}Configurator::sensorEntity(${PLUGIN_NAME}${ENTITY_NAME}& s, CFG_VAL config) {
ADD {
/*
* TODO
* Add ATTRIBUTE macros for ${ENTITY_NAME} attributes
*/
}
}
bool ${PLUGIN_NAME}Configurator::isEntityOfGroup(${PLUGIN_NAME}${ENTITY_NAME}& e, ${PLUGIN_NAME}SensorGroup& g) {
return (g.get${ENTITY_NAME}() == &e);
}
void ${PLUGIN_NAME}Configurator::setEntityForGroup(${PLUGIN_NAME}${ENTITY_NAME}& e, ${PLUGIN_NAME}SensorGroup& g) {
g.set${ENTITY_NAME}(&e);
}
void ${PLUGIN_NAME}Configurator::finalizeGroup(${PLUGIN_NAME}SensorGroup& g) {
/*
* TODO
* Do final configuration of group depending on its entity.
* If not required this method can be removed.
*/
//e.g. configure MQTT depending on entity
//g.setMqttPart(g.get${ENTITY_NAME}()->getMqttPart() + g.getMqttPart());
}
EOF
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
void ${PLUGIN_NAME}Configurator::printConfiguratorConfig(LOG_LEVEL ll) { void ${PLUGIN_NAME}Configurator::printConfiguratorConfig(LOG_LEVEL ll) {
/* /*
* TODO * TODO
...@@ -47,6 +91,24 @@ void ${PLUGIN_NAME}Configurator::printConfiguratorConfig(LOG_LEVEL ll) { ...@@ -47,6 +91,24 @@ void ${PLUGIN_NAME}Configurator::printConfiguratorConfig(LOG_LEVEL ll) {
* not attributes to log. * not attributes to log.
*/ */
LOG_VAR(ll) << " NumSpacesAsIndention: " << 2; LOG_VAR(ll) << " NumSpacesAsIndention: " << 2;
EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
LOG_VAR(ll) << " ${ENTITY_NAME}s:";
if (_sensorEntitys.size() != 0) {
for(auto e : _sensorEntitys) {
e->printConfig(ll);
}
} else {
LOG_VAR(ll) << " No ${ENTITY_NAME}s present!";
}
EOF
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
} }
EOF EOF
...@@ -13,6 +13,14 @@ cat << EOF > ${PLUGIN_NAME}Configurator.h ...@@ -13,6 +13,14 @@ cat << EOF > ${PLUGIN_NAME}Configurator.h
#include "../../includes/ConfiguratorTemplate.h" #include "../../includes/ConfiguratorTemplate.h"
#include "${PLUGIN_NAME}SensorGroup.h" #include "${PLUGIN_NAME}SensorGroup.h"
EOF
if [ "$enableEntities" = true ]
then
echo "#include \"${PLUGIN_NAME}${ENTITY_NAME}.h\"" >> ${PLUGIN_NAME}Configurator.h
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.h
class ${PLUGIN_NAME}Configurator : public ConfiguratorTemplate<${PLUGIN_NAME}SensorBase, ${PLUGIN_NAME}SensorGroup> { class ${PLUGIN_NAME}Configurator : public ConfiguratorTemplate<${PLUGIN_NAME}SensorBase, ${PLUGIN_NAME}SensorGroup> {
...@@ -24,6 +32,20 @@ protected: ...@@ -24,6 +32,20 @@ protected:
/* Overwritten from ConfiguratorTemplate */ /* Overwritten from ConfiguratorTemplate */
void sensorBase(${PLUGIN_NAME}SensorBase& s, CFG_VAL config) override; void sensorBase(${PLUGIN_NAME}SensorBase& s, CFG_VAL config) override;
void sensorGroup(${PLUGIN_NAME}SensorGroup& s, CFG_VAL config) override; void sensorGroup(${PLUGIN_NAME}SensorGroup& s, CFG_VAL config) override;
EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME}Configurator.h
void sensorEntity(${PLUGIN_NAME}${ENTITY_NAME}& s, CFG_VAL config) override;
bool isEntityOfGroup(${PLUGIN_NAME}${ENTITY_NAME}& e, ${PLUGIN_NAME}SensorGroup& g) override;
void setEntityForGroup(${PLUGIN_NAME}${ENTITY_NAME}& e, ${PLUGIN_NAME}SensorGroup& g) override;
void finalizeGroup(${PLUGIN_NAME}SensorGroup& g) override;
EOF
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.h
void printConfiguratorConfig(LOG_LEVEL ll) override; void printConfiguratorConfig(LOG_LEVEL ll) override;
}; };
......
#!/bin/sh
cat << EOF > ${PLUGIN_NAME}${ENTITY_NAME}.cpp
/*
* ${PLUGIN_NAME}${ENTITY_NAME}.cpp
*
* Created on: ${DATE}
* Author: ${AUTHOR}
*/
#include "${PLUGIN_NAME}${ENTITY_NAME}.h"
${PLUGIN_NAME}${ENTITY_NAME}::${PLUGIN_NAME}${ENTITY_NAME}() {
/*
* TODO
* Init attributes
*/
_strand = nullptr;
}
${PLUGIN_NAME}${ENTITY_NAME}::~${PLUGIN_NAME}${ENTITY_NAME}() {
/*
* TODO
* Tear down attributes
*/
if (_strand) {
delete _strand;
}
}
/*
* TODO
* Implement own methods
*/
void ${PLUGIN_NAME}${ENTITY_NAME}::initializeStrand(boost::asio::io_service& io) {
if (!_strand) {
_strand = new boost::asio::io_service::strand(io);
}
}
void ${PLUGIN_NAME}${ENTITY_NAME}::printConfig(LOG_LEVEL ll) {
/*
* TODO
* Log attributes here for debug reasons
*/
LOG_VAR(ll) << " NumSpacesAsIndention: " << 3;
}
EOF
#!/bin/sh
cat << EOF > ${PLUGIN_NAME}${ENTITY_NAME}.h
/*
* ${PLUGIN_NAME}${ENTITY_NAME}.h
*
* Created on: ${DATE}
* Author: ${AUTHOR}
*/
#ifndef ${PLUGIN_NAME_UPC}${ENTITY_NAME_UPC}_H_
#define ${PLUGIN_NAME_UPC}${ENTITY_NAME_UPC}_H_
#include <boost/asio.hpp>
#include "../../includes/Logging.h"
class ${PLUGIN_NAME}${ENTITY_NAME} {
public:
${PLUGIN_NAME}${ENTITY_NAME}();
virtual ~${PLUGIN_NAME}${ENTITY_NAME}();
/*
* TODO
* Add methods the entity provides to the SensorGroups.
* Add getter/setter for the attributes as required.
* To make the entity thread safe if called from multiple SensorGroups at
* once, we use a boost::strand. Feel free to get rid of _strand and related
* methods if your entity is already thread safe.
*/
void initializeStrand(boost::asio::io_service& io);
boost::asio::io_service::strand* getStrand() const { return _strand; }
void printConfig(LOG_LEVEL ll);
private:
/*
* TODO
* Add attributes required for your entity
* If your entity is thread safe you can remove the _strand
*/
boost::asio::io_service::strand* _strand;
LOGGER lg;
};
#endif /* ${PLUGIN_NAME_UPC}${ENTITY_NAME_UPC}_H_ */
EOF
...@@ -12,6 +12,15 @@ cat << EOF > ${PLUGIN_NAME}SensorBase.h ...@@ -12,6 +12,15 @@ cat << EOF > ${PLUGIN_NAME}SensorBase.h
#define ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORBASE_H_ #define ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORBASE_H_
#include "../../includes/SensorBase.h" #include "../../includes/SensorBase.h"
EOF
if [ "$enableEntities" = true ]
then
echo "" >> ${PLUGIN_NAME}SensorBase.h
echo "#include \"${PLUGIN_NAME}${ENTITY_NAME}.h\"" >> ${PLUGIN_NAME}SensorBase.h
fi
cat << EOF >> ${PLUGIN_NAME}SensorBase.h
/* /*
* TODO * TODO
......
...@@ -14,7 +14,14 @@ cat << EOF > ${PLUGIN_NAME}SensorGroup.cpp ...@@ -14,7 +14,14 @@ cat << EOF > ${PLUGIN_NAME}SensorGroup.cpp
${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string& name) : ${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string& name) :
SensorGroupTemplate(name) { SensorGroupTemplate(name) {
EOF
if [ "$enableEntities" = true ]
then
echo " _${ENTITY_NAME_LWC} = nullptr;" >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
/* /*
* TODO * TODO
* Init attributes * Init attributes
...@@ -22,8 +29,17 @@ ${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string& name) : ...@@ -22,8 +29,17 @@ ${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string& name) :
} }
${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const ${PLUGIN_NAME}SensorGroup& other) : ${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const ${PLUGIN_NAME}SensorGroup& other) :
SensorGroupTemplate(other) { EOF
if [ "$enableEntities" = true ]
then
echo " SensorGroupTemplate(other)," >> ${PLUGIN_NAME}SensorGroup.cpp
echo " _${ENTITY_NAME_LWC}(other._${ENTITY_NAME_LWC}) {" >> ${PLUGIN_NAME}SensorGroup.cpp
else
echo " SensorGroupTemplate(other) {" >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
/* /*
* TODO * TODO
* Copy construct attributes * Copy construct attributes
...@@ -34,11 +50,27 @@ ${PLUGIN_NAME}SensorGroup::~${PLUGIN_NAME}SensorGroup() { ...@@ -34,11 +50,27 @@ ${PLUGIN_NAME}SensorGroup::~${PLUGIN_NAME}SensorGroup() {
/* /*
* TODO * TODO
* Tear down attributes * Tear down attributes
EOF
if [ "$enableEntities" = true ]
then
echo " * But do not delete _${ENTITY_NAME_LWC}! The configurator takes care of it." >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
*/ */
} }
${PLUGIN_NAME}SensorGroup& ${PLUGIN_NAME}SensorGroup::operator=(const ${PLUGIN_NAME}SensorGroup& other) { ${PLUGIN_NAME}SensorGroup& ${PLUGIN_NAME}SensorGroup::operator=(const ${PLUGIN_NAME}SensorGroup& other) {
SensorGroupTemplate::operator=(other); SensorGroupTemplate::operator=(other);
EOF
if [ "$enableEntities" = true ]
then
echo " _${ENTITY_NAME_LWC} = other._${ENTITY_NAME_LWC};" >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
/* /*
* TODO * TODO
* Implement assignment operator * Implement assignment operator
...@@ -59,10 +91,31 @@ void ${PLUGIN_NAME}SensorGroup::start() { ...@@ -59,10 +91,31 @@ void ${PLUGIN_NAME}SensorGroup::start() {
* Start plugin specific stuff * Start plugin specific stuff
*/ */
_keepRunning = 1; EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
if (_${ENTITY_NAME_LWC}) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_${ENTITY_NAME_LWC}->getStrand()->wrap(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this)));
LOG(info) << "Sensorgroup " << _groupName << " started.";
} else {
LOG(error) << "No ${ENTITY_NAME} set for sensorgroup " << _groupName << "! Cannot start polling.";
return;
}
EOF
else
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
_keepRunning = 1;
_pendingTasks++; _pendingTasks++;
_timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this)); _timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this));
LOG(info) << "Sensorgroup " << _groupName << " started."; LOG(info) << "Sensorgroup " << _groupName << " started.";
EOF
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
} }
void ${PLUGIN_NAME}SensorGroup::stop() { void ${PLUGIN_NAME}SensorGroup::stop() {
...@@ -78,10 +131,30 @@ void ${PLUGIN_NAME}SensorGroup::stop() { ...@@ -78,10 +131,30 @@ void ${PLUGIN_NAME}SensorGroup::stop() {
void ${PLUGIN_NAME}SensorGroup::init(boost::asio::io_service& io) { void ${PLUGIN_NAME}SensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io); SensorGroupTemplate::init(io);
EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
if (_${ENTITY_NAME_LWC}) {
_${ENTITY_NAME_LWC}->initializeStrand(io);
} else {
LOG(error)<< "No ${ENTITY_NAME} set for sensorgroup " << _groupName << "! Cannot initialize sensor.";
}
/** /**
* TODO Initialize here Sensor Group or remove this member function completely * TODO Initialize here Sensor Group if required
*/
EOF
else
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
/**
* TODO Initialize here Sensor Group or remove this member function completely
* The SensorGroupTemplate::init(io); will then be called * The SensorGroupTemplate::init(io); will then be called
*/ */
EOF
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
} }
void ${PLUGIN_NAME}SensorGroup::read() { void ${PLUGIN_NAME}SensorGroup::read() {
...@@ -94,6 +167,14 @@ void ${PLUGIN_NAME}SensorGroup::read() { ...@@ -94,6 +167,14 @@ void ${PLUGIN_NAME}SensorGroup::read() {
* TODO * TODO
* Read a value for every sensor affiliated with this group and store * Read a value for every sensor affiliated with this group and store
* it with the appropriate sensor. * it with the appropriate sensor.
EOF
if [ "$enableEntities" = true ]
then
echo " * Make use of _${ENTITY_NAME_LWC} as required." >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
*/ 0; */ 0;
s->storeReading(reading); s->storeReading(reading);
#ifdef DEBUG #ifdef DEBUG
...@@ -109,9 +190,18 @@ void ${PLUGIN_NAME}SensorGroup::readAsync() { ...@@ -109,9 +190,18 @@ void ${PLUGIN_NAME}SensorGroup::readAsync() {
uint64_t now = getTimestamp(); uint64_t now = getTimestamp();
read(); read();
if (_timer && _keepRunning) { if (_timer && _keepRunning) {
_timer->expires_at(nextReadingTime()); _timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++; _pendingTasks++;
_timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this)); EOF
if [ "$enableEntities" = true ]
then
echo " _timer->async_wait(_${ENTITY_NAME_LWC}->getStrand()->wrap(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this)));" >> ${PLUGIN_NAME}SensorGroup.cpp
else
echo " _timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this));" >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
} }
_pendingTasks--; _pendingTasks--;
} }
...@@ -120,7 +210,16 @@ void ${PLUGIN_NAME}SensorGroup::printConfig(LOG_LEVEL ll) { ...@@ -120,7 +210,16 @@ void ${PLUGIN_NAME}SensorGroup::printConfig(LOG_LEVEL ll) {
/* /*
* TODO * TODO
* Log attributes here for debug reasons * Log attributes here for debug reasons
EOF
if [ "$enableEntities" = true ]
then
echo " * Printing the name of _${ENTITY_NAME_LWC} may be a good idea." >> ${PLUGIN_NAME}SensorGroup.cpp
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
*/ */
LOG_VAR(ll) << " NumSpacesAsIndention: " << 3; LOG_VAR(ll) << " NumSpacesAsIndention: " << 3;
} }
EOF EOF
...@@ -30,6 +30,15 @@ public: ...@@ -30,6 +30,15 @@ public:
* TODO * TODO
* Add getter and setters for group attributes if required * Add getter and setters for group attributes if required
*/ */
EOF
if [ "$enableEntities" = true ]
then
echo " void set${ENTITY_NAME}(${PLUGIN_NAME}${ENTITY_NAME}* ${ENTITY_NAME_LWC}) { _${ENTITY_NAME_LWC} = ${ENTITY_NAME_LWC}; }" >> ${PLUGIN_NAME}SensorGroup.h
echo " ${PLUGIN_NAME}${ENTITY_NAME}* const get${ENTITY_NAME}() const { return _${ENTITY_NAME_LWC}; }" >> ${PLUGIN_NAME}SensorGroup.h
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.h
void printConfig(LOG_LEVEL ll) override; void printConfig(LOG_LEVEL ll) override;
...@@ -41,6 +50,14 @@ private: ...@@ -41,6 +50,14 @@ private:
* TODO * TODO
* Add group internal attributes * Add group internal attributes
*/ */
EOF
if [ "$enableEntities" = true ]
then
echo " ${PLUGIN_NAME}${ENTITY_NAME}* _${ENTITY_NAME_LWC};" >> ${PLUGIN_NAME}SensorGroup.h
fi
cat << EOF >> ${PLUGIN_NAME}SensorGroup.h
}; };
#endif /* ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORGROUP_H_ */ #endif /* ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORGROUP_H_ */
......
...@@ -9,6 +9,50 @@ global { ...@@ -9,6 +9,50 @@ global {
;add here other global attributes for your plugin ;add here other global attributes for your plugin
} }
EOF
if [ "$enableEntities" = true ]
then
cat << EOF >> ${PLUGIN_NAME_LWC}.conf
template_group def1 {
;define template groups by appending "template_"
interval 1000
minValues 3
groupAtt 1234
;add other attributes your plugin requires for a sensor group
;define sensors belonging to the group below
sensor temp1 {
sensorAtt 5678
;add other attributes your plugin requires for a sensor
}
}
${ENTITY_NAME_LWC} ent1 {
entityAtt 1234
single_sensor sens1 {
;if you want a group with only one sensor you can use a single_sensor
default temp1
mqttsuffix 0001
;add other attributes your plugin requires for a sensor
}
group g1 {
interval 1000
mqttprefix 01
default def1
;sensor temp1 is taken from def1 and does not need to be redefined
sensor gSens {
mqttsuffix 00
}
}
}
EOF
else
cat << EOF >> ${PLUGIN_NAME_LWC}.conf
template_group def1 { template_group def1 {
;define template groups by appending "template_" ;define template groups by appending "template_"
interval 1000 interval 1000
...@@ -42,3 +86,4 @@ group g1 { ...@@ -42,3 +86,4 @@ group g1 {
} }
} }
EOF EOF
fi
...@@ -2,26 +2,23 @@ ...@@ -2,26 +2,23 @@
# Reminder in shell 0 = true, 1 = false # Reminder in shell 0 = true, 1 = false
#TODO: make sensor, group and entity name configurable #TODO: make sensor and group name configurable
echo "NOTE: Entities currently not supported!"
#TODO: add option to create entity class
# Init variables # Init variables
DATE=`date '+%d.%m.%Y'` DATE=`date '+%d.%m.%Y'`
AUTHOR="Your name goes here!" AUTHOR="Your name goes here!"
PLUGIN_NAME="" PLUGIN_NAME=""
enableGroups=false enableEntities=false
# parse input # parse input
while getopts "hp:ga:" opt; do while getopts "hp:e:a:" opt; do
case "$opt" in case "$opt" in
h)