2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

sensorcache.h 3.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * SensorCache.h
 *
 *  Created on: 3 Aug 2016
 *      Author: ottmi
 */

#ifndef COLLECTAGENT_SENSORCACHE_H_
#define COLLECTAGENT_SENSORCACHE_H_

#include <map>
#include <dcdb/sensorid.h>
13
14
#include <dcdb/timestamp.h>
#include "cacheentry.h"
15

16
using namespace DCDB;
17

18
typedef std::map<SensorId, CacheEntry> sensorCache_t;
19
20
21

class SensorCache {
public:
22
	SensorCache(uint64_t maxHistory=60000000000);
23
	virtual ~SensorCache();
24
25

	/**
26
27
28
29
30
31
32
33
34
35
36
37
	* @brief        Returns a constant reference to the internal sensor cache map.
	**/
	const sensorCache_t& getSensorMap();

	/**
	* @brief        Store a sensor reading in the SensorCache.
	*
	* @param sid    The SensorId of the sensor to be cached.
    * @param ts     The timestamp of the sensor reading.
    * @param val    The actual sensor reading.
	* @return       Returns true if the topic string was valid and the data field of the object was populated.
	**/
38
	void storeSensor(SensorId sid, uint64_t ts, int64_t val);
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	/**
    * @brief        Return a sensor reading or the average of the last readings
    *               from the SensorCache.
    *
    * @param sid    The SensorId of the sensor to be looked up in the cache.
    * @param avg    If avg > 0: denotes the length of the average aggregation window in nanoseconds.
    * @return       If avg == 0 :The sensor reading of the corresponding cache entry.
    *               If avg > 0 the average of the last readings is returned.
    * @throws       std::invalid_argument if the SensorId doesn't exist in the SensorCache.
    * @throws       std::out_of_range if the sid was found in the cache entry but is outdated.
    **/
	int64_t getSensor(SensorId sid, uint64_t avg=0);

	/**
	* @brief        Return a sensor reading or the average of the last readings
    *               from the SensorCache.
	*
	* @param topic  The topic of the sensor to be looked up in the cache. May contain wildcards.
	* @param avg    If avg > 0: denotes the length of the average aggregation window in nanoseconds.
	* @return       If avg == 0 :The sensor reading of the corresponding cache entry.
    *               If avg > 0 the average of the last readings is returned.
	* @throws       std::invalid_argument if the topic couldn't be found in the SensorCache.
	* @throws       std::out_of_range if the topic was found in the cache entry but is outdated.
	**/
	int64_t getSensor(std::string topic, uint64_t avg=0);

	/**
    * @brief        Dump the contents of the SensorCache to stdout.
    **/
69
70
	void dump();

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	/**
	* @brief        Removes all obsolete entries from the cache
	*
	*				All entries in the cache whose latest sensor reading is older than "now" - t nanoseconds are
	*				removed.
	*
	* @param t		The threshold in nanoseconds for entries that must be removed
	* @return		The number of purged cache entries
	**/
	uint64_t clean(uint64_t t);

    /**
    * @brief  Set a new maximum cache length.
    *
    * @param maxHistory: new sensor cache length value.
    **/
	void setMaxHistory(uint64_t maxHistory) { this->_maxHistory = maxHistory; }

    /**
    * @brief  Returns the current maximum sensor cache length
    *
    * @return Current maximum sensor cache length
    */
	uint64_t getMaxHistory() { return this->_maxHistory; }

96
private:
97
	// Map containing the single sensor caches
98
	sensorCache_t sensorCache;
99
100
	// Global maximum allowed time frame for the sensor caches
	uint64_t _maxHistory;
101
102
103
104

};

#endif /* COLLECTAGENT_SENSORCACHE_H_ */