From 3de8855dacf7468a890594bf589487b838573f74 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Thu, 24 Sep 2015 18:57:32 +0200 Subject: [PATCH] Added Client messages --- doorlockd/logic.cpp | 27 +++++++++++++++++++++++++-- doorlockd/logic.h | 10 +++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index 4f6b876..62cb269 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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 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 l(_mutex); + _doormessage = doormessage; } diff --git a/doorlockd/logic.h b/doorlockd/logic.h index c5e0d6b..735481b 100644 --- a/doorlockd/logic.h +++ b/doorlockd/logic.h @@ -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;