1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-15 19:49:12 +01:00

Added proper client and doormessage classes

Signed-off-by: Ralf Ramsauer <ralf@ramses-pyramidenbau.de>
This commit is contained in:
Ralf Ramsauer 2015-09-24 21:12:59 +02:00
parent 92d19bfc98
commit 8ed116bf7f
8 changed files with 86 additions and 28 deletions

View File

@ -52,13 +52,23 @@ include_directories(${JSON_INCLUDE_DIR})
find_package (Threads)
set(SRCS
main.cpp
logger.cpp
door.cpp
logic.cpp
response.cpp
util.cpp
clientmessage.cpp
clientmessage.h
daemon.cpp
daemon.h
door.cpp
door.h
doormessage.h
logger.cpp
logger.h
logic.cpp
logic.h
response.cpp
response.h
util.cpp
util.h
main.cpp
)
add_executable(doorlockd ${SRCS})

View File

@ -0,0 +1,23 @@
#include <json/json.h>
#include "clientmessage.h"
Clientmessage::Clientmessage(std::string token,
Doormessage doormessage) :
_token(token),
_doormessage(doormessage)
{
}
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;
return writer.write(message);
}

24
doorlockd/clientmessage.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef CLIENTMESSAGE_H
#define CLIENTMESSAGE_H
#include <string>
#include "doormessage.h"
class Clientmessage
{
public:
Clientmessage(std::string token,
Doormessage doormessage);
std::string toJson() const;
private:
std::string _token;
Doormessage _doormessage;
};
#endif

View File

@ -11,17 +11,11 @@
#include <boost/asio/serial_port.hpp>
#include "logger.h"
#include "doormessage.h"
class Door final
{
public:
struct Doormessage {
bool isUnlockButton = { false };
bool isLockButton = { false };
bool isEmergencyUnlock = { false };
};
using DoorCallback = std::function<void(Doormessage)>;
enum class State {Unlocked, Locked};

10
doorlockd/doormessage.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef DOORMESSAGE_H
#define DOORMESSAGE_H
struct Doormessage {
bool isUnlockButton = { false };
bool isLockButton = { false };
bool isEmergencyUnlock = { false };
};
#endif

View File

@ -240,25 +240,21 @@ void Logic::_createNewToken(const bool stillValid)
_onClientUpdate.notify_all();
}
std::string Logic::getClientMessage()
Clientmessage Logic::getClientMessage()
{
std::lock_guard<std::mutex> l(_mutex);
Json::Value message;
Json::StyledWriter writer;
message["token"] = _webPrefix + toHexString(_curToken);
message["unlockButton"] = _doormessage.isUnlockButton;
message["lockButton"] = _doormessage.isLockButton;
message["emergencyUnlock"] = _doormessage.isEmergencyUnlock;
Clientmessage retval(_webPrefix + toHexString(_curToken),
_doormessage);
// Reset doormessage
_doormessage = Door::Doormessage();
_doormessage = Doormessage();
return writer.write(message);
return retval;
}
void Logic::_doorCallback(Door::Doormessage doormessage)
void Logic::_doorCallback(Doormessage doormessage)
{
std::lock_guard<std::mutex> l(_mutex);
_doormessage = doormessage;
_onClientUpdate.notify_all();
}

View File

@ -11,6 +11,7 @@
#include "door.h"
#include "logger.h"
#include "response.h"
#include "clientmessage.h"
/* The "Logic" class
*
@ -35,7 +36,7 @@ public:
Response parseRequest(const Json::Value &root);
// Returns the current Token
std::string getClientMessage();
Clientmessage getClientMessage();
private:
@ -55,7 +56,7 @@ private:
// stillValid indicates whether the old (previous) token is still valid
void _createNewToken(const bool stillValid);
void _doorCallback(Door::Doormessage doormessage);
void _doorCallback(Doormessage doormessage);
const Logger &_logger;
@ -83,7 +84,7 @@ private:
// General mutex for concurrent data access
mutable std::mutex _mutex = {};
Door::Doormessage _doormessage = {};
Doormessage _doormessage = {};
// This variable gets notified on token updates
std::condition_variable &_onClientUpdate;

View File

@ -107,7 +107,7 @@ static void session(tcp::socket &&sock)
}
if (run) {
sock.write_some(boost::asio::buffer(logic->getClientMessage()));
sock.write_some(boost::asio::buffer(logic->getClientMessage().toJson()));
}
};