1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 10:24:26 +01:00

Added Client messages

This commit is contained in:
Ralf Ramsauer 2015-09-24 18:57:32 +02:00
parent 4f22e652e8
commit 3de8855dac
2 changed files with 32 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#include <chrono>
#include <functional>
#include <cstdlib>
#include <json/json.h>
@ -31,6 +32,10 @@ Logic::Logic(const chrono::seconds tokenTimeout,
srand(time(NULL));
_createNewToken(false);
_door.setDoorCallback(std::bind(&Logic::_doorCallback,
this,
std::placeholders::_1));
_tokenUpdater = thread([this] () {
while (_run)
{
@ -235,7 +240,25 @@ void Logic::_createNewToken(const bool stillValid)
_onClientUpdate.notify_all();
}
std::string Logic::getClientMessage() const
std::string Logic::getClientMessage()
{
return _webPrefix + toHexString(_curToken);
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;
// Reset doormessage
_doormessage = Door::Doormessage();
return writer.write(message);
}
void Logic::_doorCallback(Door::Doormessage doormessage)
{
std::lock_guard<std::mutex> l(_mutex);
_doormessage = doormessage;
}

View File

@ -35,7 +35,7 @@ public:
Response parseRequest(const Json::Value &root);
// Returns the current Token
std::string getClientMessage() const;
std::string getClientMessage();
private:
@ -55,6 +55,8 @@ private:
// stillValid indicates whether the old (previous) token is still valid
void _createNewToken(const bool stillValid);
void _doorCallback(Door::Doormessage doormessage);
const Logger &_logger;
// The door
@ -78,8 +80,10 @@ private:
std::condition_variable _tokenCondition = {};
// stop indicator for the thread
bool _run = true;
// Token mutex
std::mutex _mutex = {};
// General mutex for concurrent data access
mutable std::mutex _mutex = {};
Door::Doormessage _doormessage = {};
// This variable gets notified on token updates
std::condition_variable &_onClientUpdate;