From 70a4b3de032ffa3e01bba86eac5b7133a19792de Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Mon, 5 Oct 2015 23:30:25 +0200 Subject: [PATCH] Bugfix & logical change - Improved doormessage - Moved isOpen from Doormessage to Logic --- doorlockd/CMakeLists.txt | 1 + doorlockd/client/doorlock-client.cpp | 2 +- doorlockd/lib/clientmessage.cpp | 14 +++++++++++--- doorlockd/lib/clientmessage.h | 7 +++++-- doorlockd/lib/door.cpp | 12 +++--------- doorlockd/lib/doormessage.cpp | 12 ++++++++++++ doorlockd/lib/doormessage.h | 7 ++++++- doorlockd/lib/logic.cpp | 1 + 8 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 doorlockd/lib/doormessage.cpp diff --git a/doorlockd/CMakeLists.txt b/doorlockd/CMakeLists.txt index eea68c5..83529f1 100644 --- a/doorlockd/CMakeLists.txt +++ b/doorlockd/CMakeLists.txt @@ -62,6 +62,7 @@ set(LIBDOORLOCK_SRCS lib/clientmessage.h lib/door.cpp lib/door.h + lib/doormessage.cpp lib/doormessage.h lib/logger.cpp lib/logger.h diff --git a/doorlockd/client/doorlock-client.cpp b/doorlockd/client/doorlock-client.cpp index b245584..a12de78 100644 --- a/doorlockd/client/doorlock-client.cpp +++ b/doorlockd/client/doorlock-client.cpp @@ -46,7 +46,7 @@ static void onDoorlockUpdate(const Clientmessage &msg) l("Received message", LogLevel::info); l((std::string)" token: " + msg.token(), LogLevel::info); - l((std::string)" open: " + std::to_string(doormessage.isOpen), + l((std::string)" open: " + std::to_string(msg.isOpen()), LogLevel::info); l((std::string)" button lock: " + std::to_string(doormessage.isLockButton), LogLevel::info); diff --git a/doorlockd/lib/clientmessage.cpp b/doorlockd/lib/clientmessage.cpp index ccf1d4a..d6321cd 100644 --- a/doorlockd/lib/clientmessage.cpp +++ b/doorlockd/lib/clientmessage.cpp @@ -9,8 +9,10 @@ const std::string Clientmessage::_emergencyUnlockKey = "emergencyUnlock"; const std::string Clientmessage::_isOpenKey = "isOpen"; Clientmessage::Clientmessage(std::string token, + bool isOpen, Doormessage doormessage) : _token(token), + _isOpen(isOpen), _doormessage(doormessage) { } @@ -24,7 +26,7 @@ std::string Clientmessage::toJson() const message[_unlockButtonKey] = _doormessage.isUnlockButton; message[_lockButtonKey] = _doormessage.isLockButton; message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock; - message[_isOpenKey] = _doormessage.isOpen; + message[_isOpenKey] = _isOpen; return writer.write(message); } @@ -42,6 +44,7 @@ const Doormessage& Clientmessage::doormessage() const Clientmessage Clientmessage::fromJson(const Json::Value &root) { std::string token; + bool isOpen; Doormessage doormessage; try { @@ -49,13 +52,13 @@ Clientmessage Clientmessage::fromJson(const Json::Value &root) doormessage.isLockButton = getJsonOrFail(root, _lockButtonKey); doormessage.isUnlockButton = getJsonOrFail(root, _unlockButtonKey); doormessage.isEmergencyUnlock = getJsonOrFail(root, _emergencyUnlockKey); - doormessage.isOpen = getJsonOrFail(root, _isOpenKey); + isOpen = getJsonOrFail(root, _isOpenKey); } catch (const std::exception &ex) { throw Response(Response::Code::JsonError, ex.what()); } - return Clientmessage(token, doormessage); + return Clientmessage(token, isOpen, doormessage); } Clientmessage Clientmessage::fromString(const std::string &string) @@ -69,3 +72,8 @@ Clientmessage Clientmessage::fromString(const std::string &string) return fromJson(root); } + +bool Clientmessage::isOpen() const +{ + return _isOpen; +} diff --git a/doorlockd/lib/clientmessage.h b/doorlockd/lib/clientmessage.h index 3bc88ff..2dc643c 100644 --- a/doorlockd/lib/clientmessage.h +++ b/doorlockd/lib/clientmessage.h @@ -11,6 +11,7 @@ class Clientmessage public: Clientmessage(std::string token, + bool isOpen, Doormessage doormessage); static Clientmessage fromJson(const Json::Value &root); @@ -18,12 +19,14 @@ public: std::string toJson() const; const std::string& token() const; + bool isOpen() const; const Doormessage& doormessage() const; private: - std::string _token; - Doormessage _doormessage; + const std::string _token; + const bool _isOpen; + const Doormessage _doormessage; static const std::string _tokenKey; const static std::string _unlockButtonKey; diff --git a/doorlockd/lib/door.cpp b/doorlockd/lib/door.cpp index 804e66c..9b19c64 100644 --- a/doorlockd/lib/door.cpp +++ b/doorlockd/lib/door.cpp @@ -55,9 +55,6 @@ void Door::_asyncRead() _port.async_read_some( boost::asio::buffer(&recvBuf, sizeof(recvBuf)), [this] (const boost::system::error_code &ec, size_t bytes_transferred) { - Doormessage m; - m.isOpen = _state == Door::State::Unlocked; - if (ec) { // Operation canceled occurs on system shutdown // So we return without invoking an additional asyncRead() @@ -78,8 +75,7 @@ void Door::_asyncRead() // No further actions required _logger(LogLevel::notice, "Someone pushed the unlock button"); if (_doorCallback) { - m.isUnlockButton = true; - _doorCallback(m); + _doorCallback(Doormessage(true, false, false)); } goto out; } else if (recvBuf == DOOR_BUTTON_LOCK) { @@ -87,16 +83,14 @@ void Door::_asyncRead() _logger(LogLevel::notice, "Locking..."); lock(); if (_doorCallback) { - m.isLockButton = true; - _doorCallback(m); + _doorCallback(Doormessage(false, true, false)); } goto out; } else if (recvBuf == DOOR_EMERGENCY_UNLOCK) { _logger(LogLevel::warning, "Someone did an emergency unlock!"); system(EMERGENCY_UNLOCK_SCRIPT); if (_doorCallback) { - m.isEmergencyUnlock = true; - _doorCallback(m); + _doorCallback(Doormessage(false, false, true)); } goto out; } diff --git a/doorlockd/lib/doormessage.cpp b/doorlockd/lib/doormessage.cpp new file mode 100644 index 0000000..b0f149e --- /dev/null +++ b/doorlockd/lib/doormessage.cpp @@ -0,0 +1,12 @@ +#include "doormessage.h" + +Doormessage::Doormessage() +{ +} + +Doormessage::Doormessage(bool isUnlockButton, bool isLockButton, bool isEmergencyUnlock) : + isUnlockButton(isUnlockButton), + isLockButton(isLockButton), + isEmergencyUnlock(isEmergencyUnlock) +{ +} diff --git a/doorlockd/lib/doormessage.h b/doorlockd/lib/doormessage.h index 2231c7a..d5207b6 100644 --- a/doorlockd/lib/doormessage.h +++ b/doorlockd/lib/doormessage.h @@ -3,7 +3,12 @@ struct Doormessage { - bool isOpen = { false }; + Doormessage(bool isUnlockButton, + bool isLockButton, + bool isEmergencyUnlock); + + Doormessage(); + bool isUnlockButton = { false }; bool isLockButton = { false }; bool isEmergencyUnlock = { false }; diff --git a/doorlockd/lib/logic.cpp b/doorlockd/lib/logic.cpp index 56feb1b..f15c52c 100644 --- a/doorlockd/lib/logic.cpp +++ b/doorlockd/lib/logic.cpp @@ -243,6 +243,7 @@ Clientmessage Logic::getClientMessage() { std::lock_guard l(_mutex); Clientmessage retval(_webPrefix + _curToken, + _door.state() == Door::State::Unlocked, _doormessage); // Reset doormessage