SensorBase.h 6.78 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
22
23
	uint64_t timestamp;
} reading_t;

class SensorBase {
public:
lu43jih's avatar
lu43jih committed
24
25
	static const size_t QUEUE_MAXLIMIT=1024;

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

		_latestValue.timestamp	= 0;
		_latestValue.value		= 0;
44
45
        _lastSentValue.timestamp= 0;
        _lastSentValue.value	= 0;
46
47
		_lastRawValue.timestamp = 0;
		_lastRawValue.value		= 0;
Alessio Netti's avatar
Alessio Netti committed
48
49
        _accumulator.timestamp  = 0;
        _accumulator.value		= 0;
50
51
	}

52
53
54
	SensorBase(const SensorBase& other) :
		_name(other._name),
		_mqtt(other._mqtt),
55
		_skipConstVal(other._skipConstVal),
56
		_cacheInterval(other._cacheInterval),
57
58
		_subsamplingFactor(other._subsamplingFactor),
		_subsamplingIndex(0),
59
		_cacheSize(other._cacheSize),
60
		_cacheIndex(0),
61
		_cache(nullptr),
62
		_delta(other._delta),
63
		_firstReading(true),
64
		_latestValue(other._latestValue),
65
		_lastRawValue(other._lastRawValue),
66
        _lastSentValue(other._lastSentValue),
Alessio Netti's avatar
Alessio Netti committed
67
        _accumulator(other._accumulator),
68
69
		_readingQueue(nullptr),
		_sinkFile(nullptr) {}
70
71
72
73
74
75

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
76
		_skipConstVal = other._skipConstVal;
77
		_cacheInterval = other._cacheInterval;
78
79
		_subsamplingFactor = other._subsamplingFactor;
		_subsamplingIndex = 0;
80
		_cacheSize = other._cacheSize;
81
		_cacheIndex = 0;
82
		_cache.reset(nullptr);
83
		_delta = other._delta;
84
		_firstReading = true;
85
86
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
87
88
		_lastRawValue.timestamp = other._lastRawValue.timestamp;
		_lastRawValue.value		= other._lastRawValue.value;
89
90
        _lastSentValue.timestamp= other._lastSentValue.timestamp;
        _lastSentValue.value	= other._lastSentValue.value;
Alessio Netti's avatar
Alessio Netti committed
91
92
        _accumulator.timestamp  = other._accumulator.timestamp;
        _accumulator.value      = other._accumulator.value;
93
		_readingQueue.reset(nullptr);
94
		_sinkFile.reset(nullptr);
95
96

		return *this;
97
98
	}

99
	const bool 				isDelta()			const 	{ return _delta;}
100
101
	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
102
	const std::string&		getSinkPath() 		const	{ return _sinkPath; }
103
	bool					getSkipConstVal()	const	{ return _skipConstVal; }
104
	unsigned				getCacheSize()		const	{ return _cacheSize; }
105
	unsigned 				getSubsampling()	const   { return _subsamplingFactor; }
106
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
Micha Mueller's avatar
Micha Mueller committed
107
	const reading_t&		getLatestValue()	const	{ return _latestValue; }
108

109
	void	setSkipConstVal(bool skipConstVal)				{ _skipConstVal = skipConstVal;	}
110
111
112
	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; }
113
	void 	setSinkPath(const std::string& path)			{ _sinkPath = path; }
114
	void 	setCacheInterval(unsigned cacheInterval)		{ _cacheInterval = cacheInterval; }
115
	void	setSubsampling(unsigned factor)					{ _subsamplingFactor = factor; }
116
117
118
119
120

	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); }
	void				pushReadingQueue(reading_t *reads, std::size_t count) const	{ _readingQueue->push(reads, count); }

121
122
	void initSensor(unsigned interval) {
		_cacheSize = _cacheInterval / interval + 1;
123
		if(!_cache) {
124
125
			_cache.reset(new reading_t[_cacheSize]);
			for(unsigned i = 0; i < _cacheSize; i++) {
126
127
128
129
				_cache[i] = _latestValue;	//_latestValue should equal (0,0) at this point
			}
		}
		if(!_readingQueue) {
lu43jih's avatar
lu43jih committed
130
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(QUEUE_MAXLIMIT));
131
		}
132
133
134
135
136
		if(!_sinkFile && _sinkPath != "") {
			_sinkFile.reset(new std::ofstream(_sinkPath));
			if(!_sinkFile->is_open())
				_sinkFile.reset(nullptr);
		}
137
138
	}

Michael Ott's avatar
Michael Ott committed
139
140
	void storeReading(reading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
		reading_t reading = rawReading;
141
		if( _delta ) {
142
143
144
145
146
147
148
149
150
151
		    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
152
			_lastRawValue = rawReading;
153
154
		}
		else
Michael Ott's avatar
Michael Ott committed
155
			reading.value = rawReading.value * factor;
156

Alessio Netti's avatar
Alessio Netti committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		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;
172
173
		}
		if (_sinkFile) {
Alessio Netti's avatar
Alessio Netti committed
174
175
176
177
		    try {
                _sinkFile->seekp(0, std::ios::beg);
                *_sinkFile << reading.value << std::endl;
            } catch(const std::exception &e) { _sinkFile->close(); _sinkFile.reset(nullptr); }
178
		}
179

Michael Ott's avatar
Michael Ott committed
180
		_cache[_cacheIndex] = reading;
181
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
Michael Ott's avatar
Michael Ott committed
182
		_latestValue = reading;
183
184
	}

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

187
188
189
190
protected:

	std::string _name;
	std::string _mqtt;
191
	std::string _sinkPath;
192
	bool _skipConstVal;
193
	unsigned int _cacheInterval;
194
195
	unsigned int _subsamplingFactor;
	unsigned int _subsamplingIndex;
196
	unsigned int _cacheSize;
197
	unsigned int _cacheIndex;
198
	std::unique_ptr<reading_t[]> _cache;
199
	bool _delta;
200
	bool _firstReading;
201
	reading_t _latestValue;
202
	reading_t _lastRawValue;
203
	reading_t _lastSentValue;
Alessio Netti's avatar
Alessio Netti committed
204
	reading_t _accumulator;
205
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
206
	std::unique_ptr<std::ofstream> _sinkFile;
207
208
};

209
210
211
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

212
#endif /* SRC_SENSORBASE_H_ */