SensorGroup_cpp.sh 5.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/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"

Micha Mueller's avatar
Micha Mueller committed
15
${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const std::string& name) :
16
	SensorGroupTemplate(name) {
17
18
19
20
21
22
EOF

if [ "$enableEntities" = true ]
then
  echo "  _${ENTITY_NAME_LWC} = nullptr;" >> ${PLUGIN_NAME}SensorGroup.cpp
fi
23

24
cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
25
26
27
28
29
30
	/* 
	 *  TODO
	 *  Init attributes
	 */
}

31
${PLUGIN_NAME}SensorGroup::${PLUGIN_NAME}SensorGroup(const ${PLUGIN_NAME}SensorGroup& other) :
32
33
34
35
36
37
38
39
40
41
42
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
43
44
45
46
47
48
  /* 
	 *  TODO
	 *  Copy construct attributes
	 */
}

49
50
51
52
${PLUGIN_NAME}SensorGroup::~${PLUGIN_NAME}SensorGroup() {
	/* 
	 *  TODO
	 *  Tear down attributes
53
54
55
56
57
58
59
60
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
61
62
63
	 */
}

64
${PLUGIN_NAME}SensorGroup& ${PLUGIN_NAME}SensorGroup::operator=(const ${PLUGIN_NAME}SensorGroup& other) {
65
66
67
68
69
70
71
72
73
  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
74
75
76
77
78
79
80
81
  /* 
	 *  TODO
	 *  Implement assignment operator
	 */
  
  return *this;
}

82
83
84
85
86
87
88
89
90
91
92
93
void ${PLUGIN_NAME}SensorGroup::start() {
	if (_keepRunning) {
		//we have been started already
		LOG(info) << "Sensorgroup " << _groupName << " already running.";
		return;
	}

	/* 
	 *  TODO
	 *  Start plugin specific stuff
	 */

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
112
113
114
	_pendingTasks++;
	_timer->async_wait(std::bind(&${PLUGIN_NAME}SensorGroup::readAsync, this));
	LOG(info) << "Sensorgroup " << _groupName << " started.";
115
116
117
118
EOF
fi

cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
119
120
121
122
123
124
125
126
127
128
129
130
131
}

void ${PLUGIN_NAME}SensorGroup::stop() {
	_keepRunning = 0;
	
	/* 
	 *  TODO
	 *  Stop plugin specific stuff
	 */
	 
	LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}

132
133
void ${PLUGIN_NAME}SensorGroup::init(boost::asio::io_service& io) {
	SensorGroupTemplate::init(io);
134
135
136
137
138
139
140
141
142
143
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.";
	}
144
	/**
145
146
147
148
149
150
151
   * 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
152
153
	 * The SensorGroupTemplate::init(io); will then be called
	 */
154
155
156
157
EOF
fi

cat << EOF >> ${PLUGIN_NAME}SensorGroup.cpp
158
159
}

160
161
162
163
164
165
166
167
168
169
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.
170
171
172
173
174
175
176
177
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
178
							 */ 0;							 
Micha Mueller's avatar
Micha Mueller committed
179
			s->storeReading(reading);
180
#ifdef DEBUG
Micha Mueller's avatar
Micha Mueller committed
181
			LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
182
183
184
#endif
		}
	} catch (const std::exception& e) {
Micha Mueller's avatar
Micha Mueller committed
185
		LOG(error) << "Sensorgroup" << _groupName << " could not read value: " << e.what();
186
187
188
189
190
191
192
	}
}

void ${PLUGIN_NAME}SensorGroup::readAsync() {
	uint64_t now = getTimestamp();
	read();
	if (_timer && _keepRunning) {
193
		_timer->expires_at(timestamp2ptime(nextReadingTime()));
194
		_pendingTasks++;
195
196
197
198
199
200
201
202
203
204
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
205
206
207
	}
	_pendingTasks--;
}
208
209
210
211
212

void ${PLUGIN_NAME}SensorGroup::printConfig(LOG_LEVEL ll) {
  /*
	 * 	TODO
	 *  Log attributes here for debug reasons
213
214
215
216
217
218
219
220
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
221
	 */
222
	 
223
224
  LOG_VAR(ll) << "   NumSpacesAsIndention: " << 3;
}
225
EOF