SensorBase.h 2.83 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
13
14
15
16
17
18
19
20
21
#include <string>
#include <boost/lockfree/spsc_queue.hpp>

typedef struct {
	uint64_t value;
	uint64_t timestamp;
} reading_t;

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

24
25
	SensorBase(const std::string& name) :
		_name(name),
26
		_mqtt(""),
27
		_cacheIndex(0),
28
29
		_cache(nullptr),
		_readingQueue(nullptr) {
30
31
32
33
34

		_latestValue.timestamp	= 0;
		_latestValue.value		= 0;
	}

35
36
37
	SensorBase(const SensorBase& other) :
		_name(other._name),
		_mqtt(other._mqtt),
38
		_cacheIndex(0),
39
40
41
42
43
44
45
46
47
		_cache(nullptr),
		_latestValue(other._latestValue),
		_readingQueue(nullptr) {}

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
48
		_cacheIndex = 0;
49
50
51
52
53
54
		_cache.reset(nullptr);
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
		_readingQueue.reset(nullptr);

		return *this;
55
56
57
58
	}

	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
59
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
lu43jih's avatar
lu43jih committed
60
	const reading_t			getLatestValue()	const	{ return _latestValue; } /*TODO return reference*/
61

Alessio Netti's avatar
Alessio Netti committed
62
	void				setName(const std::string& name, int cpuID=-1)	{ _name = formatName(name, cpuID); }
63
64
65
66
67
68
69
70
	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) {
71
			_cache.reset(new reading_t[cacheSize]);
72
73
74
75
76
			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
77
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(QUEUE_MAXLIMIT));
78
79
80
		}
	}

81
	virtual void storeReading(reading_t reading, unsigned int cacheSize) {
82
		_readingQueue->push(reading);
83
84
		_cache[_cacheIndex] = reading;
		_cacheIndex = (_cacheIndex + 1) % cacheSize;
85
86
87
88
		_latestValue.value = reading.value;
		_latestValue.timestamp = reading.timestamp;
	}

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

91
92
93
94
protected:

	std::string _name;
	std::string _mqtt;
95
96
	unsigned int _cacheIndex;
	std::unique_ptr<reading_t[]> _cache;
97
	reading_t _latestValue;
98
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
99
100
};

101
102
103
//for better readability
using SBasePtr = std::shared_ptr<SensorBase>;

104
#endif /* SRC_SENSORBASE_H_ */