SensorBase.h 11.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * SensorBase.h
 *
 *  Created on: 09.08.2018
 *      Author: Micha Mueller
 */

#ifndef SRC_SENSORBASE_H_
#define SRC_SENSORBASE_H_

11
#include <fstream>
12
#include <memory>
13
#include <string>
14
#include <limits.h>
15
16
17
#include <boost/lockfree/spsc_queue.hpp>

typedef struct {
18
	int64_t  value;
19
20
21
	uint64_t timestamp;
} reading_t;

22
23
24
25
26
typedef struct {
  uint64_t value;
  uint64_t timestamp;
} ureading_t;

27
28
class SensorBase {
public:
lu43jih's avatar
lu43jih committed
29
30
	static const size_t QUEUE_MAXLIMIT=1024;

31
32
	SensorBase(const std::string& name) :
		_name(name),
33
		_mqtt(""),
34
		_sinkPath(""),
35
		_skipConstVal(false),
36
		_cacheInterval(900000),
37
38
		_subsamplingFactor(1),
		_subsamplingIndex(0),
39
		_cacheSize(1),
40
		_cacheIndex(0),
41
		_cache(nullptr),
42
		_delta(false),
43
		_firstReading(true),
44
45
		_readingQueue(nullptr),
		_sinkFile(nullptr) {
46

Alessio Netti's avatar
Alessio Netti committed
47
48
        _lastRawUValue.timestamp = 0;
        _lastRawUValue.value     = 0;
49
50
        _lastRawValue.timestamp = 0;
        _lastRawValue.value     = 0;
Alessio Netti's avatar
Alessio Netti committed
51
52
        _latestValue.timestamp	= 0;
        _latestValue.value		= 0;
53
54
        _lastSentValue.timestamp= 0;
        _lastSentValue.value	= 0;
Alessio Netti's avatar
Alessio Netti committed
55
56
        _accumulator.timestamp  = 0;
        _accumulator.value		= 0;
57
58
	}

59
60
61
	SensorBase(const SensorBase& other) :
		_name(other._name),
		_mqtt(other._mqtt),
62
		_skipConstVal(other._skipConstVal),
63
		_cacheInterval(other._cacheInterval),
64
65
		_subsamplingFactor(other._subsamplingFactor),
		_subsamplingIndex(0),
66
		_cacheSize(other._cacheSize),
67
		_cacheIndex(0),
68
		_cache(nullptr),
69
		_delta(other._delta),
70
		_firstReading(true),
Alessio Netti's avatar
Alessio Netti committed
71
		_lastRawUValue(other._lastRawUValue),
Alessio Netti's avatar
Alessio Netti committed
72
		_lastRawValue(other._lastRawValue),
Alessio Netti's avatar
Alessio Netti committed
73
		_latestValue(other._latestValue),
74
        _lastSentValue(other._lastSentValue),
Alessio Netti's avatar
Alessio Netti committed
75
        _accumulator(other._accumulator),
76
77
		_readingQueue(nullptr),
		_sinkFile(nullptr) {}
78
79
80
81
82
83

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
84
		_skipConstVal = other._skipConstVal;
85
		_cacheInterval = other._cacheInterval;
86
87
		_subsamplingFactor = other._subsamplingFactor;
		_subsamplingIndex = 0;
88
		_cacheSize = other._cacheSize;
89
		_cacheIndex = 0;
90
		_cache.reset(nullptr);
91
		_delta = other._delta;
92
		_firstReading = true;
93
94
95
96
		_lastRawUValue.timestamp = other._lastRawUValue.timestamp;
        _lastRawUValue.value     = other._lastRawUValue.value;
		_lastRawValue.timestamp = other._lastRawValue.timestamp;
        _lastRawValue.value     = other._lastRawValue.value;
97
98
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
99
100
        _lastSentValue.timestamp= other._lastSentValue.timestamp;
        _lastSentValue.value	= other._lastSentValue.value;
Alessio Netti's avatar
Alessio Netti committed
101
102
        _accumulator.timestamp  = other._accumulator.timestamp;
        _accumulator.value      = other._accumulator.value;
103
		_readingQueue.reset(nullptr);
104
		_sinkFile.reset(nullptr);
105
106

		return *this;
107
108
	}

109
	const bool 				isDelta()			const 	{ return _delta;}
110
111
	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
112
	const std::string&		getSinkPath() 		const	{ return _sinkPath; }
113
	bool					getSkipConstVal()	const	{ return _skipConstVal; }
114
	unsigned				getCacheSize()		const	{ return _cacheSize; }
Alessio Netti's avatar
Alessio Netti committed
115
	unsigned				getCacheInterval()	const	{ return _cacheInterval; }
116
	unsigned 				getSubsampling()	const   { return _subsamplingFactor; }
