Sensor.h 1.92 KB
Newer Older
1
2
3
4
/*
 * Sensor.h
 *
 *  Created on: 12.01.2018
Micha Mueller's avatar
Micha Mueller committed
5
 *  Author: Micha Mueller
6
7
8
9
10
11
12
13
14
 */

#ifndef SENSOR_H_
#define SENSOR_H_

#include <string>
#include <boost/asio.hpp>
#include <boost/lockfree/spsc_queue.hpp>

15
#include "Logging.h"
16

17
18
19
20
typedef struct {
	uint64_t value;
	uint64_t timestamp;
} reading_t;
Micha Mueller's avatar
Micha Mueller committed
21
22
23
24
25
/**
 *  Abstract base class which defines the interface for concrete sensors.
 *  A concrete (derived) sensor must at least implement the read()-,
 *  readAsync()- and startPolling()-methods.
 */
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Sensor {

public:
	Sensor(const std::string name);

	virtual ~Sensor();

	//non-overwriteable methods
	const std::string& 	getName() const;
	void				setName(const std::string& name);

	const std::string&	getMqtt() const;
	void				setMqtt(const std::string& mqtt);

	unsigned			getMinValues() const;
	void				setMinValues(unsigned minValues);

	unsigned			getInterval() const;
	void				setInterval(unsigned interval);

46
47
48
49
	unsigned			getCacheSize() const;

	void				setCacheInterval(unsigned cacheInterval);

50
51
	const reading_t * const	getCache() const;

52
53
54
55
	const std::size_t	getSizeOfReadingQueue() const;
	std::size_t 		popReadingQueue(reading_t *reads, std::size_t max) const;
	void				pushReadingQueue(reading_t *reads, std::size_t count) const;

56
57
58
	//can be overwritten methods
	virtual void init(boost::asio::io_service& io);

59
60
	//have to be overwritten methods
	virtual void read() = 0;
61
	virtual void readAsync() = 0;
62
63
	virtual void startPolling() = 0;
	virtual void stopPolling() = 0;
64
65

protected:
66
67
	void	storeReading(reading_t reading);

68
69
	std::string _name;
	std::string _mqtt;
70
	int _keepRunning;
71
72
	unsigned int _minValues;
	unsigned int _interval;
73
74
	unsigned int _cacheInterval;
	unsigned int _cacheSize;
75
	unsigned int _cacheIndex;
76
	reading_t * _cache;
77
78
79
	reading_t _latestValue;
	boost::asio::deadline_timer* _timer;
	boost::lockfree::spsc_queue<reading_t>* _readingQueue;
80
	boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
81
82
83
};

#endif /* SENSOR_H_ */