Fledge
An open source edge computing platform for industrial users
reading_set.h
1 #ifndef _READINGSET_H
2 #define _READINGSET_H
3 /*
4  * Fledge storage client.
5  *
6  * Copyright (c) 2018 Dianomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch, Massimiliano Pinto
11  */
12 #include <string>
13 #include <string.h>
14 #include <sstream>
15 #include <iostream>
16 #include <reading.h>
17 #include <rapidjson/document.h>
18 #include <vector>
19 
26 class ReadingSet {
27  public:
28  ReadingSet();
29  ReadingSet(const std::string& json);
30  ReadingSet(const std::vector<Reading *>* readings);
31  virtual ~ReadingSet();
32 
33  unsigned long getCount() const { return m_readings.size(); };
34  const Reading *operator[] (const unsigned int idx) {
35  return m_readings[idx];
36  };
37 
38  // Return the const reference of readings data
39  const std::vector<Reading *>& getAllReadings() const { return m_readings; };
40  // Return the reference of readings
41  std::vector<Reading *>* getAllReadingsPtr() { return &m_readings; };
42 
43  // Remove readings from reading set and return reference to readings
44  std::vector<Reading *>* moveAllReadings();
45  // Delete a reading from reading set and return pointer of deleted reading
46  Reading* removeReading(unsigned long id);
47 
48  // Return the reading id of the last data element
49  unsigned long getLastId() const { return m_last_id; };
50  unsigned long getReadingId(uint32_t pos);
51  void append(ReadingSet *);
52  void append(ReadingSet&);
53  void append(std::vector<Reading *> &);
54  void removeAll();
55  void clear();
56  bool copy(const ReadingSet& src);
57 
58  protected:
59  unsigned long m_count;
60  ReadingSet(const ReadingSet&);
61  ReadingSet& operator=(ReadingSet const &);
62  std::vector<Reading *> m_readings;
63  // Id of last Reading element
64  unsigned long m_last_id; // Id of the last Reading
65 };
66 
72 class JSONReading : public Reading {
73  public:
74  JSONReading(const rapidjson::Value& json);
75  ~JSONReading() {};
76 
77  // Return the reading id
78  unsigned long getId() const { return m_id; };
79 
80  private:
81  Datapoint *datapoint(const std::string& name, const rapidjson::Value& json);
82  void escapeCharacter(std::string& stringToEvaluate, std::string pattern);
83 };
84 
85 class ReadingSetException : public std::exception
86 {
87  public:
88  ReadingSetException(const char *what)
89  {
90  m_what = strdup(what);
91  };
93  {
94  if (m_what)
95  free(m_what);
96  };
97  virtual const char *what() const throw()
98  {
99  return m_what;
100  };
101  private:
102  char *m_what;
103 };
104 #endif
105 
virtual ~ReadingSet()
Destructor for a result set.
Definition: reading_set.cpp:155
Reading * removeReading(unsigned long id)
Remove reading from vector based on index and return its pointer.
Definition: reading_set.cpp:349
void append(ReadingSet *)
Append the readings in a second reading set to this reading set.
Definition: reading_set.cpp:171
Name and value pair used to represent a data value within an asset reading.
Definition: datapoint.h:310
void removeAll()
Remove all readings from the reading set and delete the memory After this call the reading set exists...
Definition: reading_set.cpp:310
An asset reading represented as a class.
Definition: reading.h:33
Reading set class.
Definition: reading_set.h:26
bool copy(const ReadingSet &src)
Deep copy a set of readings to this reading set.
Definition: reading_set.cpp:223
std::vector< Reading * > * moveAllReadings()
Remove readings from the vector and return a reference to new vector containing readings*.
Definition: reading_set.cpp:336
void clear()
Remove the readings from the vector without deleting them.
Definition: reading_set.cpp:325
ReadingSet()
Construct an empty reading set.
Definition: reading_set.cpp:40
Definition: reading_set.h:85
JSONReading class.
Definition: reading_set.h:72
unsigned long getReadingId(uint32_t pos)
Return the ID of the nth reading in the reading set.
Definition: reading_set.cpp:368