SensorBase.h 5.73 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
39
		_readingQueue(nullptr),
		_sinkFile(nullptr) {
40
41
42

		_latestValue.timestamp	= 0;
		_latestValue.value		= 0;
43
44
        _lastSentValue.timestamp= 0;
        _lastSentValue.value	= 0;
45
46
		_lastRawValue.timestamp = 0;
		_lastRawValue.value		= 0;
47
48
	}

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

	virtual ~SensorBase() {}

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

		return *this;
91
92
	}

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

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

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

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

Michael Ott's avatar
Michael Ott committed
133
134
	void storeReading(reading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
		reading_t reading = rawReading;
135
		if( _delta ) {
Michael Ott's avatar
Michael Ott committed
136
137
			if (rawReading.value < _lastRawValue.value)
				reading.value = (rawReading.value + (maxValue - _lastRawValue.value)) * factor;
138
			else
Michael Ott's avatar
Michael Ott committed
139
140
				reading.value = (rawReading.value - _lastRawValue.value) * factor;
			_lastRawValue = rawReading;
141
142
		}
		else
Michael Ott's avatar
Michael Ott committed
143
			reading.value = rawReading.value * factor;
144

145
146
		//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) {
147
			_readingQueue->push(reading);
148
149
150
151
152
			_lastSentValue = reading;
		}
		if (_sinkFile) {
			_sinkFile->seekp(0, std::ios::beg);
			*_sinkFile << reading.value << std::endl;
153
		}
154

Michael Ott's avatar
Michael Ott committed
155
		_cache[_cacheIndex] = reading;
156
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
Michael Ott's avatar
Michael Ott committed
157
		_latestValue = reading;
158
159
	}

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

162
163
164
165
protected:

	std::string _name;
	std::string _mqtt;
166
	std::string _sinkPath;
167
	bool _skipConstVal;
168
	unsigned int _cacheInterval;
169
170
	unsigned int _subsamplingFactor;
	unsigned int _subsamplingIndex;
171
	unsigned int _cacheSize;
172
	unsigned int _cacheIndex;
173
	std::unique_ptr<reading_t[]> _cache;
174
	bool _delta;
175
	reading_t _latestValue;
176
	reading_t _lastRawValue;
177
	reading_t _lastSentValue;
178
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
179
	std::unique_ptr<std::ofstream> _sinkFile;
180
181
};

182
183
184
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

185
#endif /* SRC_SENSORBASE_H_ */