1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 10:24:26 +01:00

Bugfix & logical change

- Improved doormessage
- Moved isOpen from Doormessage to Logic
This commit is contained in:
Ralf Ramsauer 2015-10-05 23:30:25 +02:00
parent a113fbae26
commit 70a4b3de03
8 changed files with 40 additions and 16 deletions

View File

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

View File

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

View File

@ -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<bool>(root, _lockButtonKey);
doormessage.isUnlockButton = getJsonOrFail<bool>(root, _unlockButtonKey);
doormessage.isEmergencyUnlock = getJsonOrFail<bool>(root, _emergencyUnlockKey);
doormessage.isOpen = getJsonOrFail<bool>(root, _isOpenKey);
isOpen = getJsonOrFail<bool>(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;
}

View File

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

View File

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

View File

@ -0,0 +1,12 @@
#include "doormessage.h"
Doormessage::Doormessage()
{
}
Doormessage::Doormessage(bool isUnlockButton, bool isLockButton, bool isEmergencyUnlock) :
isUnlockButton(isUnlockButton),
isLockButton(isLockButton),
isEmergencyUnlock(isEmergencyUnlock)
{
}

View File

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

View File

@ -243,6 +243,7 @@ Clientmessage Logic::getClientMessage()
{
std::lock_guard<std::mutex> l(_mutex);
Clientmessage retval(_webPrefix + _curToken,
_door.state() == Door::State::Unlocked,
_doormessage);
// Reset doormessage