mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 18:34:25 +01:00
Added door callbacks
This commit is contained in:
parent
8b4be48811
commit
4f22e652e8
@ -55,6 +55,8 @@ void Door::_asyncRead()
|
|||||||
_port.async_read_some(
|
_port.async_read_some(
|
||||||
boost::asio::buffer(&recvBuf, sizeof(recvBuf)),
|
boost::asio::buffer(&recvBuf, sizeof(recvBuf)),
|
||||||
[this] (const boost::system::error_code &ec, size_t bytes_transferred) {
|
[this] (const boost::system::error_code &ec, size_t bytes_transferred) {
|
||||||
|
Doormessage m;
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
// Operation canceled occurs on system shutdown
|
// Operation canceled occurs on system shutdown
|
||||||
// So we return without invoking an additional asyncRead()
|
// 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.
|
// In case that someone pushed the unlock button - just log it.
|
||||||
// No further actions required
|
// No further actions required
|
||||||
_logger(LogLevel::notice, "Someone pushed the unlock button");
|
_logger(LogLevel::notice, "Someone pushed the unlock button");
|
||||||
|
if (_doorCallback) {
|
||||||
|
m.isUnlockButton = true;
|
||||||
|
_doorCallback(m);
|
||||||
|
}
|
||||||
goto out;
|
goto out;
|
||||||
} else if (recvBuf == DOOR_BUTTON_LOCK) {
|
} else if (recvBuf == DOOR_BUTTON_LOCK) {
|
||||||
_logger(LogLevel::notice, "Someone pushed the lock button");
|
_logger(LogLevel::notice, "Someone pushed the lock button");
|
||||||
_logger(LogLevel::notice, "Locking...");
|
_logger(LogLevel::notice, "Locking...");
|
||||||
lock();
|
lock();
|
||||||
|
if (_doorCallback) {
|
||||||
|
m.isLockButton = true;
|
||||||
|
_doorCallback(m);
|
||||||
|
}
|
||||||
goto out;
|
goto out;
|
||||||
} else if (recvBuf == DOOR_EMERGENCY_UNLOCK) {
|
} else if (recvBuf == DOOR_EMERGENCY_UNLOCK) {
|
||||||
_logger(LogLevel::warning, "Someone did an emergency unlock!");
|
_logger(LogLevel::warning, "Someone did an emergency unlock!");
|
||||||
system(EMERGENCY_UNLOCK_SCRIPT);
|
system(EMERGENCY_UNLOCK_SCRIPT);
|
||||||
|
if (_doorCallback) {
|
||||||
|
m.isEmergencyUnlock = true;
|
||||||
|
_doorCallback(m);
|
||||||
|
}
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,3 +198,8 @@ bool Door::writeCMD(char c)
|
|||||||
_logger(LogLevel::error, "Sent Serial command, but got no response!");
|
_logger(LogLevel::error, "Sent Serial command, but got no response!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Door::setDoorCallback(DoorCallback doorCallback)
|
||||||
|
{
|
||||||
|
_doorCallback = doorCallback;
|
||||||
|
}
|
||||||
|
@ -16,13 +16,23 @@ class Door final
|
|||||||
{
|
{
|
||||||
public:
|
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,
|
Door(const std::string &serDev,
|
||||||
unsigned int baudrate);
|
unsigned int baudrate);
|
||||||
~Door();
|
~Door();
|
||||||
|
|
||||||
enum class State {Unlocked, Locked};
|
|
||||||
|
|
||||||
State state() const;
|
State state() const;
|
||||||
|
void setDoorCallback(DoorCallback doorCallback);
|
||||||
|
|
||||||
void lock();
|
void lock();
|
||||||
void unlock();
|
void unlock();
|
||||||
@ -59,6 +69,8 @@ private:
|
|||||||
std::condition_variable _receivedCondition = { };
|
std::condition_variable _receivedCondition = { };
|
||||||
std::mutex _receiveLock = { };
|
std::mutex _receiveLock = { };
|
||||||
|
|
||||||
|
DoorCallback _doorCallback = { };
|
||||||
|
|
||||||
const Logger &_logger;
|
const Logger &_logger;
|
||||||
|
|
||||||
// Writed command to AVR board
|
// Writed command to AVR board
|
||||||
|
Loading…
Reference in New Issue
Block a user