2015-05-11 00:18:22 +02:00
|
|
|
#ifndef DOOR_H
|
|
|
|
#define DOOR_H
|
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2015-09-16 22:59:14 +02:00
|
|
|
#include <functional>
|
2015-08-31 19:01:37 +02:00
|
|
|
#include <mutex>
|
2015-10-01 22:09:55 +02:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/asio/serial_port.hpp>
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
#include "logger.h"
|
2015-09-24 21:12:59 +02:00
|
|
|
#include "doormessage.h"
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
class Door final
|
|
|
|
{
|
2015-05-11 00:18:22 +02:00
|
|
|
public:
|
2015-09-24 18:55:47 +02:00
|
|
|
using DoorCallback = std::function<void(Doormessage)>;
|
|
|
|
enum class State {Unlocked, Locked};
|
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
Door(const std::string &serDev,
|
2016-07-20 20:21:43 +02:00
|
|
|
unsigned int baudrate,
|
|
|
|
const std::string &logfile_scripts);
|
2015-05-11 00:18:22 +02:00
|
|
|
~Door();
|
|
|
|
|
2015-08-30 22:14:35 +02:00
|
|
|
State state() const;
|
2015-09-24 18:55:47 +02:00
|
|
|
void setDoorCallback(DoorCallback doorCallback);
|
2015-08-27 20:30:32 +02:00
|
|
|
|
2015-05-11 00:18:22 +02:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
using Milliseconds = std::chrono::milliseconds;
|
|
|
|
|
|
|
|
const unsigned int _baudrate;
|
|
|
|
|
|
|
|
// To prevent concurrent writes
|
|
|
|
std::mutex _serialMutex = { };
|
|
|
|
|
|
|
|
boost::asio::io_service _ioService = { };
|
|
|
|
boost::asio::serial_port _port;
|
|
|
|
|
2016-07-20 20:21:43 +02:00
|
|
|
const std::string _logfile_scripts;
|
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
std::mutex _stateMutex = { };
|
|
|
|
volatile State _state = { State::Locked };
|
|
|
|
|
|
|
|
std::thread _heartbeatThread = { };
|
|
|
|
std::mutex _heartbeatMutex = { };
|
|
|
|
std::condition_variable _heartbeatCondition = { };
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
std::thread _ioThread = { };
|
|
|
|
void _asyncRead();
|
2015-05-14 19:36:49 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
volatile bool _schnapper = { false };
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
// Indicates if recvBuf contains a valid response from AVR Board
|
|
|
|
volatile bool _byteReady = { false };
|
|
|
|
// Actual response
|
|
|
|
char recvBuf = { };
|
2015-05-14 19:36:49 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
std::condition_variable _receivedCondition = { };
|
|
|
|
std::mutex _receiveLock = { };
|
2015-05-14 19:36:49 +02:00
|
|
|
|
2015-09-24 18:55:47 +02:00
|
|
|
DoorCallback _doorCallback = { };
|
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
Logger &_logger;
|
2015-08-31 19:01:37 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
// Writed command to AVR board
|
2016-07-20 19:59:09 +02:00
|
|
|
bool _writeCMD(char c);
|
2015-09-16 22:59:14 +02:00
|
|
|
// Receives one byte and returns true or returns false on timeout
|
2016-07-20 19:59:09 +02:00
|
|
|
bool _readByte(char &byte, Milliseconds timeout);
|
2016-07-20 20:04:39 +02:00
|
|
|
|
|
|
|
void _exec_and_log(const std::string &filename);
|
2015-05-11 00:18:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|