2015-09-24 21:12:59 +02:00
|
|
|
#include "clientmessage.h"
|
2015-09-25 00:38:34 +02:00
|
|
|
#include "util.h"
|
2015-10-01 22:09:55 +02:00
|
|
|
#include "response.h"
|
2015-09-25 00:38:34 +02:00
|
|
|
|
|
|
|
const std::string Clientmessage::_tokenKey = "token";
|
|
|
|
const std::string Clientmessage::_unlockButtonKey = "unlockButton";
|
|
|
|
const std::string Clientmessage::_lockButtonKey = "lockButton";
|
|
|
|
const std::string Clientmessage::_emergencyUnlockKey = "emergencyUnlock";
|
2015-09-25 03:26:07 +02:00
|
|
|
const std::string Clientmessage::_isOpenKey = "isOpen";
|
2015-09-24 21:12:59 +02:00
|
|
|
|
2016-07-17 22:52:58 +02:00
|
|
|
const std::regex Clientmessage::_token_regex(".*/([0-9a-fA-F]+)");
|
|
|
|
|
2016-07-17 22:44:02 +02:00
|
|
|
Clientmessage::Clientmessage(std::string web_address,
|
2015-10-05 23:30:25 +02:00
|
|
|
bool isOpen,
|
2015-09-24 21:12:59 +02:00
|
|
|
Doormessage doormessage) :
|
2016-07-17 22:44:02 +02:00
|
|
|
_web_address(web_address),
|
2016-07-17 22:52:58 +02:00
|
|
|
_token(),
|
2015-10-05 23:30:25 +02:00
|
|
|
_isOpen(isOpen),
|
2015-09-24 21:12:59 +02:00
|
|
|
_doormessage(doormessage)
|
|
|
|
{
|
2016-07-17 22:52:58 +02:00
|
|
|
std::smatch match;
|
|
|
|
if (std::regex_match(_web_address, match, _token_regex)) {
|
|
|
|
if (match.size() == 2) {
|
|
|
|
_token = match[1].str();
|
|
|
|
} else {
|
|
|
|
_token = "ERROR";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_token = "ERROR";
|
|
|
|
}
|
2015-09-24 21:12:59 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 15:54:28 +02:00
|
|
|
Clientmessage::Clientmessage() :
|
2016-07-17 22:44:02 +02:00
|
|
|
_web_address(),
|
2016-07-17 22:52:58 +02:00
|
|
|
_token(),
|
2015-10-07 15:54:28 +02:00
|
|
|
_isOpen(false),
|
|
|
|
_doormessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Clientmessage &Clientmessage::operator=(const Clientmessage &rhs)
|
|
|
|
{
|
|
|
|
// Protect against self assignement
|
|
|
|
if (this == &rhs) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:44:02 +02:00
|
|
|
this->_web_address = rhs._web_address;
|
2016-07-17 22:52:58 +02:00
|
|
|
this->_token = rhs._token;
|
2015-10-07 15:54:28 +02:00
|
|
|
this->_isOpen = rhs._isOpen;
|
|
|
|
this->_doormessage = rhs._doormessage;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:12:59 +02:00
|
|
|
std::string Clientmessage::toJson() const
|
|
|
|
{
|
|
|
|
Json::StyledWriter writer;
|
|
|
|
Json::Value message;
|
|
|
|
|
2016-07-17 22:44:02 +02:00
|
|
|
message[_tokenKey] = _web_address;
|
2015-09-25 00:38:34 +02:00
|
|
|
message[_unlockButtonKey] = _doormessage.isUnlockButton;
|
|
|
|
message[_lockButtonKey] = _doormessage.isLockButton;
|
|
|
|
message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock;
|
2015-10-05 23:30:25 +02:00
|
|
|
message[_isOpenKey] = _isOpen;
|
2015-09-24 21:12:59 +02:00
|
|
|
|
|
|
|
return writer.write(message);
|
|
|
|
}
|
2015-09-25 00:38:34 +02:00
|
|
|
|
2016-07-17 22:52:58 +02:00
|
|
|
const std::string& Clientmessage::token() const
|
|
|
|
{
|
|
|
|
return _token;
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:44:02 +02:00
|
|
|
const std::string& Clientmessage::web_address() const
|
2015-09-25 00:38:34 +02:00
|
|
|
{
|
2016-07-17 22:44:02 +02:00
|
|
|
return _web_address;
|
2015-09-25 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Doormessage& Clientmessage::doormessage() const
|
|
|
|
{
|
|
|
|
return _doormessage;
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
Clientmessage Clientmessage::fromJson(const Json::Value &root)
|
2015-09-25 00:38:34 +02:00
|
|
|
{
|
|
|
|
std::string token;
|
2015-10-05 23:30:25 +02:00
|
|
|
bool isOpen;
|
2015-09-25 00:38:34 +02:00
|
|
|
Doormessage doormessage;
|
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
try {
|
|
|
|
token = getJsonOrFail<std::string>(root, _tokenKey);
|
|
|
|
doormessage.isLockButton = getJsonOrFail<bool>(root, _lockButtonKey);
|
|
|
|
doormessage.isUnlockButton = getJsonOrFail<bool>(root, _unlockButtonKey);
|
|
|
|
doormessage.isEmergencyUnlock = getJsonOrFail<bool>(root, _emergencyUnlockKey);
|
2015-10-05 23:30:25 +02:00
|
|
|
isOpen = getJsonOrFail<bool>(root, _isOpenKey);
|
2015-10-01 22:09:55 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &ex) {
|
|
|
|
throw Response(Response::Code::JsonError, ex.what());
|
|
|
|
}
|
2015-09-25 00:38:34 +02:00
|
|
|
|
2015-10-05 23:30:25 +02:00
|
|
|
return Clientmessage(token, isOpen, doormessage);
|
2015-09-25 00:38:34 +02:00
|
|
|
}
|
2015-10-01 22:09:55 +02:00
|
|
|
|
|
|
|
Clientmessage Clientmessage::fromString(const std::string &string)
|
|
|
|
{
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::Value root;
|
|
|
|
|
|
|
|
if (reader.parse(string, root) == false)
|
|
|
|
throw Response(Response::Code::NotJson,
|
|
|
|
"No valid JSON");
|
|
|
|
|
|
|
|
return fromJson(root);
|
|
|
|
}
|
2015-10-05 23:30:25 +02:00
|
|
|
|
|
|
|
bool Clientmessage::isOpen() const
|
|
|
|
{
|
|
|
|
return _isOpen;
|
|
|
|
}
|