117
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
Micha Mueller's avatar
Micha Mueller committed
118
	const reading_t&		getLatestValue()	const	{ return _latestValue; }
Alessio Netti's avatar
Alessio Netti committed
119
	const bool				isInit()			const 	{ return _cache && _readingQueue; }
120

121
	void	setSkipConstVal(bool skipConstVal)				{ _skipConstVal = skipConstVal;	}
122
123
124
	void	setDelta(const bool delta) 						{ _delta = delta; }
	void	setName(const std::string& name, int cpuID=-1)	{ _name = formatName(name, cpuID); }
	void	setMqtt(const std::string& mqtt)				{ _mqtt = mqtt; }
125
	void 	setSinkPath(const std::string& path)			{ _sinkPath = path; }
126
	void 	setCacheInterval(unsigned cacheInterval)		{ _cacheInterval = cacheInterval; }
127
	void	setSubsampling(unsigned factor)					{ _subsamplingFactor = factor; }
128
129
	void    setLastRaw(int64_t raw)                         { _lastRawValue.value = raw; }
	void    setLastURaw(uint64_t raw)                       { _lastRawUValue.value = raw; }
130
131
132

	const std::size_t	getSizeOfReadingQueue() const { return _readingQueue->read_available(); }
	std::size_t 		popReadingQueue(reading_t *reads, std::size_t max) const	{ return _readingQueue->pop(reads, max); }
Alessio Netti's avatar
Alessio Netti committed
133
	void 				clearReadingQueue() const	{ reading_t buf; while(_readingQueue->pop(buf)) {} }
134
135
	void				pushReadingQueue(reading_t *reads, std::size_t count) const	{ _readingQueue->push(reads, count); }

136
137
	void initSensor(unsigned interval) {
		_cacheSize = _cacheInterval / interval + 1;
138
		if(!_cache) {
139
140
			_cache.reset(new reading_t[_cacheSize]);
			for(unsigned i = 0; i < _cacheSize; i++) {
141
142
143
144
				_cache[i] = _latestValue;	//_latestValue should equal (0,0) at this point
			}
		}
		if(!_readingQueue) {
lu43jih's avatar
lu43jih committed
145
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(QUEUE_MAXLIMIT));
146
		}
147
148
149
150
151
		if(!_sinkFile && _sinkPath != "") {
			_sinkFile.reset(new std::ofstream(_sinkPath));
			if(!_sinkFile->is_open())
				_sinkFile.reset(nullptr);
		}
152
153
	}

154
155
156
157
158
159
160
161
162
163
164
165
	/**
	 * Store a reading, in order to get it pushed to the data base eventually.
	 * Also this methods takes care of other optional reading post-processing,
	 * e.g. delta computation, subsampling, caching, scaling, etc.
	 *
	 * This is the primary storeReading() and should be used whenever possible.
	 *
	 * @param rawReading  Reading struct with value and timestamp to be stored.
	 * @param factor      Scaling factor, which is applied to the reading value (optional)
	 * @param maxValue    Maximum possible value of the reading; required for the
	 *                    delta computation to detect an overflow.
	 */
Micha Mueller's avatar
Micha Mueller committed
166
	void storeReading(reading_t rawReading, double factor=1.0, long long maxValue=LLONG_MAX) {
Michael Ott's avatar
Michael Ott committed
167
		reading_t reading = rawReading;
168
		if( _delta ) {
169
170
171
172
173
174
175
176
177
178
		    if (!_firstReading) {
              if (rawReading.value < _lastRawValue.value)
                  reading.value = (rawReading.value + (maxValue - _lastRawValue.value)) * factor;
              else
                  reading.value = (rawReading.value - _lastRawValue.value) * factor;
		    } else {
		      _firstReading = false;
		      _lastRawValue = rawReading;
		      return;
		    }
Michael Ott's avatar
Michael Ott committed
179
			_lastRawValue = rawReading;
180
181
		}
		else
Michael Ott's avatar
Michael Ott committed
182
			reading.value = rawReading.value * factor;
183

Alessio Netti's avatar
Alessio Netti committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
		if( _delta )
			// If in delta mode, _accumulator acts as a buffer, summing all deltas for the subsampling period
		    _accumulator.value += reading.value;
		else
		    _accumulator.value = reading.value;

		if (_subsamplingIndex++ % _subsamplingFactor == 0) {
            _accumulator.timestamp = reading.timestamp;
            //TODO: if sensor starts with values of 0, these won't be pushed. This should be fixed
            if( !(_skipConstVal && (_accumulator.value == _lastSentValue.value)) ) {
                _readingQueue->push(_accumulator);
                _lastSentValue = _accumulator;
            }
            // We reset the accumulator's value for the correct accumulation of deltas
			_accumulator.value = 0;
199
200
		}
		if (_sinkFile) {
Alessio Netti's avatar
Alessio Netti committed
201
202
203
204
		    try {
                _sinkFile->seekp(0, std::ios::beg);
                *_sinkFile << reading.value << std::endl;
            } catch(const std::exception &e) { _sinkFile->close(); _sinkFile.reset(nullptr); }
205
		}
206

Michael Ott's avatar
Michael Ott committed
207
		_cache[_cacheIndex] = reading;
208
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
Michael Ott's avatar
Michael Ott committed
209
		_latestValue = reading;
210
211
	}

