1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00
doorlockd-mirror/doorlockd/lib/clientmessage.cpp
Ralf Ramsauer 60ed23486b Improved directory structure
Signed-off-by: Ralf Ramsauer <ralf@ramses-pyramidenbau.de>
2015-09-29 14:33:53 +02:00

61 lines
1.7 KiB
C++

#include <json/json.h>
#include "clientmessage.h"
#include "util.h"
const std::string Clientmessage::_tokenKey = "token";
const std::string Clientmessage::_unlockButtonKey = "unlockButton";
const std::string Clientmessage::_lockButtonKey = "lockButton";
const std::string Clientmessage::_emergencyUnlockKey = "emergencyUnlock";
const std::string Clientmessage::_isOpenKey = "isOpen";
Clientmessage::Clientmessage(std::string token,
Doormessage doormessage) :
_token(token),
_doormessage(doormessage)
{
}
std::string Clientmessage::toJson() const
{
Json::StyledWriter writer;
Json::Value message;
message[_tokenKey] = _token;
message[_unlockButtonKey] = _doormessage.isUnlockButton;
message[_lockButtonKey] = _doormessage.isLockButton;
message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock;
message[_isOpenKey] = _doormessage.isOpen;
return writer.write(message);
}
const std::string& Clientmessage::token() const
{
return _token;
}
const Doormessage& Clientmessage::doormessage() const
{
return _doormessage;
}
Clientmessage Clientmessage::fromJson(const std::string &json)
{
Json::Reader reader;
Json::Value root;
std::string token;
Doormessage doormessage;
if (reader.parse(json, root) == false)
throw std::runtime_error("Error parsing JSON");
token = getJsonOrFail<std::string>(root, _tokenKey);
doormessage.isLockButton = getJsonOrFail<bool>(root, _lockButtonKey);
doormessage.isUnlockButton = getJsonOrFail<bool>(root, _unlockButtonKey);
doormessage.isEmergencyUnlock = getJsonOrFail<bool>(root, _emergencyUnlockKey);
doormessage.isOpen = getJsonOrFail<bool>(root, _isOpenKey);
return Clientmessage(token, doormessage);
}