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
|
|
|
|
|
|
|
Clientmessage::Clientmessage(std::string token,
|
2015-10-05 23:30:25 +02:00
|
|
|
bool isOpen,
|
2015-09-24 21:12:59 +02:00
|
|
|
Doormessage doormessage) :
|
|
|
|
_token(token),
|
2015-10-05 23:30:25 +02:00
|
|
|
_isOpen(isOpen),
|
2015-09-24 21:12:59 +02:00
|
|
|
_doormessage(doormessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Clientmessage::toJson() const
|
|
|
|
{
|
|
|
|
Json::StyledWriter writer;
|
|
|
|
Json::Value message;
|
|
|
|
|
2015-09-25 00:38:34 +02:00
|
|
|
message[_tokenKey] = _token;
|
|
|
|
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
|
|
|
|
|
|
|
const std::string& Clientmessage::token() const
|
|
|
|
{
|
|
|
|
return _token;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|