1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-10-02 18:52:27 +02:00
doorlockd-mirror/doorlockd/lib/logic.h

102 lines
2.7 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef LOGIC_H
#define LOGIC_H
2015-05-12 17:35:57 +02:00
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
2015-05-11 00:18:22 +02:00
#include "config.h"
#include "clientmessage.h"
2015-05-11 00:18:22 +02:00
#include "door.h"
#include "logger.h"
#include "request.h"
#include "response.h"
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
/* The "Logic" class
*
* This class is initilized by all settings.
*
* It handles incoming requests and allows modifications of the door
2015-05-21 13:35:30 +02:00
*/
2015-05-11 00:18:22 +02:00
class Logic
{
public:
2015-05-13 16:40:30 +02:00
Logic(const std::chrono::seconds tokenTimeout,
2015-05-24 19:15:47 +02:00
const std::string &ldapUri,
2015-05-13 16:40:30 +02:00
const std::string &bindDN,
const std::string &webPrefix,
const std::string &serDev,
2015-09-23 15:33:27 +02:00
const unsigned int baudrate,
2015-09-24 18:15:05 +02:00
std::condition_variable &onClientUpdate);
2015-05-11 00:18:22 +02:00
~Logic();
2015-05-21 13:35:30 +02:00
// Parse incoming JSON Requests
Response request(const Request &request);
// Send direct command to door without credential checks
enum class DoorCommand { Lock, Unlock };
Response processDoor(const DoorCommand &doorCommand);
2015-05-11 00:18:22 +02:00
// Returns the current Token
Clientmessage getClientMessage();
2015-05-11 00:18:22 +02:00
private:
2015-05-21 13:35:30 +02:00
// Internal lock wrapper
2015-05-12 15:59:04 +02:00
Response _lock();
2015-05-21 13:35:30 +02:00
// Internal unlock wrapper
2015-05-12 15:59:04 +02:00
Response _unlock();
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// Checks if the incoming token is valid
Response _checkToken(std::string token) const;
2015-05-21 13:35:30 +02:00
// Checks if incoming credentials against LDAP
2015-05-13 16:40:30 +02:00
Response _checkLDAP(const std::string &user,
const std::string &password);
2015-05-21 13:35:30 +02:00
// Creates a new random token and draws it on the epaper.
// stillValid indicates whether the old (previous) token is still valid
2015-05-12 17:35:57 +02:00
void _createNewToken(const bool stillValid);
2015-05-11 00:18:22 +02:00
void _doorCallback(Doormessage doormessage);
2015-09-24 18:57:32 +02:00
Logger &_logger;
2015-05-21 13:35:30 +02:00
// The door
Door _door;
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// The current token
std::string _curToken = { "0000000000000000" };
2015-05-21 13:35:30 +02:00
// The previous token
std::string _prevToken = { "0000000000000000" };
2015-05-21 13:35:30 +02:00
// Indicates whether the previous token is valid
bool _prevValid = { false };
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// Tokens are refreshed all tokenTimout seconds
2015-05-12 17:35:57 +02:00
const std::chrono::seconds _tokenTimeout;
2015-05-21 13:35:30 +02:00
// Thread for asynchronosly updating tokens
std::thread _tokenUpdater = {};
// Thread can be force-triggered for updates using the condition variable
std::condition_variable _tokenCondition = {};
// stop indicator for the thread
bool _run = true;
2015-09-24 18:57:32 +02:00
// General mutex for concurrent data access
mutable std::mutex _mutex = {};
Doormessage _doormessage = {};
2015-05-21 13:35:30 +02:00
// This variable gets notified on token updates
2015-09-24 18:15:05 +02:00
std::condition_variable &_onClientUpdate;
2015-05-21 13:35:30 +02:00
// The URI of the ldap server
2015-05-24 19:15:47 +02:00
const std::string _ldapUri;
2015-05-21 13:35:30 +02:00
// LDAP bindDN
2015-05-13 16:40:30 +02:00
const std::string _bindDN;
2015-05-21 13:35:30 +02:00
// Prefix of the website
2015-05-13 16:40:30 +02:00
const std::string _webPrefix;
2015-05-11 00:18:22 +02:00
};
#endif