diff --git a/doorlockd/door.cpp b/doorlockd/door.cpp index f12d1a8..336dfc8 100644 --- a/doorlockd/door.cpp +++ b/doorlockd/door.cpp @@ -55,6 +55,8 @@ 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; + if (ec) { // Operation canceled occurs on system shutdown // So we return without invoking an additional asyncRead() @@ -74,15 +76,27 @@ void Door::_asyncRead() // In case that someone pushed the unlock button - just log it. // No further actions required _logger(LogLevel::notice, "Someone pushed the unlock button"); + if (_doorCallback) { + m.isUnlockButton = true; + _doorCallback(m); + } goto out; } else if (recvBuf == DOOR_BUTTON_LOCK) { _logger(LogLevel::notice, "Someone pushed the lock button"); _logger(LogLevel::notice, "Locking..."); lock(); + if (_doorCallback) { + m.isLockButton = true; + _doorCallback(m); + } 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); + } goto out; } @@ -184,3 +198,8 @@ bool Door::writeCMD(char c) _logger(LogLevel::error, "Sent Serial command, but got no response!"); return false; } + +void Door::setDoorCallback(DoorCallback doorCallback) +{ + _doorCallback = doorCallback; +} diff --git a/doorlockd/door.h b/doorlockd/door.h index db1ffa5..f1a237b 100644 --- a/doorlockd/door.h +++ b/doorlockd/door.h @@ -16,13 +16,23 @@ class Door final { public: + struct Doormessage { + bool isUnlockButton = { false }; + bool isLockButton = { false }; + bool isEmergencyUnlock = { false }; + }; + + using DoorCallback = std::function; + enum class State {Unlocked, Locked}; + + + Door(const std::string &serDev, unsigned int baudrate); ~Door(); - enum class State {Unlocked, Locked}; - State state() const; + void setDoorCallback(DoorCallback doorCallback); void lock(); void unlock(); @@ -59,6 +69,8 @@ private: std::condition_variable _receivedCondition = { }; std::mutex _receiveLock = { }; + DoorCallback _doorCallback = { }; + const Logger &_logger; // Writed command to AVR board