SensorBase.h 4.29 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 <memory>
12
#include <string>
13
#include <limits.h>
14
15
16
#include <boost/lockfree/spsc_queue.hpp>

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

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

25
26
	SensorBase(const std::string& name) :
		_name(name),
27
		_mqtt(""),
28
		_skipConstVal(false),
29
30
		_cacheInterval(900000),
		_cacheSize(1),
31
		_cacheIndex(0),
32
		_cache(nullptr),
33
		_delta(false),
34
		_readingQueue(nullptr) {
35
36
37

		_latestValue.timestamp	= 0;
		_latestValue.value		= 0;
38
39
		_lastRawValue.timestamp = 0;
		_lastRawValue.value		= 0;
40
41
	}

42
43
44
	SensorBase(const SensorBase& other) :
		_name(other._name),
		_mqtt(other._mqtt),
45
		_skipConstVal(other._skipConstVal),
46
47
		_cacheInterval(other._cacheInterval),
		_cacheSize(other._cacheSize),
48
		_cacheIndex(0),
49
		_cache(nullptr),
50
		_delta(other._delta),
51
		_latestValue(other._latestValue),
52
		_lastRawValue(other._lastRawValue),
53
54
55
56
57
58
59
		_readingQueue(nullptr) {}

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
60
		_skipConstVal = other._skipConstVal;
61
62
		_cacheInterval = other._cacheInterval;
		_cacheSize = other._cacheSize;
63
		_cacheIndex = 0;
64
		_cache.reset(nullptr);
65
		_delta = other._delta;
66
67
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
68
69
		_lastRawValue.timestamp = other._lastRawValue.timestamp;
		_lastRawValue.value		= other._lastRawValue.value;
70
71
72
		_readingQueue.reset(nullptr);

		return *this;
73
74
	}

75
	const bool 				isDelta()			const 	{ return _delta;}
76
77
	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
78
	bool					getSkipConstVal()	const	{ return _skipConstVal; }
79
	unsigned				getCacheSize()		const	{ return _cacheSize; }
80
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
Micha Mueller's avatar
Micha Mueller committed
81
	const reading_t&		getLatestValue()	const	{ return _latestValue; }
82

83
	void	setSkipConstVal(bool skipConstVal)	{ _skipConstVal = skipConstVal;	}
84
85
86
87
	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; }
	void 	setCacheInterval(unsigned cacheInterval)		{ _cacheInterval = cacheInterval; }
88
89
90
91
92

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

93
94
	void initSensor(unsigned interval) {
		_cacheSize = _cacheInterval / interval + 1;
95
		if(!_cache) {
96
97
			_cache.reset(new reading_t[_cacheSize]);
			for(unsigned i = 0; i < _cacheSize; i++) {
98
99
100
101
				_cache[i] = _latestValue;	//_latestValue should equal (0,0) at this point
			}
		}
		if(!_readingQueue) {
lu43jih's avatar
lu43jih committed
102
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(QUEUE_MAXLIMIT));
103
104
105
		}
	}

Michael Ott's avatar
Michael Ott committed
106
107
	void storeReading(reading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
		reading_t reading = rawReading;
108
		if( _delta ) {
Michael Ott's avatar
Michael Ott committed
109
110
			if (rawReading.value < _lastRawValue.value)
				reading.value = (rawReading.value + (maxValue - _lastRawValue.value)) * factor;
111
			else
Michael Ott's avatar
Michael Ott committed
112
113
				reading.value = (rawReading.value - _lastRawValue.value) * factor;
			_lastRawValue = rawReading;
114
115
		}
		else
Michael Ott's avatar
Michael Ott committed
116
			reading.value = rawReading.value * factor;
117

118
119
120
		if (!(_skipConstVal && (reading.value == _latestValue.value))) {
			_readingQueue->push(reading);
		}
Michael Ott's avatar
Michael Ott committed
121
		_cache[_cacheIndex] = reading;
122
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
Michael Ott's avatar
Michael Ott committed
123
		_latestValue = reading;
124
125
	}

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

128
129
130
131
protected:

	std::string _name;
	std::string _mqtt;
132
	bool _skipConstVal;
133
134
	unsigned int _cacheInterval;
	unsigned int _cacheSize;
135
	unsigned int _cacheIndex;
136
	std::unique_ptr<reading_t[]> _cache;
137
	bool _delta;
138
	reading_t _latestValue;
139
	reading_t _lastRawValue;
140
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
141
142
};

143
144
145
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

146
#endif /* SRC_SENSORBASE_H_ */