SensorGroupInterface.h 3.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * SensorGroupInterface.h
 *
 *  Created on: 10.08.2018
 *      Author: Micha Mueller
 */

#ifndef SENSORGROUPINTERFACE_H_
#define SENSORGROUPINTERFACE_H_

11
12
13
14
#include <atomic>
#include <memory>
#include <boost/asio.hpp>

15
16
#include "logging.h"
#include "sensorbase.h"
17

18
class SensorGroupInterface {
19
public:
20

21
	SensorGroupInterface(const std::string& groupName) :
22
		_groupName(groupName),
23
		_mqttPart(""),
24
		_sync(true),
25
26
27
28
29
30
31
32
		_keepRunning(0),
		_minValues(1),
		_interval(1000),
		_pendingTasks(0),
		_timer(nullptr) {}

	SensorGroupInterface(const SensorGroupInterface& other) :
		_groupName(other._groupName),
33
		_mqttPart(other._mqttPart),
34
		_sync(other._sync),
35
36
37
38
39
40
		_keepRunning(other._keepRunning),
		_minValues(other._minValues),
		_interval(other._interval),
		_timer(nullptr) {
		_pendingTasks.store(other._pendingTasks.load());
	}
41
42
43

	virtual ~SensorGroupInterface() {}

44
45
	SensorGroupInterface& operator=(const SensorGroupInterface& other) {
		_groupName = other._groupName;
46
		_mqttPart = other._mqttPart;
47
		_sync = other._sync;
48
49
50
51
52
53
54
55
56
		_keepRunning = other._keepRunning;
		_minValues = other._minValues;
		_interval = other._interval;
		_pendingTasks.store(other._pendingTasks.load());
		_timer = nullptr;

		return *this;
	}

57
	const std::string& 	getGroupName()	const	{ return _groupName; }
58
	const std::string& 	getMqttPart()	const	{ return _mqttPart; }
59
	bool				getSync()		const	{ return _sync; }
60
61
62
63
	unsigned			getMinValues()	const	{ return _minValues; }
	unsigned			getInterval()	const	{ return _interval; }

	void setGroupName(const std::string& groupName)	{ _groupName = groupName; }
64
	void setMqttPart(const std::string& mqttPart)	{ _mqttPart = mqttPart; }
65
	void setSync(bool sync)							{ _sync = sync; }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	void setMinValues(unsigned minValues)			{ _minValues = minValues; }
	void setInterval(unsigned interval)				{ _interval = interval; }

	/**
	 * Does a busy wait until all dispatched handlers are finished (_pendingTasks == 0)
	 */
	void wait() {
		while(_pendingTasks) {
			sleep(1);
		}
	}

	//can be overwritten
	virtual void init(boost::asio::io_service& io) {
		_timer.reset(new boost::asio::deadline_timer(io, boost::posix_time::seconds(0)));
	}

83
	virtual void printConfig(LOG_LEVEL ll) {
Micha Mueller's avatar
Micha Mueller committed
84
	  LOG_VAR(ll) << "  Sensor Group: " << _groupName;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	  if (_mqttPart != "") {
	    LOG_VAR(ll) << "   MQTT part: " << _mqttPart;
	  }

	  if (_sync) {
	    LOG_VAR(ll) << "   Synchronized readings enabled";
	  } else {
	    LOG_VAR(ll) << "   Synchronized readings disabled";
	  }

	  LOG_VAR(ll) << "   minValues: " << _minValues;
	  LOG_VAR(ll) << "   interval:  " << _interval;
	}

99
100
101
	//have to be overwritten
	virtual void start() = 0;
	virtual void stop() = 0;
102

103
104
	virtual void pushBackSensor(SBasePtr s)		= 0;
	virtual std::vector<SBasePtr>& getSensors()	= 0;
105
106

protected:
107
108
109
	virtual void read() = 0;
	virtual void readAsync() = 0;

110
	std::string _groupName;
111
	std::string	_mqttPart;
112
	bool _sync;
113
114
115
116
117
	int _keepRunning;
	unsigned int _minValues;
	unsigned int _interval;
	std::atomic_uint _pendingTasks;
	std::unique_ptr<boost::asio::deadline_timer> _timer;
118
	LOGGER lg;
119
120
};

121
122
123
//for better readability
using SGroupPtr = std::shared_ptr<SensorGroupInterface>;

124
#endif /* SENSORGROUPINTERFACE_H_ */