1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +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/clientmessage.h
lib/door.cpp lib/door.cpp
lib/door.h lib/door.h
lib/doormessage.cpp
lib/doormessage.h lib/doormessage.h
lib/logger.cpp lib/logger.cpp
lib/logger.h lib/logger.h

View File

@ -46,7 +46,7 @@ static void onDoorlockUpdate(const Clientmessage &msg)
l("Received message", LogLevel::info); l("Received message", LogLevel::info);
l((std::string)" token: " + msg.token(), l((std::string)" token: " + msg.token(),
LogLevel::info); LogLevel::info);
l((std::string)" open: " + std::to_string(doormessage.isOpen), l((std::string)" open: " + std::to_string(msg.isOpen()),
LogLevel::info); LogLevel::info);
l((std::string)" button lock: " + std::to_string(doormessage.isLockButton), l((std::string)" button lock: " + std::to_string(doormessage.isLockButton),
LogLevel::info); LogLevel::info);

View File

@ -9,8 +9,10 @@ const std::string Clientmessage::_emergencyUnlockKey = "emergencyUnlock";
const std::string Clientmessage::_isOpenKey = "isOpen"; const std::string Clientmessage::_isOpenKey = "isOpen";
Clientmessage::Clientmessage(std::string token, Clientmessage::Clientmessage(std::string token,
bool isOpen,
Doormessage doormessage) : Doormessage doormessage) :
_token(token), _token(token),
_isOpen(isOpen),
_doormessage(doormessage) _doormessage(doormessage)
{ {
} }
@ -24,7 +26,7 @@ std::string Clientmessage::toJson() const
message[_unlockButtonKey] = _doormessage.isUnlockButton; message[_unlockButtonKey] = _doormessage.isUnlockButton;
message[_lockButtonKey] = _doormessage.isLockButton; message[_lockButtonKey] = _doormessage.isLockButton;
message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock; message[_emergencyUnlockKey] = _doormessage.isEmergencyUnlock;
message[_isOpenKey] = _doormessage.isOpen; message[_isOpenKey] = _isOpen;
return writer.write(message); return writer.write(message);
} }
@ -42,6 +44,7 @@ const Doormessage& Clientmessage::doormessage() const
Clientmessage Clientmessage::fromJson(const Json::Value &root) Clientmessage Clientmessage::fromJson(const Json::Value &root)
{ {
std::string token; std::string token;
bool isOpen;
Doormessage doormessage; Doormessage doormessage;
try { try {
@ -49,13 +52,13 @@ Clientmessage Clientmessage::fromJson(const Json::Value &root)
doormessage.isLockButton = getJsonOrFail<bool>(root, _lockButtonKey); doormessage.isLockButton = getJsonOrFail<bool>(root, _lockButtonKey);
doormessage.isUnlockButton = getJsonOrFail<bool>(root, _unlockButtonKey); doormessage.isUnlockButton = getJsonOrFail<bool>(root, _unlockButtonKey);
doormessage.isEmergencyUnlock = getJsonOrFail<bool>(root, _emergencyUnlockKey); doormessage.isEmergencyUnlock = getJsonOrFail<bool>(root, _emergencyUnlockKey);
doormessage.isOpen = getJsonOrFail<bool>(root, _isOpenKey); isOpen = getJsonOrFail<bool>(root, _isOpenKey);
} }
catch (const std::exception &ex) { catch (const std::exception &ex) {
throw Response(Response::Code::JsonError, ex.what()); throw Response(Response::Code::JsonError, ex.what());
} }
return Clientmessage(token, doormessage); return Clientmessage(token, isOpen, doormessage);
} }
Clientmessage Clientmessage::fromString(const std::string &string) Clientmessage Clientmessage::fromString(const std::string &string)
@ -69,3 +72,8 @@ Clientmessage Clientmessage::fromString(const std::string &string)
return fromJson(root); return fromJson(root);
} }
bool Clientmessage::isOpen() const
{
return _isOpen;
}

View File

@ -11,6 +11,7 @@ class Clientmessage
public: public:
Clientmessage(std::string token, Clientmessage(std::string token,
bool isOpen,
Doormessage doormessage); Doormessage doormessage);
static Clientmessage fromJson(const Json::Value &root); static Clientmessage fromJson(const Json::Value &root);
@ -18,12 +19,14 @@ public:
std::string toJson() const; std::string toJson() const;
const std::string& token() const; const std::string& token() const;
bool isOpen() const;
const Doormessage& doormessage() const; const Doormessage& doormessage() const;
private: private:
std::string _token; const std::string _token;
Doormessage _doormessage; const bool _isOpen;
const Doormessage _doormessage;
static const std::string _tokenKey; static const std::string _tokenKey;
const static std::string _unlockButtonKey; const static std::string _unlockButtonKey;

View File

@ -55,9 +55,6 @@ 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;
m.isOpen = _state == Door::State::Unlocked;
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()
@ -78,8 +75,7 @@ void Door::_asyncRead()
// 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) { if (_doorCallback) {
m.isUnlockButton = true; _doorCallback(Doormessage(true, false, false));
_doorCallback(m);
} }
goto out; goto out;
} else if (recvBuf == DOOR_BUTTON_LOCK) { } else if (recvBuf == DOOR_BUTTON_LOCK) {
@ -87,16 +83,14 @@ void Door::_asyncRead()
_logger(LogLevel::notice, "Locking..."); _logger(LogLevel::notice, "Locking...");
lock(); lock();
if (_doorCallback) { if (_doorCallback) {
m.isLockButton = true; _doorCallback(Doormessage(false, true, false));
_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) { if (_doorCallback) {
m.isEmergencyUnlock = true; _doorCallback(Doormessage(false, false, true));
_doorCallback(m);
} }
goto out; 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 struct Doormessage
{ {
bool isOpen = { false }; Doormessage(bool isUnlockButton,
bool isLockButton,
bool isEmergencyUnlock);
Doormessage();
bool isUnlockButton = { false }; bool isUnlockButton = { false };
bool isLockButton = { false }; bool isLockButton = { false };
bool isEmergencyUnlock = { false }; bool isEmergencyUnlock = { false };

View File

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