CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
Observations.tpl.h
Go to the documentation of this file.
1
15#include "Utils.h"
16
17namespace cmlabs {
18
31template <typename T>
32std::list<GenericObservation<T>*> GenericObservation<T>::ReadAllFromMessage(DataMessage* msg) {
33 uint32 n = 1;
34 std::list<GenericObservation<T>*> list;
36 uint32 size;
37 const char* data;
38 while ((data = msg->getData(n, "OBS", size)) && size && (size == sizeof(ObsHeader) + sizeof(T))) {
39 obs = new GenericObservation<T>();
40 if (obs->readFromData(data, size))
41 list.push_back(obs);
42 else
43 delete obs;
44 n++;
45 }
46 return list;
47}
48
52template <typename T>
54 // #################
55 return false;
56}
57
60template <typename T>
62 memset(&header, 0, sizeof(ObsHeader));
64 header.size = sizeof(ObsHeader);
65 header.timestamp = GetTimeNow();
66 header.type = data.getType();
67 data.init();
68}
69
75template <typename T>
76GenericObservation<T>::GenericObservation(const char* cdata, uint32 size, uint64 id) {
77 if (!readFromData(cdata, size, id)) {
78 memset(&header, 0, sizeof(ObsHeader));
80 header.size = sizeof(ObsHeader);
81 header.timestamp = GetTimeNow();
82 header.type = data.getType();
83 data.init();
84 }
85}
86
87
88template <typename T>
90
91//template <typename T>
92//bool GenericObservation<T>::init() {}
93
102template <typename T>
104 if (!msg || (!header.id && !id)) return false;
105 uint32 s;
106 if (!id)
107 id = header.id;
108 if (msg->getData(id, "OBS", s))
109 return false;
110 // calculate size needed
111 header.size = sizeof(ObsHeader) + sizeof(T);
112 // create temp memory buffer
113 char* buffer = new char[header.size];
114 memcpy(buffer, &header, sizeof(ObsHeader));
115 memcpy(buffer + sizeof(ObsHeader), &data, sizeof(T));
116 bool res = msg->setData(id, "OBS", buffer, header.size);
117 delete[] buffer;
118 return res;
119}
120
130template <typename T>
131bool GenericObservation<T>::readFromData(const char* cdata, uint32 size, uint64 id) {
132 if (!cdata || (size != (sizeof(ObsHeader) + sizeof(T))))
133 return false;
134 if (((ObsHeader*)cdata)->type != header.type)
135 return false;
136 memcpy(&header, cdata, sizeof(ObsHeader));
137 memcpy(&data, cdata + sizeof(ObsHeader), sizeof(T));
138 return true;
139}
140
143template <typename T>
145 if (!msg || !id) return false;
146 uint32 s;
147 const char* cdata = msg->getData(id, "OBS", s);
148 return readFromData(cdata, s, id);
149}
150
151//template <typename T>
152//bool GenericObservation<T>::buildFromQuery(std::string& query) {
153// return data.buildFromQuery(query.c_str());
154//}
155
158template <typename T>
162
170template <typename T>
171bool GenericObservation<T>::extractDataFromMessage(DataMessage* msg, std::map<std::string, std::string>& mappings) {
172 if (!data.extractDataFromMessage(msg, mappings))
173 return false;
174 // Extract header data
175 //header.creatorID = (uint64)msg->getInt("SystemID");
176 header.creatorID = msg->getSystemID();
177 header.objectID = (uint64)msg->getInt("ObjectID");
178 header.confidence = msg->getFloat("Confidence");
179 header.consensus = msg->getFloat("Consensus");
180 header.timestamp = msg->getCreatedTime();
181 return true;
182}
183
185template <typename T>
187 return data.print();
188}
189
193template <typename T>
195 std::string json = data.toJSON();
196 if (json.length())
197 return utils::StringFormat("{ \"datatype\": \"%s\", \"name\": \"%s\", \"id\": %llu, \"timestamp\": %llu, \"timetext\": \"%s\", \"objectid\": %llu, \"systemid\": %llu, \"topicid\": %llu, \"confidence\": %.3f, \"consensus\": %.3f, %s}\n",
198 data.getTypeName().c_str(), header.name, header.id, header.timestamp, PrintTimeString(header.timestamp).c_str(), header.objectID, header.creatorID, header.topicID, header.confidence, header.consensus, json.c_str());
199 else
200 return utils::StringFormat("{ \"datatype\": \"%s\", \"name\": \"%s\", \"id\": %llu, \"timestamp\": %llu, \"timetext\": \"%s\", \"objectid\": %llu, \"systemid\": %llu, \"topicid\": %llu, \"confidence\": %.3f, \"consensus\": %.3f, \"datatype\": \"unknown\"}\n",
201 data.getTypeName().c_str(), header.name, header.id, header.timestamp, PrintTimeString(header.timestamp).c_str(), header.objectID, header.creatorID, header.topicID, header.confidence, header.consensus);
202}
203
204} // namespace cmlabs
#define OBSERVATIONID
Definition ObjectIDs.h:74
Cross-platform utility toolbox for CMSDK: threading, synchronization, shared memory,...
The central Psyclone data container: a self-contained binary message with typed, named user entries.
uint32 getSystemID()
getSystemID() Get and return message system id
bool getInt(const char *key, int64 &value)
getInt(const char* key, int64& value)
const char * getData(const char *key, uint32 &size)
getData(const char* key, uint32& size)
bool getFloat(const char *key, float64 &value)
getFloat(const char* key, float64& value)
uint64 getCreatedTime()
getCreatedTime()
bool setData(const char *key, const char *value, uint32 size)
setData(const char* key, const char* value, uint32 size)
std::string toJSON()
JSON object rendering.
bool extractDataFromMessage(DataMessage *msg, std::map< std::string, std::string > &mappings)
Populate the payload from a message using logical→actual field mappings.
static bool WriteAllToMessage(DataMessage *msg, std::list< GenericObservation< T > * > &list)
Serialize all observations in list into msg.
ObsHeader header
Identity/provenance/confidence header.
bool readFromMessage(DataMessage *msg, uint64 id=0)
Read the observation with id id (0 = first) from msg.
std::string print()
One-line human-readable rendering (header + payload).
static std::list< GenericObservation< T > * > ReadAllFromMessage(DataMessage *msg)
Deserialize every observation of type T carried by msg.
bool writeToMessage(DataMessage *msg, uint64 id)
Append this observation to msg under entry id id.
bool readFromData(const char *data, uint32 size, uint64 id=0)
Parse from a raw blob.
GenericObservation()
Empty observation (zeroed header and payload).
bool isBetween(GenericObservation< T > *a, GenericObservation< T > *b)
Range test delegated to the payload's isBetween(); see per-type notes on semantics.
std::string PrintTimeString(uint64 t, bool local=true, bool us=true, bool ms=true)
Definition PsyTime.cpp:676
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
std::string StringFormat(const char *format,...)
printf into a std::string.
Definition Utils.cpp:6626
Fixed-size header preceding every observation payload: sizes/ids, provenance (creator,...