Fledge
An open source edge computing platform for industrial users
reading.h
1 #ifndef _READING_H
2 #define _READING_H
3 /*
4  * Fledge storage client.
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 <datapoint.h>
13 #include <string>
14 #include <ctime>
15 #include <vector>
16 #include <sys/time.h>
17 #include <rapidjson/document.h>
18 
19 #define DEFAULT_DATE_TIME_FORMAT "%Y-%m-%d %H:%M:%S"
20 #define COMBINED_DATE_STANDARD_FORMAT "%Y-%m-%dT%H:%M:%S"
21 #define ISO8601_DATE_TIME_FORMAT "%Y-%m-%d %H:%M:%S +0000"
22 #define DATE_TIME_BUFFER_LEN 52
23 
33 class Reading {
34  public:
35  Reading(const std::string& asset, Datapoint *value);
36  Reading(const std::string& asset, std::vector<Datapoint *> values);
37  Reading(const std::string& asset, std::vector<Datapoint *> values, const std::string& ts);
38  Reading(const std::string& asset, const std::string& datapoints);
39  Reading(const Reading& orig);
40 
41  ~Reading(); // This should bbe virtual
42  void addDatapoint(Datapoint *value);
43  Datapoint *removeDatapoint(const std::string& name);
44  Datapoint *getDatapoint(const std::string& name) const;
45  std::string toJSON(bool minimal = false) const;
46  std::string getDatapointsJSON() const;
47  // Return AssetName
48  const std::string& getAssetName() const { return m_asset; };
49  // Set AssetName
50  void setAssetName(std::string assetName) { m_asset = assetName; };
51  unsigned int getDatapointCount() { return m_values.size(); };
52  void removeAllDatapoints();
53  // Return Reading datapoints
54  const std::vector<Datapoint *> getReadingData() const { return m_values; };
55  // Return refrerence to Reading datapoints
56  std::vector<Datapoint *>& getReadingData() { return m_values; };
57  bool hasId() const { return m_has_id; };
58  unsigned long getId() const { return m_id; };
59  unsigned long getTimestamp() const { return (unsigned long)m_timestamp.tv_sec; };
60  unsigned long getUserTimestamp() const { return (unsigned long)m_userTimestamp.tv_sec; };
61  void setId(unsigned long id) { m_id = id; };
62  void setTimestamp(unsigned long ts) { m_timestamp.tv_sec = (time_t)ts; };
63  void setTimestamp(struct timeval tm) { m_timestamp = tm; };
64  void setTimestamp(const std::string& timestamp);
65  void getTimestamp(struct timeval *tm) { *tm = m_timestamp; };
66  void setUserTimestamp(unsigned long uTs) { m_userTimestamp.tv_sec = (time_t)uTs; };
67  void setUserTimestamp(struct timeval tm) { m_userTimestamp = tm; };
68  void setUserTimestamp(const std::string& timestamp);
69  void getUserTimestamp(struct timeval *tm) { *tm = m_userTimestamp; };
70 
71  typedef enum dateTimeFormat { FMT_DEFAULT, FMT_STANDARD, FMT_ISO8601, FMT_ISO8601MS } readingTimeFormat;
72 
73  void getFormattedDateTimeStr(const time_t *tv_sec, char *date_time, readingTimeFormat dateFormat) const;
74  // Return Reading asset time - ts time
75  const std::string getAssetDateTime(readingTimeFormat datetimeFmt = FMT_DEFAULT, bool addMs = true) const;
76  // Return Reading asset time - user_ts time
77  const std::string getAssetDateUserTime(readingTimeFormat datetimeFmt = FMT_DEFAULT, bool addMs = true) const;
78  std::string substitute(const std::string& str);
79 
80  protected:
81  Reading() {};
82  Reading& operator=(Reading const&);
83  void stringToTimestamp(const std::string& timestamp, struct timeval *ts);
84  const std::string escape(const std::string& str) const;
85  std::vector<Datapoint *> *JSONtoDatapoints(const rapidjson::Value& json);
86  unsigned long m_id;
87  bool m_has_id;
88  std::string m_asset;
89  struct timeval m_timestamp;
90  struct timeval m_userTimestamp;
91  std::vector<Datapoint *> m_values;
92  // Supported date time formats for 'm_timestamp'
93  static std::vector<std::string> m_dateTypes;
94  private:
95  // Internal class used for macro substitution
96  class Macro {
97  public:
98  Macro(const std::string& dpname, std::string::size_type s,
99  const std::string& defValue) :
100  start(s), name(dpname), def(defValue)
101 
102  {
103  };
104  Macro(const std::string& dpname, std::string::size_type s) :
105  start(s), name(dpname)
106 
107  {
108  };
109  // Start of variable to substitute
110  std::string::size_type start;
111  // Name of variable to substitute
112  std::string name;
113  // Default value to substitute
114  std::string def;
115  };
116  void collectMacroInfo(const std::string& str, std::vector<Macro>& macros);
117 };
118 #endif
119 
const std::string escape(const std::string &str) const
Escape quotes etc to allow the string to be a property value within a JSON document.
Definition: reading.cpp:542
Datapoint * getDatapoint(const std::string &name) const
Return a specific data point by name.
Definition: reading.cpp:216
Name and value pair used to represent a data value within an asset reading.
Definition: datapoint.h:310
void removeAllDatapoints()
Remove all data points for Reading class.
Definition: reading.cpp:171
An asset reading represented as a class.
Definition: reading.h:33
~Reading()
Destructor for Reading class.
Definition: reading.cpp:160
std::string substitute(const std::string &str)
Substitute values from this reading into the string.
Definition: reading.cpp:670
const std::string getAssetDateUserTime(readingTimeFormat datetimeFmt=FMT_DEFAULT, bool addMs=true) const
Return a formatted m_userTimestamp DataTime in UTC.
Definition: reading.cpp:384
void addDatapoint(Datapoint *value)
Add another data point to an asset reading.
Definition: reading.cpp:183
std::string getDatapointsJSON() const
Return the asset reading as a JSON structure encoded in a C++ string.
Definition: reading.cpp:273
std::string toJSON(bool minimal=false) const
Return the asset reading as a JSON structure encoded in a C++ string.
Definition: reading.cpp:235
Datapoint * removeDatapoint(const std::string &name)
Remove a datapoint from the reading.
Definition: reading.cpp:194
void stringToTimestamp(const std::string &timestamp, struct timeval *ts)
Convert a string timestamp, with milliseconds to a struct timeval.
Definition: reading.cpp:457
std::vector< Datapoint * > * JSONtoDatapoints(const rapidjson::Value &json)
Convert a JSON Value object to a set of data points.
Definition: reading.cpp:584
void getFormattedDateTimeStr(const time_t *tv_sec, char *date_time, readingTimeFormat dateFormat) const
Convert time since epoch to a formatted m_timestamp DataTime in UTC and use a cache to speed it up...
Definition: reading.cpp:297
const std::string getAssetDateTime(readingTimeFormat datetimeFmt=FMT_DEFAULT, bool addMs=true) const
Return a formatted m_timestamp DataTime in UTC.
Definition: reading.cpp:340