diff --git a/doorlockd/door.cpp b/doorlockd/door.cpp index 04ca1cd..4b00baf 100644 --- a/doorlockd/door.cpp +++ b/doorlockd/door.cpp @@ -32,6 +32,11 @@ Door &Door::get() return d; } +const Door::State &Door::state() const +{ + return _state; +} + void Door::lock() { _l(LogLevel::notice, "Executing Pre Lock Script"); diff --git a/doorlockd/door.h b/doorlockd/door.h index 9f01234..f8ba736 100644 --- a/doorlockd/door.h +++ b/doorlockd/door.h @@ -28,6 +28,9 @@ public: enum class State {Locked, Unlocked}; + // Current state of the door + const State &state() const; + // Lock the door void lock(); // Unlock the door diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index ab810ca..4a94202 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -117,14 +117,13 @@ out: Logic::Response Logic::_lock() { - if (_state == Door::State::Locked) + if (_door.state() == Door::State::Locked) { _logger(LogLevel::warning, "Unable to lock: already closed"); return AlreadyLocked; } _door.lock(); - _state = Door::State::Locked; _createNewToken(false); return Success; @@ -132,18 +131,17 @@ Logic::Response Logic::_lock() Logic::Response Logic::_unlock() { - _door.unlock(); - _createNewToken(false); + const auto state = _door.state(); + _door.unlock(); + _createNewToken(false); - if (_state == Door::State::Unlocked) - { - _logger(LogLevel::warning, "Unable to unlock: already unlocked"); - return AlreadyUnlocked; - } else { - _state = Door::State::Unlocked; - } + if (state == Door::State::Unlocked) + { + _logger(LogLevel::warning, "Unable to unlock: already unlocked"); + return AlreadyUnlocked; + } - return Success; + return Success; } bool Logic::_checkToken(const string &strToken) diff --git a/doorlockd/logic.h b/doorlockd/logic.h index 73c5899..ef264b1 100644 --- a/doorlockd/logic.h +++ b/doorlockd/logic.h @@ -97,8 +97,6 @@ private: const std::string _bindDN; // Prefix of the website const std::string _webPrefix; - - Door::State _state = { Door::State::Locked }; }; #endif