KPN Things Device SDK
senml_record.h
Go to the documentation of this file.
1/* _ __ ____ _ _
2 * | |/ / | _ \ | \ | |
3 * | ' / | |_) | | \| |
4 * | . \ | __/ | |\ |
5 * |_|\_\ |_| |_| \_|
6 *
7 * (c) 2020 KPN
8 * License: MIT License.
9 * Author: Joseph Verburg, Jan Bogaerts
10 *
11 * record base class
12 */
13
14#ifndef SENMLTIMED
15#define SENMLTIMED
16
17#include "../defaults.h"
18#include "../senml_base.h"
19#include "../senml_enums.h"
20
33class SenMLRecord : public SenMLBase {
34 friend class SenMLCborParser;
35 friend class SenMLJsonListener;
36
37 public:
41 SenMLRecord() : _index(THINGSML_NO_INDEX), _unit(SENML_UNIT_NONE), _time(NAN), _updateTime(0){};
42
48 SenMLRecord(const char *name)
49 : _index(THINGSML_NO_INDEX), _name(name), _unit(SENML_UNIT_NONE), _time(NAN), _updateTime(0){};
50
51 SenMLRecord(ThingsMLMeasurementIndex index) : _index(index), _unit(SENML_UNIT_NONE), _time(NAN), _updateTime(0){};
59 SenMLRecord(const char *name, SenMLUnit unit)
60 : _index(THINGSML_NO_INDEX), _name(name), _unit(unit), _time(NAN), _updateTime(0){};
61
69 double getTime() {
70 return this->_time;
71 };
72
88 bool setTime(double value, bool absolute = true);
89
97 void setTimeDirect(double value) {
98 this->_time = value;
99 }
100
106 inline const char *getName() {
107 return this->_name;
108 }
109
111 return this->_index;
112 }
113
121 inline void setName(const char *name) {
122 this->_name = name;
123 }
124
129 inline double getUpdateTime() {
130 return this->_updateTime;
131 }
132
144 bool setUpdateTime(double value, bool absolute = true);
145
154 return this->_unit;
155 }
156
166 this->_unit = value;
167 }
168
176 virtual int fieldsToJson();
177
186 virtual int fieldsToCbor();
187
188 protected:
189 /*
190 renders all the fields to json, including the starting and ending brackets.
191 Inheriters can extend this function if they want to add extra fields to the json output
192 */
193 virtual int contentToJson();
194
195 // renders all the fields to cbor format. renders all the fields of the object including the
196 // length info at the beginning
197 virtual int contentToCbor();
198
199 // This function is called by the root SenMLPack object to indicate that the object
200 // should adjust it's time info relative to the new base time (if applicable)
201 // doesn't do anything by default
202 virtual void adjustToBaseTime(double prev, double time);
203
204 // called while parsing a senml message, when the parser found the value for an SenMLJsonListener
205 virtual void actuate(const void *value, int dataLength, SenMLDataType dataType);
206
207 // calculates the nr of fields that this record will produce.
208 // The default implementation already adds 1 field for the value.
209 int getFieldLength();
210
211 private:
213 const char *_name = NULL;
214 SenMLUnit _unit;
215 double _time;
216 double _updateTime;
217};
218
219#endif // SENMLRECORD
Definition: senml_base.h:24
Definition: senml_cbor_parser.h:31
Definition: senml_json_parser.h:23
virtual void value(String value)
Definition: senml_json_parser.cpp:97
Definition: senml_record.h:33
SenMLRecord(const char *name, SenMLUnit unit)
Definition: senml_record.h:59
ThingsMLMeasurementIndex getIndex()
Definition: senml_record.h:110
void setName(const char *name)
Definition: senml_record.h:121
double getUpdateTime()
Definition: senml_record.h:129
virtual void actuate(const void *value, int dataLength, SenMLDataType dataType)
Definition: senml_record.cpp:93
virtual int contentToCbor()
Definition: senml_record.cpp:98
virtual int fieldsToCbor()
Definition: senml_record.cpp:124
int getFieldLength()
Definition: senml_record.cpp:104
void setTimeDirect(double value)
Definition: senml_record.h:97
bool setUpdateTime(double value, bool absolute=true)
Definition: senml_record.cpp:44
SenMLRecord(ThingsMLMeasurementIndex index)
Definition: senml_record.h:51
SenMLRecord(const char *name)
Definition: senml_record.h:48
void setUnit(SenMLUnit value)
Definition: senml_record.h:165
double getTime()
Definition: senml_record.h:69
SenMLRecord()
Definition: senml_record.h:41
SenMLUnit getUnit()
Definition: senml_record.h:153
virtual void adjustToBaseTime(double prev, double time)
Definition: senml_record.cpp:56
bool setTime(double value, bool absolute=true)
Definition: senml_record.cpp:25
const char * getName()
Definition: senml_record.h:106
virtual int fieldsToJson()
Definition: senml_record.cpp:67
virtual int contentToJson()
Definition: senml_record.cpp:49
SenMLUnit
Definition: senml_enums.h:97
@ SENML_UNIT_NONE
Definition: senml_enums.h:98
ThingsMLMeasurementIndex
Definition: senml_enums.h:51
@ THINGSML_NO_INDEX
Definition: senml_enums.h:52
SenMLDataType
Definition: senml_enums.h:25