SensorBase.h 4.06 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
29
		_cacheInterval(900000),
		_cacheSize(1),
30
		_cacheIndex(0),
31
		_cache(nullptr),
32
		_delta(false),
33
		_readingQueue(nullptr) {
34
35
36

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

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

	virtual ~SensorBase() {}

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

		return *this;
70
71
	}

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

79
80
81
82
	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; }
83
	void	setLastRawValue(int64_t value)					{ _lastRawValue.value = value; }
84
85
86
87
88

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

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

102
	virtual void storeReading(reading_t reading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
103
		_latestValue.timestamp = reading.timestamp;
104
105
		if( _delta ) {
			if (reading.value < _lastRawValue.value)
106
				_latestValue.value = (reading.value + (maxValue - _lastRawValue.value)) * factor;
107
			else
108
				_latestValue.value = (reading.value - _lastRawValue.value) * factor;
109
110
111
			_lastRawValue.value = reading.value;
		}
		else
112
			_latestValue.value = reading.value * factor;
113
114
115

		_readingQueue->push(_latestValue);
		_cache[_cacheIndex] = _latestValue;
116
		_cacheIndex = (_cacheIndex + 1) % _cacheSize;
117
118
	}

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

121
122
123
124
protected:

	std::string _name;
	std::string _mqtt;
125
126
	unsigned int _cacheInterval;
	unsigned int _cacheSize;
127
128
	unsigned int _cacheIndex;
	std::unique_ptr<reading_t[]> _cache;
129
	bool _delta;
130
	reading_t _latestValue;
131
	reading_t _lastRawValue;
132
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
133
134
};

135
136
137
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

138
#endif /* SRC_SENSORBASE_H_ */