11#include <boost/algorithm/string/join.hpp>
40 template <
typename T,
int x = -1,
int y = -1>
61 std::vector<unsigned char>
hash;
83 virtual void read(
void);
92 virtual void write(
void);
229 std::vector <unsigned char>
bytes(
void);
239 std::vector <unsigned char>
sha256(
void);
255 std::vector <unsigned char>
sign(std::shared_ptr<SIGN::Signer> signer = NULL);
268 std::vector <unsigned char>
fingerprint(std::shared_ptr<SIGN::Signer> signer);
277 inline std::shared_ptr<Variable>
ptr(
void)
303 bool mqtt(std::shared_ptr<MQTT::Publisher> publisher,
int qos = 0,
bool retain =
false);
311template<
typename T,
int x,
int y>
312SOIL::Variable<T, x, y>::Variable(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology,
Range<T> range,
TIME time, std::string nonce) :
SOIL::
Figure<T, x, y>(parent, uuid, name, description, unit, ontology, range, time)
314 if (
uuid.substr(0, 3) !=
"VAR")
316 throw std::logic_error(
"UUIDs for Variables must start with VAR!");
322template<
typename T,
int x,
int y>
327template<
typename T,
int x,
int y>
328inline std::shared_ptr<SOIL::Variable<T,x,y> >
SOIL::Variable<T, x, y>::create(std::shared_ptr<Element> parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology,
Range<T> range,
TIME time, std::string nonce)
330 Variable<T, x, y>* variable =
new Variable<T, x, y>(parent, uuid, name, description, unit, ontology, range, time, nonce);
331 return variable->
ptr();
334template<
typename T,
int x,
int y>
342template<
typename T,
int x,
int y>
349 std::ostringstream buffer;
351 for (std::string::size_type i = 0; i < hash.size();i++)
353 buffer << std::hex << std::setfill(
'0') << std::setw(2) << std::uppercase << (int)hash[i];
354 if (i != hash.size() - 1)
356 buffer << std::setw(1) <<
" ";
360 std::string readable_hash = buffer.str();
362 json_root[U(
"nonce")] = (nonce ==
"") ? HTTP::Json::null() :
SOIL::to_json(nonce);
363 json_root[U(
"hash")] = (readable_hash ==
"") ? HTTP::Json::null() :
SOIL::to_json(readable_hash);
364 json_root[U(
"covariance")] = covariance.
wjson()[U(
"value")];
369template<
typename T,
int x,
int y>
374template<
typename T,
int x,
int y>
379template<
typename T,
int x,
int y>
383 this->covariance = covariance;
386template<
typename T,
int x,
int y>
390 std::vector<unsigned char> result;
393 std::vector<unsigned char> bytes_covariance = covariance.
serialize_value();
394 std::vector<unsigned char> bytes_unit(3,
' ');
396 std::vector<unsigned char> bytes_nonce;
397 for (
int i = 0; i < static_cast<int>(std::min(
static_cast<size_t>(3),
Figure<T,x,y>::unit.length())); i++)
401 for (
int i = 0; i < static_cast<int>(nonce.length()); i++)
403 bytes_nonce.push_back(nonce.at(i));
406 result.
insert(result.end(), bytes_dimension.begin(), bytes_dimension.end());
407 result.insert(result.end(), bytes_value.begin(), bytes_value.end());
408 result.insert(result.end(), bytes_covariance.begin(), bytes_covariance.end());
409 result.insert(result.end(), bytes_unit.begin(), bytes_unit.end());
410 result.insert(result.end(), bytes_time.begin(), bytes_time.end());
411 result.insert(result.end(), bytes_nonce.begin(), bytes_nonce.end());
416template<
typename T,
int x,
int y>
419 std::vector<unsigned char> data = this->bytes();
423template<
typename T,
int x,
int y>
426 return signer->sign(this->sha256());
429template<
typename T,
int x,
int y>
434 this->hash = this->fingerprint(signer);
437 this->hash = this->sha256();
443template<
typename T,
int x,
int y>
446 std::string topic = boost::algorithm::join(this->fqid(),
"/");
447 return publisher->publish(topic, this->
json(), qos, retain);
451template<
typename T,
int x,
int y>
457 response.set_body(this->wjson());
458 response.set_status_code(HTTP::Status::OK);
463template<
typename T,
int x,
int y>
467 response.set_body(this->wjson());
468 response.set_status_code(HTTP::Status::OK);
473template<
typename T,
int x,
int y>
std::vector< web::http::method > allowed_methods
Allowed methods.
static std::vector< unsigned char > sha256(const unsigned char *data, size_t length)
SHA256 hash.
HTTP::Json wjson(void)
WJSON representation.
void set_null(bool _null=true)
Set null.
std::vector< unsigned char > serialize_value(void) const
Serialize value.
bool insert(std::string uuid, std::shared_ptr< Element > child)
Add Child Element.
std::string uuid
Local UUID.
std::string description
Description.
std::string ontology
Ontology identifier.
std::shared_ptr< Element > parent
Parent Pointer.
std::recursive_mutex mutex
Element Mutex.
std::shared_ptr< Element > self
Self Pointer.
std::vector< unsigned char > sha256(void)
Calculate SHA256.
HTTP::Response handle_get(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP GET Handler.
std::shared_ptr< Variable > ptr(void)
Get Pointer.
void set_covariance(Container< T, x, x > covariance)
Set Covariance.
HTTP::Json wjson(void) override
HTTP JSON.
static std::shared_ptr< Variable > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME(), std::string nonce="")
Create new Variable.
Variable(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME(), std::string nonce="")
Constructor.
HTTP::Response handle_options(HTTP::Request message, std::smatch match=std::smatch()) override
HTTP OPTIONS Handler.
Variable< T, x, y > & operator=(const Container< T, x, y > &value)
Assignment operator.
bool mqtt(std::shared_ptr< MQTT::Publisher > publisher, int qos=0, bool retain=false)
Publish to MQTT.
virtual void read(void)
Read callback.
virtual void write(void)
Write callback.
std::vector< unsigned char > fingerprint(std::shared_ptr< SIGN::Signer > signer)
Calculate RSA fingerprint.
void update(const Container< T, x, y > &value, TIME time, std::string nonce="")
Update value.
std::vector< unsigned char > bytes(void)
Get bytewise representation.
Container< T, x, x > covariance
Covariance of the value.
std::vector< unsigned char > sign(std::shared_ptr< SIGN::Signer > signer=NULL)
Sign the variable data.
std::vector< unsigned char > hash
Checking Hash.
web::json::value Json
HTTP JSON.
web::http::http_request Request
HTTP Request.
web::http::http_response Response
HTTP Response.
DLL web::json::value to_json(const T &value)
Value to JSON.