SensorBase.h 3.56 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
17
18
19
20
21
22
#include <boost/lockfree/spsc_queue.hpp>

typedef struct {
	uint64_t value;
	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
		_cacheIndex(0),
29
		_cache(nullptr),
30
		_delta(false),
31
		_readingQueue(nullptr) {
32
33
34

		_latestValue.timestamp	= 0;
		_latestValue.value		= 0;
35
36
		_lastRawValue.timestamp = 0;
		_lastRawValue.value		= 0;
37
38
	}

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

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
54
		_cacheIndex = 0;
55
		_cache.reset(nullptr);
56
		_delta = other._delta;
57
58
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
59
60
		_lastRawValue.timestamp = other._lastRawValue.timestamp;
		_lastRawValue.value		= other._lastRawValue.value;
61
62
63
		_readingQueue.reset(nullptr);

		return *this;
64
65
	}

66
	const bool 				isDelta()			const 	{ return _delta;}
67
68
	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
69
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
lu43jih's avatar
lu43jih committed
70
	const reading_t			getLatestValue()	const	{ return _latestValue; } /*TODO return reference*/
71

72
	void				setDelta(const bool delta) { _delta = delta; }
Alessio Netti's avatar
Alessio Netti committed
73
	void				setName(const std::string& name, int cpuID=-1)	{ _name = formatName(name, cpuID); }
74
75
76
77
78
79
80
81
	void				setMqtt(const std::string& mqtt)	{ _mqtt = mqtt; }

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

	void initSensor(unsigned cacheSize) {
		if(!_cache) {
82
			_cache.reset(new reading_t[cacheSize]);
83
84
85
86
87
			for(unsigned i = 0; i < cacheSize; i++) {
				_cache[i] = _latestValue;	//_latestValue should equal (0,0) at this point
			}
		}
		if(!_readingQueue) {
lu43jih's avatar
lu43jih committed
88
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(QUEUE_MAXLIMIT));
89
90
91
		}
	}

92
	virtual void storeReading(reading_t reading, unsigned int cacheSize, unsigned long long maxValue=ULLONG_MAX) {
93
		_latestValue.timestamp = reading.timestamp;
94
95
96
97
98
99
100
101
102
103
104
105
106
		if( _delta ) {
			if (reading.value < _lastRawValue.value)
				_latestValue.value = reading.value + (maxValue - _lastRawValue.value);
			else
				_latestValue.value = reading.value - _lastRawValue.value;
			_lastRawValue.value = reading.value;
		}
		else
			_latestValue.value = reading.value;

		_readingQueue->push(_latestValue);
		_cache[_cacheIndex] = _latestValue;
		_cacheIndex = (_cacheIndex + 1) % cacheSize;
107
108
	}

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

111
112
113
114
protected:

	std::string _name;
	std::string _mqtt;
115
116
	unsigned int _cacheIndex;
	std::unique_ptr<reading_t[]> _cache;
117
	bool _delta;
118
	reading_t _latestValue;
119
	reading_t _lastRawValue;
120
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
121
122
};

123
124
125
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

126
#endif /* SRC_SENSORBASE_H_ */