Commit 8e8ba433 authored by Micha Mueller's avatar Micha Mueller
Browse files

Continue work on file generator scripts; add support for plugins with sensor groups

parent 933559c8
#!/bin/sh
cat << EOF > ${PLUGIN_NAME}Attributes.h
/*
* ${PLUGIN_NAME}Attributes.h
*
* Created on: ${DATE}
* Author: ${AUTHOR}
*/
#ifndef ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}ATTRIBUTES_H_
#define ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}ATTRIBUTES_H_
class ${PLUGIN_NAME}Attributes {
public:
${PLUGIN_NAME}Attributes() :
/*
* TODO
* Initializer list for attributes
*/ {}
virtual ~${PLUGIN_NAME}Attributes() {
/*
* TODO
* Deconstruct attributes if necessary
*/
}
/*
* TODO
* Getters + Setters if required for the attributes
*/
protected:
/*
* TODO
* Add plugin specific attributes, which are only required per SingleSensor
* or SensorGroup. Attributes which are required per SensorBase go to the
* SensorBase class.
*/
};
#endif /* ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}ATTRIBUTES_H_ */
EOF
......@@ -48,6 +48,58 @@ bool ${PLUGIN_NAME}Configurator::derivedReadConfig(boost::property_tree::iptree&
}
}
}
EOF
if [ "${enableGroups}" = true ]
then
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
//read groups
BOOST_FOREACH(boost::property_tree::iptree::value_type &group, cfg.get_child("groups")) {
if (STRCMP(group, "group")) {
LOG(debug) << "Group \"" << group.second.data() << "\"";
if (!group.second.empty()) {
${PLUGIN_NAME}SensorGroup ${PLUGIN_NAME_LWC}Group = new ${PLUGIN_NAME}SensorGroup(group.second.data());
/*
* TODO
* Read in other plugin specific group attributes
*/
//read mqttPart if present
std::string mqttPart = "";
boost::optional<boost::property_tree::iptree&> mqtt = group.second.get_child_optional("mqttprefix");
if (mqtt) {
mqttPart = mqtt.get().data();
}
//read remaining values
if (!readSensorInterface(${PLUGIN_NAME_LWC}Group, group.second)) {
LOG(warning) << " SensorGroup \"" << group.second.data() << "\" has bad values!";
}
//read sensors for this group
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, group.second) {
if (STRCMP(val, "sensor")) {
LOG(debug) << "Sensor \"" << val.second.data() << "\"";
${PLUGIN_NAME}SensorBase* ${PLUGIN_NAME_LWC}SB = new ${PLUGIN_NAME}SensorBase(val.second.data());
if(readSensorBase(*${PLUGIN_NAME_LWC}SB, val.second)) {
${PLUGIN_NAME_LWC}SB->setMqtt(_mqttPrefix + mqttPart + ${PLUGIN_NAME_LWC}SB->getMqtt());
LOG(debug) << " Sensor " << ${PLUGIN_NAME_LWC}SB->getName() << " using MQTT-Topic " << ${PLUGIN_NAME_LWC}SB->getMqtt();
${PLUGIN_NAME_LWC}Group->pushBackSensor(${PLUGIN_NAME_LWC}SB);
} else {
LOG(warning) << " Sensor \"" << val.second.data() << "\" has bad values! Ignoring...";
}
}
}
_sensorGroups.push_back(${PLUGIN_NAME_LWC}Group);
}
}
}
EOF
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
return true;
}
......@@ -58,7 +110,16 @@ bool ${PLUGIN_NAME}Configurator::derivedReadSensorBase(${PLUGIN_NAME}SensorBase&
* Read in plugin specific sensor attributes
*/
if (STRCMP(val, "mqttsuffix")) {
sensor.setMqtt(_mqttPrefix + val.second.data());
EOF
if [ "${enableGroups}" = true ]
then
echo "\t\t\tsensor.setMqtt(val.second.data());" >> ${PLUGIN_NAME}Configurator.cpp
else
echo "\t\t\tsensor.setMqtt(_mqttPrefix + val.second.data());" >> ${PLUGIN_NAME}Configurator.cpp
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.cpp
/* Example code:
} else if (STRCMP(val, ("stringAttribute"))) {
sensor.setStringAttribute(val.second.data());
......
......@@ -13,6 +13,14 @@ cat << EOF > ${PLUGIN_NAME}Configurator.h
#include "../../headers/ConfiguratorTemplate.h"
EOF
if [ "${enableGroups}" = true ]
then
echo "#include \"${PLUGIN_NAME}SensorGroup.h\"" >> ${PLUGIN_NAME}Configurator.h
fi
cat << EOF >> ${PLUGIN_NAME}Configurator.h
#include "${PLUGIN_NAME}SingleSensor.h"
class ${PLUGIN_NAME}Configurator : public ConfiguratorTemplate<${PLUGIN_NAME}SensorBase, ${PLUGIN_NAME}SingleSensor> {
......
#!/bin/sh
cat << EOF > ${PLUGIN_NAME}SensorGroup.cpp
/*
* ${PLUGIN_NAME}SensorGroup.cpp
*
* Created on: ${DATE}
* Author: ${AUTHOR}
*/
#include "${PLUGIN_NAME}SensorGroup.h"
#include "timestamp.h"
${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string name) :
SensorGroupTemplate(name) {
/*
* TODO
* Init attributes
*/
}
${PLUGIN_NAME}SensorGroup::~${PLUGIN_NAME}SensorGroup() {
/*
* TODO
* Tear down attributes
*/
}
void ${PLUGIN_NAME}SensorGroup::init(boost::asio::io_service& io) {
_cacheSize = _cacheInterval / _interval + 1;
_timer.reset(new boost::asio::deadline_timer(io, boost::posix_time::seconds(0)));
/*
* TODO
* Init plugin specific stuff
*/
for (auto s : _sensors) {
s->initSensor(_cacheSize);
}
}
void ${PLUGIN_NAME}SensorGroup::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running.";
return;
}
/*
* TODO
* Start plugin specific stuff
*/
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this));
LOG(info) << "Sensorgroup " << _groupName << " started.";
}
void ${PLUGIN_NAME}SensorGroup::stop() {
_keepRunning = 0;
/*
* TODO
* Stop plugin specific stuff
*/
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}
void ${PLUGIN_NAME}SensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
for(auto s : _sensors) {
reading.value = /*
* TODO
* Read a value for every sensor affiliated with this group and store
* it with the appropriate sensor.
*/ 0;
s->storeReading(reading, _cacheIndex);
#ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << ": \"" << reading.value << "\"";
#endif
}
} catch (const std::exception& e) {
LOG(error) << "Sensorgroup" << _groupName << " could not read value";
return;
}
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
}
void ${PLUGIN_NAME}SensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this));
}
_pendingTasks--;
}
EOF
#!/bin/sh
cat << EOF > ${PLUGIN_NAME}SensorGroup.h
/*
* ${PLUGIN_NAME}SensorGroup.h
*
* Created on: ${DATE}
* Author: ${AUTHOR}
*/
#ifndef ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORGROUP_H_
#define ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORGROUP_H_
#include "../../headers/SensorGroupTemplate.h"
#include "${PLUGIN_NAME}Attributes.h"
#include "${PLUGIN_NAME}SensorBase.h"
class ${PLUGIN_NAME}SensorGroup : public SensorGroupTemplate<${PLUGIN_NAME}SensorBase>, public ${PLUGIN_NAME}Attributes {
public:
${PLUGIN_NAME}SensorGroup(const std::string name);
virtual ~${PLUGIN_NAME}SensorGroup();
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
private:
void read() override;
void readAsync() override;
/*
* TODO
* Add group internal attributes
*/
};
#endif /* ${PLUGIN_NAME_UPC}_${PLUGIN_NAME_UPC}SENSORGROUP_H_ */
EOF
......@@ -13,8 +13,19 @@ cat << EOF > ${PLUGIN_NAME}SingleSensor.h
#include "${PLUGIN_NAME}SensorBase.h"
#include "../../headers/SingleSensor.h"
EOF
class ${PLUGIN_NAME}SingleSensor : public ${PLUGIN_NAME}SensorBase, public SingleSensor {
if [ "${enableGroups}" = true ]
then
echo "#include \"${PLUGIN_NAME}Attributes.h\"" >> ${PLUGIN_NAME}SingleSensor.h
echo "" >> ${PLUGIN_NAME}SingleSensor.h
echo "class ${PLUGIN_NAME}SingleSensor : public ${PLUGIN_NAME}SensorBase, public ${PLUGIN_NAME}Attributes public SingleSensor {" >> ${PLUGIN_NAME}SingleSensor.h
else
echo "" >> ${PLUGIN_NAME}SingleSensor.h
echo "class ${PLUGIN_NAME}SingleSensor : public ${PLUGIN_NAME}SensorBase, public SingleSensor {" >> ${PLUGIN_NAME}SingleSensor.h
fi
cat << EOF >> ${PLUGIN_NAME}SingleSensor.h
public:
${PLUGIN_NAME}SingleSensor(const std::string& name);
......
......@@ -20,7 +20,7 @@ sensorTemplates {
}
sensors {
;define here the actual sensors for your plugin
;define here the actual singleSensors for your plugin
sensor sens1 {
default temp1
......@@ -29,3 +29,24 @@ sensors {
}
}
EOF
if [ "${enableGroups}" = true ]
then
cat << EOF >> ${PLUGIN_NAME}.conf
groups {
;define here the sensor groups for your plugin
group g1 {
;specify group attributes here
interval 1000
mqttprefix 01
;define sensors belonging to the group below
sensor gSens {
mqttsuffix 00
}
}
}
EOF
fi
#!/bin/sh
# Only plugin generation without groups supported currently!
# TODO generate stuff for config and Makefile
# Reminder in shell 0 = true, 1 = false
# Init variables
DATE=`date '+%d.%m.%Y'`
AUTHOR="Your name goes here!"
PLUGIN_NAME=""
enableGroups=false
# parse input
while getopts "hp:ga:" opt; do
case "$opt" in
h)
echo "Usage:"
echo "./generatePlugin.sh -p pluginName"
echo "Options:"
echo "-h\tPrint this help section"
echo "-g\tGenerate files for SensorGroups"
echo "-a xx\tSpecify name of the author which is to be mentioned in the"
echo "\tsource files"
exit 0
;;
p) PLUGIN_NAME=$OPTARG
echo "Plugin named ${PLUGIN_NAME}"
;;
g) enableGroups=true
echo "Groups enabled"
;;
a) AUTHOR=$OPTARG
echo "Author: ${AUTHOR}"
;;
esac
done
# first check input parameters
if [ $# -ne 1 ]
if [ -z ${PLUGIN_NAME} ]
then
echo "False number of arguments!"
echo "Usage:"
echo "./generatePlugin.sh pluginName"
echo "Invalid plugin name!"
exit 1
fi
# Set variables
DATE=`date '+%d.%m.%Y'`
AUTHOR="Your name goes here!"
PLUGIN_NAME=$1
# Set further variables
PLUGIN_NAME_UPC=`echo $PLUGIN_NAME | tr 'a-z' 'A-Z'`
PLUGIN_NAME_LWC=`echo $PLUGIN_NAME | tr 'A-Z' 'a-z'`
if [ -e ${PLUGIN_NAME_LWC} ]
then
echo "${PLUGIN_NAME_LWC}/ directory already present"
echo "${PLUGIN_NAME_LWC}/ directory already present. Overwriting"
else
echo "Creating ${PLUGIN_NAME_LWC}/ directory"
mkdir ${PLUGIN_NAME_LWC}
......@@ -32,6 +57,21 @@ cd ${PLUGIN_NAME_LWC}
echo "Generating ${PLUGIN_NAME}SensorBase.h ..."
. ../SensorBase_h.sh
if [ "${enableGroups}" = true ]
then
# Generate ...Attributes.h
echo "Generating ${PLUGIN_NAME}Attributes.h ..."
. ../Attributes_h.sh
# Generate ...SensorGroup.h
echo "Generating ${PLUGIN_NAME}SensorGroup.h ..."
. ../SensorGroup_h.sh
# Generate ...SensorGroup.cpp
echo "Generating ${PLUGIN_NAME}SensorGroup.cpp ..."
. ../SensorGroup_cpp.sh
fi
# Generate ...SingleSensor.h
echo "Generating ${PLUGIN_NAME}SingleSensor.h ..."
. ../SingleSensor_h.sh
......
......@@ -4,8 +4,16 @@ cat << EOF > appendToMakefile.txt
Append to PLUGINS_BASE variable: libdcdbplugin_${PLUGIN_NAME_LWC}
Append at end of Makefile:
EOF
if [ "${enableGroups}" = true ]
then
echo "libdcdbplugin_${PLUGIN_NAME_LWC}.\$(LIBEXT): src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}SingleSensor.o src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}Configurator.o src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}SensorGroup.o" >> appendToMakefile.txt
else
echo "libdcdbplugin_${PLUGIN_NAME_LWC}.\$(LIBEXT): src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}SingleSensor.o src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}Configurator.o" >> appendToMakefile.txt
fi
libdcdbplugin_${PLUGIN_NAME_LWC}.\$(LIBEXT): src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}SingleSensor.o src/sensors/${PLUGIN_NAME_LWC}/${PLUGIN_NAME}Configurator.o
cat << EOF >> appendToMakefile.txt
\$(CXX) \$(LIBFLAGS)\$@ -o \$@ $^ -L\$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system
NOTE: Probably you will have to append further libraries to the linker for your plugin to compile
......
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