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 merge(std::vector<Reading *> *readings);
55  void removeAll();
56  void clear();
57  bool copy(const ReadingSet& src);
58 
59  protected:
60  unsigned long m_count;
61  ReadingSet(const ReadingSet&);
62  ReadingSet& operator=(ReadingSet const &);
63  std::vector<Reading *> m_readings;
64  // Id of last Reading element
65  unsigned long m_last_id; // Id of the last Reading
66 };
67 
73 class JSONReading : public Reading {
74  public:
75  JSONReading(const rapidjson::Value& json);
76  ~JSONReading() {};
77 
78  // Return the reading id
79  unsigned long getId() const { return m_id; };
80 
81  private:
82  Datapoint *datapoint(const std::string& name, const rapidjson::Value& json);
83  void escapeCharacter(std::string& stringToEvaluate, std::string pattern);
84 };
85 
86 class ReadingSetException : public std::exception
87 {
88  public:
89  ReadingSetException(const char *what)
90  {
91  m_what = strdup(what);
92  };
94  {
95  if (m_what)
96  free(m_what);
97  };
98  virtual const char *what() const throw()
99  {
100  return m_what;
101  };
102  private:
103  char *m_what;
104 };
105 #endif
106 
ReadingSet::clear
void clear()
Remove the readings from the vector without deleting them.
Definition: reading_set.cpp:376
Reading
An asset reading represented as a class.
Definition: reading.h:33
ReadingSet::moveAllReadings
std::vector< Reading * > * moveAllReadings()
Remove readings from the vector and return a reference to new vector containing readings*.
Definition: reading_set.cpp:387
ReadingSetException
Definition: reading_set.h:86
ReadingSet::merge
void merge(std::vector< Reading * > *readings)
merge the readings in a vector with the set of readings in the reading set.
Definition: reading_set.cpp:221
ReadingSet
Reading set class.
Definition: reading_set.h:26
Datapoint
Name and value pair used to represent a data value within an asset reading.
Definition: datapoint.h:310
ReadingSet::~ReadingSet
virtual ~ReadingSet()
Destructor for a result set.
Definition: reading_set.cpp:155
ReadingSet::removeReading
Reading * removeReading(unsigned long id)
Remove reading from vector based on index and return its pointer.
Definition: reading_set.cpp:400
ReadingSet::getReadingId
unsigned long getReadingId(uint32_t pos)
Return the ID of the nth reading in the reading set.
Definition: reading_set.cpp:419
JSONReading
JSONReading class.
Definition: reading_set.h:73
ReadingSet::removeAll
void removeAll()
Remove all readings from the reading set and delete the memory After this call the reading set exists...
Definition: reading_set.cpp:361
ReadingSet::append
void append(ReadingSet *)
Append the readings in a second reading set to this reading set.
Definition: reading_set.cpp:171
JSONReading::JSONReading
JSONReading(const rapidjson::Value &json)
Construct a reading from a JSON document.
Definition: reading_set.cpp:437
ReadingSet::copy
bool copy(const ReadingSet &src)
Deep copy a set of readings to this reading set.
Definition: reading_set.cpp:274
ReadingSet::ReadingSet
ReadingSet()
Construct an empty reading set.
Definition: reading_set.cpp:40