2015-09-25 00:45:13 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2015-09-25 13:11:36 +02:00
|
|
|
#include <thread>
|
2015-09-25 00:45:13 +02:00
|
|
|
|
2015-10-11 19:33:19 +02:00
|
|
|
#include <ao/ao.h>
|
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2015-09-30 15:21:41 +02:00
|
|
|
#include <QApplication>
|
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
#include "config.h"
|
2015-09-29 14:33:53 +02:00
|
|
|
|
|
|
|
#include "../lib/clientmessage.h"
|
|
|
|
#include "../lib/logger.h"
|
|
|
|
#include "../lib/response.h"
|
2015-09-25 00:45:13 +02:00
|
|
|
|
2015-09-28 17:05:05 +02:00
|
|
|
#include "mainwindow.h"
|
2015-09-25 13:11:36 +02:00
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
// Info about doorlock-client version
|
|
|
|
const static std::string version =
|
|
|
|
"doorlock-client-" DOORLOCK_VERSION;
|
|
|
|
const static std::string gitversion =
|
|
|
|
DOORLOCK_GIT_BRANCH "-" DOORLOCK_GIT_COMMIT_HASH;
|
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
static Logger &l = Logger::get();
|
2015-09-25 00:45:13 +02:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
2015-09-30 15:21:41 +02:00
|
|
|
namespace ba = boost::asio;
|
|
|
|
using ba::ip::tcp;
|
2015-09-25 00:45:13 +02:00
|
|
|
|
2015-10-01 22:57:01 +02:00
|
|
|
static ba::io_service io_service;
|
2015-09-25 00:45:13 +02:00
|
|
|
|
|
|
|
const static std::string subscriptionCommand =
|
|
|
|
"{ \"command\": \"subscribe\"}";
|
|
|
|
|
|
|
|
// The receive buffer length of the TCP socket
|
2015-10-01 22:57:01 +02:00
|
|
|
constexpr static int SOCKET_BUFFERLENGTH = 2048;
|
2015-09-25 00:45:13 +02:00
|
|
|
|
2015-09-25 13:11:36 +02:00
|
|
|
static volatile bool run = true;
|
|
|
|
|
2015-10-01 22:57:01 +02:00
|
|
|
static std::unique_ptr<MainWindow> mainWindow = nullptr;
|
2015-09-25 13:11:36 +02:00
|
|
|
|
2015-09-29 02:13:48 +02:00
|
|
|
static void onDoorlockUpdate(const Clientmessage &msg)
|
2015-09-24 21:22:51 +02:00
|
|
|
{
|
2015-09-29 14:55:20 +02:00
|
|
|
const auto& doormessage = msg.doormessage();
|
|
|
|
l("Received message", LogLevel::info);
|
2015-10-01 18:02:25 +02:00
|
|
|
l((std::string)" token: " + msg.token(),
|
2015-09-29 14:55:20 +02:00
|
|
|
LogLevel::info);
|
2015-10-05 23:30:25 +02:00
|
|
|
l((std::string)" open: " + std::to_string(msg.isOpen()),
|
2015-09-29 14:55:20 +02:00
|
|
|
LogLevel::info);
|
|
|
|
l((std::string)" button lock: " + std::to_string(doormessage.isLockButton),
|
|
|
|
LogLevel::info);
|
|
|
|
l((std::string)" button unlock: " + std::to_string(doormessage.isUnlockButton),
|
|
|
|
LogLevel::info);
|
|
|
|
l((std::string)" emergency open: " + std::to_string(doormessage.isEmergencyUnlock),
|
|
|
|
LogLevel::info);
|
2015-09-28 17:05:05 +02:00
|
|
|
if (mainWindow) {
|
2015-10-02 19:39:47 +02:00
|
|
|
mainWindow->setClientmessage(msg);
|
2015-09-25 13:11:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
static int doorlock_client(const std::string &hostname,
|
|
|
|
const unsigned short port)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tcp::resolver resolver(io_service);
|
|
|
|
tcp::socket socket(io_service);
|
|
|
|
tcp::resolver::query query(hostname, std::to_string(port));
|
|
|
|
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
|
|
|
|
tcp::resolver::iterator end;
|
2015-09-30 15:21:41 +02:00
|
|
|
boost::system::error_code error = ba::error::host_not_found;
|
2015-09-25 00:45:13 +02:00
|
|
|
std::vector<char> data;
|
|
|
|
|
|
|
|
while (error && endpoint_iterator != end) {
|
|
|
|
socket.close();
|
|
|
|
socket.connect(*endpoint_iterator++, error);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
throw boost::system::system_error(error);
|
|
|
|
|
|
|
|
// After connection is established, send the subscription command
|
2015-09-30 15:21:41 +02:00
|
|
|
socket.write_some(ba::buffer(subscriptionCommand), error);
|
2015-09-25 00:45:13 +02:00
|
|
|
if (error)
|
|
|
|
throw boost::system::system_error(error);
|
|
|
|
|
|
|
|
data.resize(SOCKET_BUFFERLENGTH);
|
|
|
|
|
2015-09-30 15:21:41 +02:00
|
|
|
std::function<void(void)> receiveMessage = [&] () {
|
|
|
|
socket.async_read_some(ba::buffer(data),
|
|
|
|
[&] (const boost::system::error_code &ec,
|
|
|
|
const size_t length)
|
|
|
|
{
|
|
|
|
if (ec) {
|
|
|
|
throw boost::system::system_error(ec);
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:09:55 +02:00
|
|
|
const auto message = Clientmessage::fromString(
|
|
|
|
std::string(data.begin(), data.begin()+length));
|
2015-09-30 15:21:41 +02:00
|
|
|
onDoorlockUpdate(message);
|
|
|
|
|
|
|
|
receiveMessage();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
receiveMessage();
|
|
|
|
io_service.run();
|
2015-09-25 00:45:13 +02:00
|
|
|
}
|
2015-10-01 22:09:55 +02:00
|
|
|
catch(const Response &err) {
|
|
|
|
l(err.message, LogLevel::error);
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
catch(const std::exception &err) {
|
|
|
|
l(LogLevel::error, err.what());
|
|
|
|
retval = -1;
|
2015-09-25 00:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:31:10 +02:00
|
|
|
io_service.reset();
|
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
std::string hostname;
|
|
|
|
unsigned short port;
|
|
|
|
|
|
|
|
l((std::string)"Hello, this is " + version + " built on " + gitversion,
|
|
|
|
LogLevel::info);
|
|
|
|
|
|
|
|
try {
|
|
|
|
po::options_description desc("doorlockd (" + version + " built on " + gitversion + ")");
|
|
|
|
desc.add_options()
|
|
|
|
("help,h",
|
|
|
|
"print help")
|
|
|
|
("port,p",
|
|
|
|
po::value<unsigned short>(&port)->default_value(DEFAULT_PORT),
|
|
|
|
"Port")
|
|
|
|
("host,c",
|
|
|
|
po::value<std::string>(&hostname)->default_value("localhost"),
|
|
|
|
"IP or name of host running doorlockd");
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
|
|
|
|
|
|
|
|
if (vm.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl;
|
2015-09-29 02:13:48 +02:00
|
|
|
exit(-1);
|
2015-09-25 00:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
po::notify(vm);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
l(LogLevel::error, e.what());
|
2015-09-29 02:13:48 +02:00
|
|
|
exit(-1);
|
2015-09-25 00:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l(LogLevel::notice, "Starting doorlock-client");
|
2015-09-25 13:11:36 +02:00
|
|
|
|
2015-09-29 02:13:48 +02:00
|
|
|
QApplication app(argc, argv);
|
|
|
|
app.setOrganizationName("Binary Kitchen");
|
|
|
|
app.setApplicationName("doorlock-client");
|
|
|
|
|
2015-10-11 19:33:19 +02:00
|
|
|
ao_initialize();
|
|
|
|
|
|
|
|
try {
|
|
|
|
mainWindow = std::unique_ptr<MainWindow>(new MainWindow);
|
|
|
|
mainWindow->showFullScreen();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
l(LogLevel::error, e.what());
|
|
|
|
exit(-1);
|
|
|
|
}
|
2015-09-29 02:13:48 +02:00
|
|
|
|
|
|
|
// Start the TCP client as thread
|
|
|
|
std::thread clientThread = std::thread([&] () {
|
|
|
|
// If the TCP client returns, an error has occured
|
|
|
|
// In normal operation, it never returns
|
2015-10-01 22:31:10 +02:00
|
|
|
while (run) {
|
|
|
|
doorlock_client(hostname, port);
|
2015-10-01 22:57:01 +02:00
|
|
|
if (run) {
|
|
|
|
l(LogLevel::error, "client aborted, retrying in 5 seconds");
|
|
|
|
// Todo: Write message to QT frontend
|
|
|
|
|
|
|
|
sleep(5);
|
|
|
|
}
|
2015-10-01 22:31:10 +02:00
|
|
|
}
|
2015-09-29 02:13:48 +02:00
|
|
|
|
2015-09-30 00:44:06 +02:00
|
|
|
// This will stop the Qapplication
|
2015-09-29 15:00:36 +02:00
|
|
|
mainWindow->hide();
|
2015-09-30 00:44:06 +02:00
|
|
|
mainWindow->close();
|
2015-09-29 02:13:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// This routine will never return in normal operation
|
|
|
|
app.exec();
|
|
|
|
|
2015-10-01 22:57:01 +02:00
|
|
|
run = false;
|
|
|
|
|
2015-09-30 15:21:41 +02:00
|
|
|
// Stop the IO service
|
|
|
|
io_service.stop();
|
|
|
|
|
2015-09-29 02:13:48 +02:00
|
|
|
clientThread.join();
|
2015-09-25 00:45:13 +02:00
|
|
|
|
2015-09-30 00:44:06 +02:00
|
|
|
if (mainWindow)
|
|
|
|
mainWindow.reset();
|
|
|
|
|
2015-10-11 19:33:19 +02:00
|
|
|
ao_shutdown();
|
|
|
|
|
2015-09-25 00:45:13 +02:00
|
|
|
l(LogLevel::notice, "Stopping doorlock-client");
|
2015-09-29 02:13:48 +02:00
|
|
|
return 0;
|
2015-09-24 21:22:51 +02:00
|
|
|
}
|