1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-05-29 02:45:07 +02:00

Added door callbacks

This commit is contained in:
Ralf Ramsauer 2015-09-24 18:55:47 +02:00
parent 8b4be48811
commit 4f22e652e8
2 changed files with 33 additions and 2 deletions

View File

@ -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;
}

View File

@ -16,13 +16,23 @@ class Door final
{
public:
struct Doormessage {
bool isUnlockButton = { false };
bool isLockButton = { false };
bool isEmergencyUnlock = { false };
};
using DoorCallback = std::function<void(Doormessage)>;
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