Fledge
An open source edge computing platform for industrial users
datapoint.h
1 #ifndef _DATAPOINT_H
2 #define _DATAPOINT_H
3 /*
4  * Fledge
5  *
6  * Copyright (c) 2018 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch, Massimiliano Pinto
11  */
12 #include <string>
13 #include <sstream>
14 #include <iomanip>
15 #include <cfloat>
16 #include <vector>
17 #include <logger.h>
18 #include <dpimage.h>
19 #include <databuffer.h>
20 #include <rapidjson/document.h>
21 #include "string_utils.h"
22 
23 class Datapoint;
31  public:
35  DatapointValue(const std::string& value)
36  {
37  m_value.str = new std::string(value);
38  m_type = T_STRING;
39  };
43  DatapointValue(const long value)
44  {
45  m_value.i = value;
46  m_type = T_INTEGER;
47  };
51  DatapointValue(const double value)
52  {
53  m_value.f = value;
54  m_type = T_FLOAT;
55  };
59  DatapointValue(const std::vector<double>& values)
60  {
61  m_value.a = new std::vector<double>(values);
62  m_type = T_FLOAT_ARRAY;
63  };
64 
68  DatapointValue(std::vector<Datapoint*>*& values, bool isDict)
69  {
70  m_value.dpa = values;
71  m_type = isDict? T_DP_DICT : T_DP_LIST;
72  }
73 
77  DatapointValue(const DPImage& value)
78  {
79  m_value.image = new DPImage(value);
80  m_type = T_IMAGE;
81  }
82 
86  DatapointValue(const DataBuffer& value)
87  {
88  m_value.dataBuffer = new DataBuffer(value);
89  m_type = T_DATABUFFER;
90  }
91 
97  {
98  m_value.image = value;
99  m_type = T_IMAGE;
100  }
101 
106  {
107  m_value.dataBuffer = value;
108  m_type = T_DATABUFFER;
109  }
110 
114  DatapointValue(const std::vector< std::vector<double> *>& values)
115  {
116  m_value.a2d = new std::vector< std::vector<double>* >;
117  for (auto row : values)
118  {
119  std::vector<double> *nrow = new std::vector<double>;
120  for (auto& d : *row)
121  {
122  nrow->push_back(d);
123  }
124  m_value.a2d->push_back(nrow);
125  }
126  m_type = T_2D_FLOAT_ARRAY;
127  };
128 
132  DatapointValue(const DatapointValue& obj);
133 
138 
142  ~DatapointValue();
143 
149  void setValue(std::string value)
150  {
151  if(m_value.str)
152  {
153  delete m_value.str;
154  }
155  m_value.str = new std::string(value);
156  m_type = T_STRING;
157  }
158 
164  void setValue(long value)
165  {
166  m_value.i = value;
167  m_type = T_INTEGER;
168  }
169 
175  void setValue(double value)
176  {
177  m_value.f = value;
178  m_type = T_FLOAT;
179  }
180 
184  void setValue(const DPImage& value)
185  {
186  m_value.image = new DPImage(value);
187  m_type = T_IMAGE;
188  }
189 
193  std::string toString() const;
194 
198  std::string toStringValue() const { return *m_value.str; };
199 
203  long toInt() const { return m_value.i; };
207  double toDouble() const { return m_value.f; };
208 
209  // Supported Data Tag Types
210  typedef enum DatapointTag
211  {
212  T_STRING,
213  T_INTEGER,
214  T_FLOAT,
215  T_FLOAT_ARRAY,
216  T_DP_DICT,
217  T_DP_LIST,
218  T_IMAGE,
219  T_DATABUFFER,
220  T_2D_FLOAT_ARRAY
221  } dataTagType;
222 
226  dataTagType getType() const
227  {
228  return m_type;
229  }
230 
231  std::string getTypeStr() const
232  {
233  switch(m_type)
234  {
235  case T_STRING: return std::string("STRING");
236  case T_INTEGER: return std::string("INTEGER");
237  case T_FLOAT: return std::string("FLOAT");
238  case T_FLOAT_ARRAY: return std::string("FLOAT_ARRAY");
239  case T_DP_DICT: return std::string("DP_DICT");
240  case T_DP_LIST: return std::string("DP_LIST");
241  case T_IMAGE: return std::string("IMAGE");
242  case T_DATABUFFER: return std::string("DATABUFFER");
243  case T_2D_FLOAT_ARRAY: return std::string("2D_FLOAT_ARRAY");
244  default: return std::string("INVALID");
245  }
246  }
247 
251  std::vector<Datapoint*>*& getDpVec()
252  {
253  return m_value.dpa;
254  }
255 
259  std::vector<double>*& getDpArr()
260  {
261  return m_value.a;
262  }
263 
267  std::vector<std::vector<double>* >*& getDp2DArr()
268  {
269  return m_value.a2d;
270  }
271 
276  {
277  return m_value.image;
278  }
279 
284  {
285  return m_value.dataBuffer;
286  }
287 
288  private:
289  void deleteNestedDPV();
290  const std::string escape(const std::string& str) const;
291  union data_t {
292  std::string* str;
293  long i;
294  double f;
295  std::vector<double>* a;
296  std::vector<Datapoint*>
297  *dpa;
298  DPImage *image;
299  DataBuffer *dataBuffer;
300  std::vector< std::vector<double>* >
301  *a2d;
302  } m_value;
303  DatapointTag m_type;
304 };
305 
310 class Datapoint {
311  public:
315  Datapoint(const std::string& name, DatapointValue& value) : m_name(name), m_value(value)
316  {
317  }
318 
319  ~Datapoint()
320  {
321  }
327  std::string toJSONProperty()
328  {
329  std::string rval = "\"" + escape(m_name) + "\":";
330  rval += m_value.toString();
331 
332  return rval;
333  }
334 
338  const std::string getName() const
339  {
340  return m_name;
341  }
342 
346  void setName(std::string name)
347  {
348  m_name = name;
349  }
350 
354  const DatapointValue getData() const
355  {
356  return m_value;
357  }
358 
363  {
364  return m_value;
365  }
366 
371  std::vector<Datapoint*>* parseJson(const std::string& json);
372  std::vector<Datapoint*>* recursiveJson(const rapidjson::Value& document);
373 
374  private:
375  std::string m_name;
376  DatapointValue m_value;
377 };
378 #endif
379 
const DatapointValue getData() const
Return Datapoint value.
Definition: datapoint.h:354
~DatapointValue()
Destructor.
Definition: datapoint.cpp:183
Datapoint(const std::string &name, DatapointValue &value)
Construct with a data point value.
Definition: datapoint.h:315
Name and value pair used to represent a data value within an asset reading.
Definition: datapoint.h:310
DatapointValue(const long value)
Construct with an integer value.
Definition: datapoint.h:43
std::string toStringValue() const
Return string value without trailing/leading quotes.
Definition: datapoint.h:198
void setValue(long value)
Set the value of a datapoint, this may also cause the type to be changed.
Definition: datapoint.h:164
void setName(std::string name)
Rename the datapoint.
Definition: datapoint.h:346
long toInt() const
Return long value.
Definition: datapoint.h:203
dataTagType getType() const
Return the Tag type.
Definition: datapoint.h:226
void setValue(std::string value)
Set the value of a datapoint, this may also cause the type to be changed.
Definition: datapoint.h:149
DatapointValue(const std::vector< std::vector< double > *> &values)
Construct with a 2 dimentional array of floating point values.
Definition: datapoint.h:114
DataBuffer * getDataBuffer()
Return the DataBuffer.
Definition: datapoint.h:283
const std::string getName() const
Return the Datapoint name.
Definition: datapoint.h:338
void setValue(double value)
Set the value of a datapoint, this may also cause the type to be changed.
Definition: datapoint.h:175
DatapointValue(const std::string &value)
Construct with a string.
Definition: datapoint.h:35
std::vector< std::vector< double > *> *& getDp2DArr()
Return 2D array of float.
Definition: datapoint.h:267
DatapointValue & operator=(const DatapointValue &rhs)
Assignment Operator.
Definition: datapoint.cpp:247
DatapointValue(DataBuffer *value)
Construct with a DataBuffer.
Definition: datapoint.h:105
Buffer type for storage of arbitrary buffers of data within a datapoint.
Definition: databuffer.h:19
DatapointValue(DPImage *value)
Construct with an Image Pointer, the image becomes owned by the datapointValue.
Definition: datapoint.h:96
DPImage * getImage()
Return the Image.
Definition: datapoint.h:275
double toDouble() const
Return double value.
Definition: datapoint.h:207
Class to hold an actual reading value.
Definition: datapoint.h:30
Simple Image class that will be used within data points to store image data.
Definition: dpimage.h:21
DatapointValue(const DataBuffer &value)
Construct with a DataBuffer.
Definition: datapoint.h:86
DatapointValue & getData()
Return reference to Datapoint value.
Definition: datapoint.h:362
DatapointValue(const std::vector< double > &values)
Construct with an array of floating point values.
Definition: datapoint.h:59
void setValue(const DPImage &value)
Set the value of a datapoint to be an image.
Definition: datapoint.h:184
std::string toJSONProperty()
Return asset reading data point as a JSON property that can be included within a JSON document...
Definition: datapoint.h:327
std::vector< Datapoint * > *& getDpVec()
Return array of datapoints.
Definition: datapoint.h:251
std::string toString() const
Return the value as a string.
Definition: datapoint.cpp:27
DatapointValue(const DPImage &value)
Construct with an Image.
Definition: datapoint.h:77
DatapointValue(const double value)
Construct with a floating point value.
Definition: datapoint.h:51
std::vector< double > *& getDpArr()
Return array of float.
Definition: datapoint.h:259
DatapointValue(std::vector< Datapoint *> *&values, bool isDict)
Construct with an array of Datapoints.
Definition: datapoint.h:68