SensorBase.h 6 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;
48
49
	}

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

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
74
		_sinkPath = other._sinkPath;
75
		_skipConstVal = other._skipConstVal;
76
		_cacheInterval = other._cacheInterval;
77
78
		_subsamplingFactor = other._subsamplingFactor;
		_subsamplingIndex = 0;
79
		_cacheSize = other._cacheSize;
80
		_cacheIndex = 0;
81
		_cache.reset(nullptr);
82
		_delta = other._delta;
83
		_firstReading = true;
84
85
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
86
87
		_lastRawValue.timestamp = other._lastRawValue.timestamp;
		_lastRawValue.value		= other._lastRawValue.value;
88
89
        _lastSentValue.timestamp= other._lastSentValue.timestamp;
        _lastSentValue.value	= other._lastSentValue.value;
90
		_readingQueue.reset(nullptr);
91
		_sinkFile.reset(nullptr);
92
93

		return *this;
94
95
	}

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

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

	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); }

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

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

154
155
		//TODO: if sensor starts with values of 0, these won't be pushed. This should be fixed
		if (!(_skipConstVal && (reading.value == _lastSentValue.value)) && _subsamplingIndex++ % _subsamplingFactor == 0) {
156
			_readingQueue->push(reading);
157
158
159
160
161
			_lastSentValue = reading;
		}
		if (_sinkFile) {
			_sinkFile->seekp(0, std::ios::beg);
			*_sinkFile << reading.value << std::endl;
162
		}
163

Michael Ott's avatar
Michael Ott committed
164
		_cache[_cacheIndex] = reading;
165
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
Michael Ott's avatar
Michael Ott committed
166
		_latestValue = reading;
167
168
	}

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

171
172
173
174
protected:

	std::string _name;
	std::string _mqtt;
175
	std::string _sinkPath;
176
	bool _skipConstVal;
177
	unsigned int _cacheInterval;
178
179
	unsigned int _subsamplingFactor;
	unsigned int _subsamplingIndex;
180
	unsigned int _cacheSize;
181
	unsigned int _cacheIndex;
182
	std::unique_ptr<reading_t[]> _cache;
183
	bool _delta;
184
	bool _firstReading;
185
	reading_t _latestValue;
186
	reading_t _lastRawValue;
187
	reading_t _lastSentValue;
188
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
189
	std::unique_ptr<std::ofstream> _sinkFile;
190
191
};

192
193
194
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

195
#endif /* SRC_SENSORBASE_H_ */