SensorBase.h 2.86 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
22
23
#include <string>
#include <boost/lockfree/spsc_queue.hpp>

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

class SensorBase {
public:
	SensorBase(const std::string& name) :
		_name(name),
24
		_mqtt(""),
25
		_skipConstVal(false),
26
27
		_cache(nullptr),
		_readingQueue(nullptr) {
28
29
30
31
32

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

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

	virtual ~SensorBase() {}

	SensorBase& operator=(const SensorBase& other) {
		_name = other._name;
		_mqtt = other._mqtt;
46
		_skipConstVal = other._skipConstVal;
47
48
49
50
51
52
		_cache.reset(nullptr);
		_latestValue.timestamp	= other._latestValue.timestamp;
		_latestValue.value		= other._latestValue.value;
		_readingQueue.reset(nullptr);

		return *this;
53
54
55
56
	}

	const std::string& 		getName() 			const	{ return _name; }
	const std::string&		getMqtt() 			const	{ return _mqtt; }
57
	bool					getSkipConstVal()	const	{ return _skipConstVal; }
58
	const reading_t * const	getCache() 			const	{ return _cache.get(); }
59
60
	const reading_t			getLatestValue()	const	{ return _latestValue; }

Alessio Netti's avatar
Alessio Netti committed
61
	void 	setName(const std::string& name, int cpuID=-1)	{ _name = formatName(name, cpuID); }
62
	void	setMqtt(const std::string& mqtt)	{ _mqtt = mqtt; }
63
	void	setSkipConstVal(bool skipConstVal)	{ _skipConstVal = skipConstVal;	}
64
65
66
67
68
69
70

	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) {
77
			_readingQueue.reset(new boost::lockfree::spsc_queue<reading_t>(1024));
78
79
80
81
		}
	}

	virtual void storeReading(reading_t reading, unsigned cacheIndex) {
82
83
84
		if (!(_skipConstVal && (reading.value == _latestValue.value))) {
			_readingQueue->push(reading);
		}
85
86
87
88
89
		_cache[cacheIndex] = reading;
		_latestValue.value = reading.value;
		_latestValue.timestamp = reading.timestamp;
	}

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

92
93
94
95
protected:

	std::string _name;
	std::string _mqtt;
96
	bool _skipConstVal;
97
	std::unique_ptr<reading_t[]> _cache;
98
	reading_t _latestValue;
99
	std::unique_ptr<boost::lockfree::spsc_queue<reading_t>> _readingQueue;
100
101
102
};

#endif /* SRC_SENSORBASE_H_ */