-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
113 lines (94 loc) · 3.18 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <QGuiApplication>
#include <QCursor>
#include <QDebug>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <ctime>
#include "common.h"
#include "sockethelper.h"
#include "socketthread.h"
#include "robotmodel.h"
#include "paramsmodel.h"
#include "process.h"
#include "spdlog/sinks/basic_file_sink.h"
void printUsage() {
cerr << "Usage socket unix : robots-gui unix /tmp/ecran.sock [debug]" << endl;
cerr << "Usage socket inet : robots-gui inet 8686 [debug]" << endl;
}
void qDebugToSpdLog(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context)
switch (type) {
case QtDebugMsg:
spdlog::debug(msg.toStdString());
break;
case QtInfoMsg:
spdlog::info(msg.toStdString());
break;
case QtWarningMsg:
spdlog::warn(msg.toStdString());
break;
case QtCriticalMsg:
case QtFatalMsg:
spdlog::error(msg.toStdString());
break;
}
}
int main(int argc, char *argv[])
{
if (argc < 3) {
printUsage();
return 1;
}
const bool debug = argc >=4 && strcmp(argv[3], "debug") == 0;
// Construit la date d'execution
time_t now = time(nullptr);
tm *ptm = localtime(&now);
char timeBuffer[15];
strftime(timeBuffer, 15, "%Y%m%d%H%M%S", ptm);
const string outputDir = "logs/";
const string outputPrefix = outputDir + string(timeBuffer);
// configuration du logger
//auto file_sink = make_shared<spdlog::sinks::basic_file_sink_mt>(outputPrefix + ".log");
//spdlog::default_logger()->sinks().push_back(file_sink);
//spdlog::flush_every(std::chrono::seconds(1));
spdlog::set_level(debug ? spdlog::level::debug : spdlog::level::info);
qInstallMessageHandler(qDebugToSpdLog);
spdlog::info("Démarrage de l'application");
// Configuration de la Socket
string socketType = argv[1];
string socketConf = argv[2];
SocketHelper socket(socketType);
if (socket.isUnknown()) {
printUsage();
return 2;
} else if (socket.isInet()) {
socket.setPort(atoi(socketConf.c_str()));
} else if (socket.isUnix()) {
socket.setSocketFile(socketConf);
}
SocketThread socketThread;
socketThread.setSocketHelper(&socket);
socketThread.start();
// QML Application
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext* ctx = engine.rootContext();
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) {
QCoreApplication::exit(-1);
}
}, Qt::QueuedConnection);
// Enregistrement du model
ctx->setContextProperty("RobotModel", RobotModel::getInstance());
ctx->setContextProperty("ParamsModel", ParamsModel::getInstance());
// Enregistrement de classe CPP pour QML
qmlRegisterType<Process>( "Process", 1, 0, "Process" );
engine.load(url);
#ifdef RASPI
app.setOverrideCursor(QCursor(Qt::BlankCursor));
#endif
return app.exec();
}