mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 10:24:26 +01:00
Added proper client and doormessage classes
Signed-off-by: Ralf Ramsauer <ralf@ramses-pyramidenbau.de>
This commit is contained in:
parent
92d19bfc98
commit
8ed116bf7f
@ -52,13 +52,23 @@ include_directories(${JSON_INCLUDE_DIR})
|
|||||||
find_package (Threads)
|
find_package (Threads)
|
||||||
|
|
||||||
set(SRCS
|
set(SRCS
|
||||||
main.cpp
|
clientmessage.cpp
|
||||||
logger.cpp
|
clientmessage.h
|
||||||
door.cpp
|
|
||||||
logic.cpp
|
|
||||||
response.cpp
|
|
||||||
util.cpp
|
|
||||||
daemon.cpp
|
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})
|
add_executable(doorlockd ${SRCS})
|
||||||
|
23
doorlockd/clientmessage.cpp
Normal file
23
doorlockd/clientmessage.cpp
Normal 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
24
doorlockd/clientmessage.h
Normal 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
|
@ -11,17 +11,11 @@
|
|||||||
#include <boost/asio/serial_port.hpp>
|
#include <boost/asio/serial_port.hpp>
|
||||||
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "doormessage.h"
|
||||||
|
|
||||||
class Door final
|
class Door final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
struct Doormessage {
|
|
||||||
bool isUnlockButton = { false };
|
|
||||||
bool isLockButton = { false };
|
|
||||||
bool isEmergencyUnlock = { false };
|
|
||||||
};
|
|
||||||
|
|
||||||
using DoorCallback = std::function<void(Doormessage)>;
|
using DoorCallback = std::function<void(Doormessage)>;
|
||||||
enum class State {Unlocked, Locked};
|
enum class State {Unlocked, Locked};
|
||||||
|
|
||||||
|
10
doorlockd/doormessage.h
Normal file
10
doorlockd/doormessage.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef DOORMESSAGE_H
|
||||||
|
#define DOORMESSAGE_H
|
||||||
|
|
||||||
|
struct Doormessage {
|
||||||
|
bool isUnlockButton = { false };
|
||||||
|
bool isLockButton = { false };
|
||||||
|
bool isEmergencyUnlock = { false };
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -240,25 +240,21 @@ void Logic::_createNewToken(const bool stillValid)
|
|||||||
_onClientUpdate.notify_all();
|
_onClientUpdate.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Logic::getClientMessage()
|
Clientmessage Logic::getClientMessage()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(_mutex);
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
Json::Value message;
|
Clientmessage retval(_webPrefix + toHexString(_curToken),
|
||||||
Json::StyledWriter writer;
|
_doormessage);
|
||||||
|
|
||||||
message["token"] = _webPrefix + toHexString(_curToken);
|
|
||||||
message["unlockButton"] = _doormessage.isUnlockButton;
|
|
||||||
message["lockButton"] = _doormessage.isLockButton;
|
|
||||||
message["emergencyUnlock"] = _doormessage.isEmergencyUnlock;
|
|
||||||
|
|
||||||
// Reset 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);
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
_doormessage = doormessage;
|
_doormessage = doormessage;
|
||||||
|
_onClientUpdate.notify_all();
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "door.h"
|
#include "door.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "response.h"
|
#include "response.h"
|
||||||
|
#include "clientmessage.h"
|
||||||
|
|
||||||
/* The "Logic" class
|
/* The "Logic" class
|
||||||
*
|
*
|
||||||
@ -35,7 +36,7 @@ public:
|
|||||||
Response parseRequest(const Json::Value &root);
|
Response parseRequest(const Json::Value &root);
|
||||||
|
|
||||||
// Returns the current Token
|
// Returns the current Token
|
||||||
std::string getClientMessage();
|
Clientmessage getClientMessage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -55,7 +56,7 @@ private:
|
|||||||
// stillValid indicates whether the old (previous) token is still valid
|
// stillValid indicates whether the old (previous) token is still valid
|
||||||
void _createNewToken(const bool stillValid);
|
void _createNewToken(const bool stillValid);
|
||||||
|
|
||||||
void _doorCallback(Door::Doormessage doormessage);
|
void _doorCallback(Doormessage doormessage);
|
||||||
|
|
||||||
const Logger &_logger;
|
const Logger &_logger;
|
||||||
|
|
||||||
@ -83,7 +84,7 @@ private:
|
|||||||
// General mutex for concurrent data access
|
// General mutex for concurrent data access
|
||||||
mutable std::mutex _mutex = {};
|
mutable std::mutex _mutex = {};
|
||||||
|
|
||||||
Door::Doormessage _doormessage = {};
|
Doormessage _doormessage = {};
|
||||||
|
|
||||||
// This variable gets notified on token updates
|
// This variable gets notified on token updates
|
||||||
std::condition_variable &_onClientUpdate;
|
std::condition_variable &_onClientUpdate;
|
||||||
|
@ -107,7 +107,7 @@ static void session(tcp::socket &&sock)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (run) {
|
if (run) {
|
||||||
sock.write_some(boost::asio::buffer(logic->getClientMessage()));
|
sock.write_some(boost::asio::buffer(logic->getClientMessage().toJson()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user