SOIL C++
C++ Unified Device Interface
Parameter.h
Go to the documentation of this file.
1#pragma once
2#include "constants.h"
3#include "Types.h"
4#include "Element.h"
5#include "Container.h"
6#include "Range.h"
7#include "Figure.h"
8#include "MQTT/Publisher.h"
9#include <boost/algorithm/string/join.hpp>
10
11namespace SOIL
12{
33 template <typename T, int x=-1, int y=-1>
34 class Parameter : public Figure<T, x, y>
35 {
36 protected:
37
38 //Looks stale as Figure as a time member
39 //TIME time;
40
47
54 virtual void read(void);
55
62 virtual void write(void);
63
64 public:
79 Parameter(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant = false, std::string ontology = "", Range<T> range = Range<T>(), TIME time = TIME());
80
86 ~Parameter();
87
103 static std::shared_ptr<Parameter> create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant = false, std::string ontology = "", Range<T> range = Range<T>(), TIME time = TIME());
104
117
118
128 HTTP::Json wjson(void) override;
129
141 HTTP::Response handle_get(HTTP::Request message, std::smatch match = std::smatch()) override;
142
158 HTTP::Response handle_patch(HTTP::Request message, std::smatch match = std::smatch()) override;
159
166 inline std::shared_ptr<Parameter> ptr(void)
167 {
168 return std::dynamic_pointer_cast<Parameter>(Element::self);
169 }
170
192 bool mqtt(std::shared_ptr<MQTT::Publisher> publisher, int qos = 0, bool retain = false);
193 };
194
195
196
197}
198
199
200
201template<typename T, int x, int y>
202SOIL::Parameter<T, x, y>::Parameter(std::shared_ptr<SOIL::Element> parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant, std::string ontology, Range<T> range, SOIL::TIME time) : SOIL::Figure<T, x, y>(parent, uuid, name, description, unit, ontology, range, time), constant(constant)
203{
204 if (uuid.substr(0, 3) != "PAR")
205 {
206 throw std::logic_error("UUIDs for parameters must start with PAR!");
207 }
208
209 HTTP::Resource::allowed_methods = { HTTP::Methods::GET, HTTP::Methods::OPTIONS, HTTP::Methods::HEAD, HTTP::Methods::PATCH };
210}
211
212template<typename T, int x, int y>
214{
215}
216
217template<typename T, int x, int y>
218inline std::shared_ptr<SOIL::Parameter<T, x, y> > SOIL::Parameter<T, x, y>::create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant, std::string ontology, Range<T> range, TIME time)
219{
220 Parameter<T, x, y>* parameter = new Parameter<T, x, y>(parent, uuid, name, description, unit, constant, ontology, range, time);
221 return parameter->ptr();
222}
223
224template<typename T, int x, int y>
226{
228 return *this;
229}
230
231
232template<typename T, int x, int y>
234{
235 std::unique_lock<std::recursive_mutex> lock(Element::mutex);
236 HTTP::Json json_root = Figure<T,x,y> ::wjson();
237 json_root[U("constant")] = HTTP::Json::boolean(constant);
238
239 return json_root;
240}
241
242template<typename T, int x, int y>
244{
245}
246
247template<typename T, int x, int y>
249{
250}
251
252
253template<typename T, int x, int y>
255{
256 this->read();
257
258 HTTP::Response response;
259 HTTP::Json body = this->wjson();
260 response.set_body(body);
261
262 response.set_status_code(HTTP::Status::OK);
263
264 return response;
265}
266
267template<typename T, int x, int y>
269{
270 auto task = message.extract_json();
271 task.wait();
272 HTTP::Json external_json = task.get();
273
274 SOIL::TIME timestamp;
275
276 if (external_json.has_field(U("timestamp")))
277 {
278 timestamp = to_value<TIME>(external_json[U("timestamp")]);
279 }
280 else
281 {
282 timestamp = SOIL::TIME::utc_now();
283 }
284
285
286 this->update(Container<T, x, y>(external_json[U("value")]), timestamp);
287
288
289 this->write();
290
291 HTTP::Response response;
292 response.set_body(this->wjson());
293 response.set_status_code(HTTP::Status::Created);
294
295
296 return response;
297}
298
299template<typename T, int x, int y>
300inline bool SOIL::Parameter<T, x, y>::mqtt(std::shared_ptr<MQTT::Publisher> publisher, int qos, bool retain)
301{
302 std::string topic = boost::algorithm::join(this->fqid(), "/");
303 return publisher->publish(topic, this->json(), qos, retain);
304}
305
306
307
nlohmann::json json
std::vector< web::http::method > allowed_methods
Allowed methods.
Definition: Resource.h:42
std::string uuid
Local UUID.
Definition: Element.h:53
std::string description
Description.
Definition: Element.h:67
std::string ontology
Ontology identifier.
Definition: Element.h:76
std::string name
Name.
Definition: Element.h:60
std::shared_ptr< Element > parent
Parent Pointer.
Definition: Element.h:39
std::recursive_mutex mutex
Element Mutex.
Definition: Element.h:83
std::shared_ptr< Element > self
Self Pointer.
Definition: Element.h:46
Intermediate class for Variable and Parameter that derives from Element.
Definition: Figure.h:49
Figure< T, x, y > & operator=(const Container< T, x, y > &value)
Assignment operator.
Definition: Figure.h:259
std::string unit
Unit.
Definition: Figure.h:63
TIME time
Data Timestamp.
Definition: Figure.h:56
Container< T, x, y > value
Value.
Definition: Figure.h:70
Range< T > range
Range.
Definition: Figure.h:77
HTTP::Json wjson(void) override
HTTP JSON.
Definition: Figure.h:229
Parameter Class.
Definition: Parameter.h:35
HTTP::Response handle_get(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP GET Handler.
Definition: Parameter.h:254
~Parameter()
Destructor.
Definition: Parameter.h:213
std::shared_ptr< Parameter > ptr(void)
Get Pointer.
Definition: Parameter.h:166
virtual void read(void)
Read callback.
Definition: Parameter.h:243
Parameter(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant=false, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME())
Constructor.
Definition: Parameter.h:202
Parameter< T, x, y > & operator=(const Container< T, x, y > &value)
Assignment operator.
Definition: Parameter.h:225
bool mqtt(std::shared_ptr< MQTT::Publisher > publisher, int qos=0, bool retain=false)
Publish to MQTT.
Definition: Parameter.h:300
HTTP::Response handle_patch(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP PATCH Handler.
Definition: Parameter.h:268
static std::shared_ptr< Parameter > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, bool constant=false, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME())
Create new Parameter.
Definition: Parameter.h:218
HTTP::Json wjson(void) override
HTTP JSON.
Definition: Parameter.h:233
bool constant
Constant flag.
Definition: Parameter.h:46
virtual void write(void)
Write callback.
Definition: Parameter.h:248
Range Helper Class.
Definition: Range.h:25
SOIL Time.
Definition: Time.h:13
static DLL boost::posix_time::ptime utc_now(void)
Current Time.
Definition: Time.cpp:57
web::json::value Json
HTTP JSON.
Definition: Types.h:39
web::http::http_request Request
HTTP Request.
Definition: Types.h:11
web::http::http_response Response
HTTP Response.
Definition: Types.h:18
Type definitions.
Definition: Container.h:7
Time TIME
SOIL Time.
Definition: Types.h:54