212
213
214
215
	int64_t getCacheOffset(int64_t t) {
		if( t < 0)
			return -1;
		// Converting from milliseconds to nanoseconds
Alessio Netti's avatar
Alessio Netti committed
216
		int64_t offset = ( ( (int64_t)_cacheSize * t ) / ( (int64_t)_cacheInterval * 1000000 ) ) + 1;
217
218
219
220
221
		if(offset > _cacheSize)
			return -1;
		return ( _cacheSize + _cacheIndex - offset ) % _cacheSize;
	}

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	/**
     * Store an unsigned reading, in order to get it pushed to the data base eventually.
     * Also this methods takes care of other optional reading post-processing,
     * e.g. delta computation, subsampling, caching, scaling, etc.
     *
     * This is a variant of the primary storeReading() for monotonically increasing
     * sensors reading unsigned 64bit values which may require more than the 63bit
     * offered by a signed reading_t. The readings are still stored as signed int64
     * in the database, therefore all such sensors should enable storage of deltas!
     *
     * This variant only adapts the delta computation for ureading_t actually.
     * FIXME: Avoid code duplication
     *
     * @param rawReading  Reading struct with (usigned) value and timestamp to be stored.
     * @param factor      Scaling factor, which is applied to the reading value (optional)
     * @param maxValue    Maximum possible value of the reading; required for the
     *                    delta computation to detect an overflow.
     */
	void storeReading(ureading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
241
242
        reading_t reading;
        reading.timestamp = rawReading.timestamp;
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
        if( _delta ) {
            if (!_firstReading) {
              if (rawReading.value < _lastRawUValue.value)
                  reading.value = (rawReading.value + (maxValue - _lastRawUValue.value)) * factor;
              else
                  reading.value = (rawReading.value - _lastRawUValue.value) * factor;
            } else {
              _firstReading = false;
              _lastRawUValue = rawReading;
              return;
            }
            _lastRawUValue = rawReading;
        }
        else
            reading.value = rawReading.value * factor;

        if( _delta )
            // If in delta mode, _accumulator acts as a buffer, summing all deltas for the subsampling period
            _accumulator.value += reading.value;
        else
            _accumulator.value = reading.value;

        if (_subsamplingIndex++ % _subsamplingFactor == 0) {
            _accumulator.timestamp = reading.timestamp;
            //TODO: if sensor starts with values of 0, these won't be pushed. This should be fixed
            if( !(_skipConstVal && (_accumulator.value == _lastSentValue.value)) ) {
                _readingQueue->push(_accumulator);
                _lastSentValue = _accumulator;
            }
            // We reset the accumulator's value for the correct accumulation of deltas
            _accumulator.value = 0;
        }
        if (_sinkFile) {
            try {
                _sinkFile->seekp(0, std::ios::beg);
                *_sinkFile << reading.value << std::endl;
            } catch(const std::exception &e) { _sinkFile->close(); _sinkFile.reset(nullptr); }
        }

        _cache[_cacheIndex] = reading;
        _cacheIndex = (_cacheIndex + 1) % _cacheSize;
        _latestValue = reading;
    }

Alessio Netti's avatar
Alessio Netti committed
287
288
	static std::string formatName(const std::string& name, int cpuID=-1) {return cpuID<0 ? name : "cpu" + std::to_string(cpuID) + "." + name;}

289
290
291
292
protected:

	std::string _name;
	std::string _mqtt;
293
	std::string _sinkPath;
294
	bool _skipConstVal;
295
	unsigned int _cacheInterval;
296
297
	unsigned int _subsamplingFactor;
	unsigned int _subsamplingIndex;
298
	unsigned int _cacheSize;
299
	unsigned int _cacheIndex;
300
	std::unique_ptr<reading_t[]> _cache;
301
	bool _delta;
302
	bool _firstReading;
303
	ureading_t _lastRawUValue;
304
	reading_t _lastRawValue;
305
	reading_t _latestValue;
306
	reading_t _lastSentValue;
Alessio Netti's avatar
Alessio Netti committed
307
	reading_t _accumulator;
308
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
309
	std::unique_ptr<std::ofstream> _sinkFile;
310
311
};

312
313
314
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

315
#endif /* SRC_SENSORBASE_H_ */