mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 02:14:26 +01:00
Improved Clientmessage
- Added Clientmessage::fromJson - Improved JSON key handling
This commit is contained in:
parent
aa79228e47
commit
b9dc2f5ee1
@ -1,6 +1,12 @@
|
||||
#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";
|
||||
|
||||
Clientmessage::Clientmessage(std::string token,
|
||||
Doormessage doormessage) :
|
||||
@ -14,10 +20,38 @@ std::string Clientmessage::toJson() const
|
||||
Json::StyledWriter writer;
|
||||
Json::Value message;
|
||||
|
||||
message["token"] = _token;
|
||||
message["unlockButton"] = _doormessage.isUnlockButton;
|
||||
message["lockButton"] = _doormessage.isLockButton;
|
||||
message["emergencyUnlock"] = _doormessage.isEmergencyUnlock;
|
||||
message[_tokenKey] = _token;
|
||||
message[_unlockButtonKey] = _doormessage.isUnlockButton;
|
||||
message[_lockButtonKey] = _doormessage.isLockButton;
|
||||
message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock;
|
||||
|
||||
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);
|
||||
|
||||
return Clientmessage(token, doormessage);
|
||||
}
|
||||
|
@ -13,12 +13,21 @@ public:
|
||||
Clientmessage(std::string token,
|
||||
Doormessage doormessage);
|
||||
|
||||
static Clientmessage fromJson(const std::string &json);
|
||||
std::string toJson() const;
|
||||
|
||||
const std::string& token() const;
|
||||
const Doormessage& doormessage() const;
|
||||
|
||||
private:
|
||||
|
||||
std::string _token;
|
||||
Doormessage _doormessage;
|
||||
|
||||
static const std::string _tokenKey;
|
||||
const static std::string _unlockButtonKey;
|
||||
const static std::string _lockButtonKey;
|
||||
const static std::string _emergencyUnlockKey;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user