2015-09-16 23:48:44 +02:00
|
|
|
#include "config.h"
|
2015-09-16 22:59:14 +02:00
|
|
|
#include "door.h"
|
2015-05-14 19:36:49 +02:00
|
|
|
|
2015-09-28 17:07:21 +02:00
|
|
|
#include "../../doorcmds.h"
|
2015-09-17 14:53:19 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
Door::Door(const std::string &serDev,
|
2016-07-20 20:21:43 +02:00
|
|
|
unsigned int baudrate,
|
|
|
|
const std::string &logfile_scripts) :
|
2015-09-16 22:59:14 +02:00
|
|
|
_baudrate(baudrate),
|
|
|
|
_port(_ioService, serDev),
|
2016-07-20 20:21:43 +02:00
|
|
|
_logfile_scripts(logfile_scripts),
|
2015-09-16 22:59:14 +02:00
|
|
|
_logger(Logger::get())
|
|
|
|
{
|
|
|
|
// Configure serial port
|
|
|
|
_port.set_option(boost::asio::serial_port_base::baud_rate(baudrate));
|
|
|
|
_port.set_option(boost::asio::serial_port_base::character_size(8));
|
|
|
|
_port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
|
|
|
|
_port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
|
|
|
|
_port.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_asyncRead();
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_ioThread = std::thread([this] () {
|
|
|
|
_ioService.run();
|
|
|
|
});
|
2015-05-11 00:18:22 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
// TODO Ping device
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Door::~Door()
|
|
|
|
{
|
2015-05-14 19:36:49 +02:00
|
|
|
lock();
|
2015-09-16 22:59:14 +02:00
|
|
|
|
|
|
|
_ioService.stop();
|
|
|
|
_ioService.reset();
|
|
|
|
|
|
|
|
_port.cancel();
|
|
|
|
_port.close();
|
|
|
|
|
|
|
|
_ioThread.join();
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:59:09 +02:00
|
|
|
bool Door::_readByte(char &byte, std::chrono::milliseconds timeout)
|
2015-05-11 00:18:22 +02:00
|
|
|
{
|
2015-09-16 22:59:14 +02:00
|
|
|
std::unique_lock<std::mutex> lock(_receiveLock);
|
|
|
|
_receivedCondition.wait_for(lock, timeout);
|
|
|
|
if (_byteReady) {
|
|
|
|
byte = recvBuf;
|
|
|
|
_byteReady = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Door::_asyncRead()
|
|
|
|
{
|
|
|
|
_port.async_read_some(
|
|
|
|
boost::asio::buffer(&recvBuf, sizeof(recvBuf)),
|
|
|
|
[this] (const boost::system::error_code &ec, size_t bytes_transferred) {
|
|
|
|
if (ec) {
|
|
|
|
// Operation canceled occurs on system shutdown
|
|
|
|
// So we return without invoking an additional asyncRead()
|
|
|
|
if (ec == boost::system::errc::operation_canceled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_logger(LogLevel::error, "Serialport error: %s", ec.message().c_str());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_transferred != 1) {
|
|
|
|
_logger(LogLevel::error, "Fatal serial error");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-09-17 14:53:19 +02:00
|
|
|
if (recvBuf == DOOR_BUTTON_UNLOCK) {
|
2015-09-17 14:50:30 +02:00
|
|
|
// In case that someone pushed the unlock button - just log it.
|
|
|
|
// No further actions required
|
2015-09-16 22:59:14 +02:00
|
|
|
_logger(LogLevel::notice, "Someone pushed the unlock button");
|
2015-09-24 18:55:47 +02:00
|
|
|
if (_doorCallback) {
|
2015-10-05 23:30:25 +02:00
|
|
|
_doorCallback(Doormessage(true, false, false));
|
2015-09-24 18:55:47 +02:00
|
|
|
}
|
2015-09-17 14:50:30 +02:00
|
|
|
goto out;
|
2015-09-17 14:53:19 +02:00
|
|
|
} else if (recvBuf == DOOR_BUTTON_LOCK) {
|
2015-09-16 22:59:14 +02:00
|
|
|
_logger(LogLevel::notice, "Someone pushed the lock button");
|
|
|
|
_logger(LogLevel::notice, "Locking...");
|
|
|
|
lock();
|
2015-09-24 18:55:47 +02:00
|
|
|
if (_doorCallback) {
|
2015-10-05 23:30:25 +02:00
|
|
|
_doorCallback(Doormessage(false, true, false));
|
2015-09-24 18:55:47 +02:00
|
|
|
}
|
2015-09-16 22:59:14 +02:00
|
|
|
goto out;
|
2015-09-17 14:53:19 +02:00
|
|
|
} else if (recvBuf == DOOR_EMERGENCY_UNLOCK) {
|
2015-09-17 14:50:30 +02:00
|
|
|
_logger(LogLevel::warning, "Someone did an emergency unlock!");
|
2016-07-20 20:04:39 +02:00
|
|
|
_exec_and_log(EMERGENCY_UNLOCK_SCRIPT);
|
2015-09-24 18:55:47 +02:00
|
|
|
if (_doorCallback) {
|
2015-10-05 23:30:25 +02:00
|
|
|
_doorCallback(Doormessage(false, false, true));
|
2015-09-24 18:55:47 +02:00
|
|
|
}
|
2015-09-17 14:50:30 +02:00
|
|
|
goto out;
|
2015-09-16 22:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_byteReady = true;
|
|
|
|
_receivedCondition.notify_one();
|
|
|
|
|
|
|
|
out:
|
|
|
|
_asyncRead();
|
|
|
|
});
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 22:14:35 +02:00
|
|
|
Door::State Door::state() const
|
2015-08-27 20:30:32 +02:00
|
|
|
{
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
2015-05-11 00:18:22 +02:00
|
|
|
void Door::lock()
|
|
|
|
{
|
2015-09-16 22:59:14 +02:00
|
|
|
_stateMutex.lock();
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 23:48:44 +02:00
|
|
|
|
|
|
|
_logger(LogLevel::notice, "Executing Pre Lock Script");
|
2016-07-20 20:04:39 +02:00
|
|
|
_exec_and_log(PRE_LOCK_SCRIPT);
|
2015-09-16 23:48:44 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
if (_state == State::Locked) {
|
|
|
|
_stateMutex.unlock();
|
2015-09-16 23:48:44 +02:00
|
|
|
_logger(LogLevel::info, "Door already closed");
|
|
|
|
goto out;
|
2015-05-14 19:36:49 +02:00
|
|
|
}
|
2015-05-18 22:22:08 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_state = State::Locked;
|
|
|
|
_stateMutex.unlock();
|
|
|
|
_heartbeatCondition.notify_one();
|
|
|
|
_heartbeatThread.join();
|
2015-09-16 23:48:44 +02:00
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
_logger(LogLevel::notice , "Door closed");
|
2015-09-16 23:48:44 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
_logger(LogLevel::notice, "Executing Post Lock Script");
|
2016-07-20 20:04:39 +02:00
|
|
|
_exec_and_log(POST_LOCK_SCRIPT);
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Door::unlock()
|
|
|
|
{
|
2015-09-16 22:59:14 +02:00
|
|
|
_stateMutex.lock();
|
2015-05-14 19:36:49 +02:00
|
|
|
_schnapper = true;
|
|
|
|
|
2015-09-16 23:48:44 +02:00
|
|
|
_logger(LogLevel::notice, "Executing Pre Unlock Script");
|
2016-07-20 20:04:39 +02:00
|
|
|
_exec_and_log(PRE_UNLOCK_SCRIPT);
|
2015-09-16 23:48:44 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
if(_state == State::Unlocked) {
|
|
|
|
_stateMutex.unlock();
|
2015-09-16 23:48:44 +02:00
|
|
|
_logger(LogLevel::info, "Door already opened");
|
|
|
|
goto out;
|
2015-05-14 19:36:49 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
_state = State::Unlocked;
|
2015-09-16 22:59:14 +02:00
|
|
|
_stateMutex.unlock();
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_heartbeatThread = std::thread([this] () {
|
|
|
|
std::unique_lock<std::mutex> lock(_heartbeatMutex);
|
2015-05-20 22:42:20 +02:00
|
|
|
|
2015-05-21 13:35:30 +02:00
|
|
|
while (_state == State::Unlocked) {
|
2015-09-16 22:59:14 +02:00
|
|
|
if (_state == State::Unlocked) {
|
2016-07-20 19:59:09 +02:00
|
|
|
_writeCMD(DOOR_CMD_UNLOCK);
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
if (_schnapper) {
|
|
|
|
_schnapper = false;
|
2016-07-20 19:59:09 +02:00
|
|
|
_writeCMD(DOOR_CMD_SCHNAPER);
|
2015-05-20 22:42:20 +02:00
|
|
|
}
|
2015-05-14 19:36:49 +02:00
|
|
|
}
|
2015-05-21 13:35:30 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_heartbeatCondition.wait_for(lock, Milliseconds(400));
|
2015-05-14 19:36:49 +02:00
|
|
|
}
|
2016-07-20 19:59:09 +02:00
|
|
|
_writeCMD(DOOR_CMD_LOCK);
|
2015-05-14 19:36:49 +02:00
|
|
|
});
|
2015-09-16 23:48:44 +02:00
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
_logger(LogLevel::notice, "Door opened");
|
2015-09-16 23:48:44 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
_logger(LogLevel::notice, "Executing Post Unlock Script");
|
2016-07-20 20:04:39 +02:00
|
|
|
_exec_and_log(POST_UNLOCK_SCRIPT);
|
2015-09-16 22:59:14 +02:00
|
|
|
}
|
2015-05-14 19:36:49 +02:00
|
|
|
|
2016-07-20 19:59:09 +02:00
|
|
|
bool Door::_writeCMD(char c)
|
2015-09-16 22:59:14 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(_serialMutex);
|
2015-05-25 15:47:44 +02:00
|
|
|
|
2015-09-16 22:59:14 +02:00
|
|
|
_port.write_some(boost::asio::buffer(&c, sizeof(c)));
|
|
|
|
char response;
|
2016-07-20 19:59:09 +02:00
|
|
|
if (_readByte(response, Milliseconds(100)))
|
2015-09-16 22:59:14 +02:00
|
|
|
{
|
|
|
|
if (c != response) {
|
2015-09-17 14:43:36 +02:00
|
|
|
_logger(LogLevel::error, "Sent command '%c' but got response '%c'", c, response);
|
2015-09-16 22:59:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_logger(LogLevel::error, "Sent Serial command, but got no response!");
|
|
|
|
return false;
|
2015-05-11 00:18:22 +02:00
|
|
|
}
|
2015-09-24 18:55:47 +02:00
|
|
|
|
|
|
|
void Door::setDoorCallback(DoorCallback doorCallback)
|
|
|
|
{
|
|
|
|
_doorCallback = doorCallback;
|
|
|
|
}
|
2016-07-20 20:04:39 +02:00
|
|
|
|
|
|
|
void Door::_exec_and_log(const std::string &filename)
|
|
|
|
{
|
2016-07-20 20:33:54 +02:00
|
|
|
const std::string cmd = "nohup " + filename + " &>> " + _logfile_scripts;
|
2016-07-20 20:04:39 +02:00
|
|
|
::system(cmd.c_str());
|
|
|
|
}
|