166 lines
4.2 KiB
C++
166 lines
4.2 KiB
C++
#include <QCoreApplication>
|
|
#include <QCommandLineParser>
|
|
#include <QIODevice>
|
|
#include <QTcpSocket>
|
|
#include <iostream>
|
|
#include <QJsonObject>
|
|
#include <QJsonDocument>
|
|
#include <QStandardPaths>
|
|
#include <QFile>
|
|
#include <QTimer>
|
|
#include <signal.h>
|
|
|
|
#include "microcontroller.h"
|
|
#include "nfcuid.h"
|
|
#include "overlorditemstore.h"
|
|
#include "train.h"
|
|
#include "turnout.h"
|
|
#include "layout.h"
|
|
|
|
void sigHandler(int sig)
|
|
{
|
|
QCoreApplication::quit();
|
|
}
|
|
|
|
QJsonObject getJsonObjectFromDisk(const QString& filePath, bool* error)
|
|
{
|
|
QFile file;
|
|
|
|
if(filePath.size() > 0)
|
|
file.setFileName(filePath);
|
|
else
|
|
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/trainoverloard.json");
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
if(!file.isOpen())
|
|
{
|
|
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
|
}
|
|
else
|
|
{
|
|
QJsonParseError qerror;
|
|
QJsonDocument document(QJsonDocument::fromJson(file.readAll(), &qerror));
|
|
file.close();
|
|
if(qerror.error != QJsonParseError::NoError)
|
|
{
|
|
qDebug()<<filePath<<" "<<qerror.errorString();
|
|
if(error)
|
|
(*error) = true;
|
|
}
|
|
return document.object();
|
|
}
|
|
return QJsonObject();
|
|
}
|
|
|
|
bool storeJsonObjectToDisk(const QJsonObject& json, QString filePath = QString())
|
|
{
|
|
if(filePath.size() == 0)
|
|
{
|
|
filePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/trainoverloard.json";
|
|
}
|
|
QFile file(filePath + ".out");
|
|
|
|
qDebug()<<"config file: "<<filePath;
|
|
file.open(QIODevice::WriteOnly);
|
|
if(!file.isOpen())
|
|
{
|
|
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
QJsonDocument document(json);
|
|
file.write(document.toJson());
|
|
file.close();
|
|
QFile::remove(filePath);
|
|
file.rename(filePath);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void getItemsCb(QTimer* timer, ItemStore* items, Microcontroller* micro)
|
|
{
|
|
if(items->getItems()->empty())
|
|
{
|
|
micro->requestState();
|
|
timer->start(1000);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication a(argc, argv);
|
|
|
|
signal(SIGINT, &sigHandler);
|
|
|
|
//set info
|
|
QCoreApplication::setOrganizationName("UVOS");
|
|
QCoreApplication::setOrganizationDomain("uvos.xyz");
|
|
QCoreApplication::setApplicationName("TrainOverlord");
|
|
QCoreApplication::setApplicationVersion("0.1");
|
|
|
|
//parse comand line
|
|
QCommandLineParser parser;
|
|
parser.setApplicationDescription("Train control application");
|
|
parser.addHelpOption();
|
|
parser.addVersionOption();
|
|
QCommandLineOption hostOption(QStringList() << "H" << "host", QCoreApplication::translate("main",
|
|
"Set server host ip addres"), "adress");
|
|
parser.addOption(hostOption);
|
|
QCommandLineOption portOption(QStringList() << "p" << "port", QCoreApplication::translate("main",
|
|
"Set server Port in TCP mode or Serial port in serial mode"), "port");
|
|
parser.addOption(portOption);
|
|
parser.process(a);
|
|
|
|
QTcpSocket microSocket;
|
|
|
|
int port = 6856;
|
|
if(parser.isSet(portOption))
|
|
port = parser.value(portOption).toInt();
|
|
|
|
QString host("127.0.0.1");
|
|
if(parser.isSet(hostOption)) host = parser.value(hostOption);
|
|
std::cout<<"connecting to "<<host.toStdString()<<':'<<port<<'\n';
|
|
microSocket.connectToHost(host, port, QIODevice::ReadWrite);
|
|
if(!microSocket.waitForConnected(3000))
|
|
{
|
|
std::cout<<"Can not connect to to Server.\n";
|
|
return 1;
|
|
}
|
|
|
|
OverlordItemStore items;
|
|
Layout layout(&items);
|
|
|
|
Microcontroller micro(µSocket);
|
|
QObject::connect(µ, &Microcontroller::gotTag, &layout, &Layout::tagArrivedAtReader);
|
|
QObject::connect(µ, &Microcontroller::gotItemList, &items, &OverlordItemStore::addItems);
|
|
QObject::connect(µ, &Microcontroller::itemChanged, &items, &OverlordItemStore::itemStateChanged);
|
|
|
|
Train::micro = µ
|
|
Turnout::micro = µ
|
|
Signal::micro = µ
|
|
|
|
QTimer timer;
|
|
timer.setSingleShot(false);
|
|
QObject::connect(&timer, &QTimer::timeout, &timer, [µ, &timer, &items](){getItemsCb(&timer, &items, µ);});
|
|
getItemsCb(&timer, &items, µ);
|
|
|
|
{
|
|
bool err = false;
|
|
QJsonObject json = getJsonObjectFromDisk(QString(), &err);
|
|
if(err)
|
|
{
|
|
std::cerr<<"Could not load config file\n";
|
|
return -1;
|
|
}
|
|
layout.load(json);
|
|
}
|
|
|
|
int ret = a.exec();
|
|
/*
|
|
QJsonObject jsonStore;
|
|
layout.store(jsonStore);
|
|
storeJsonObjectToDisk(jsonStore);
|
|
*/
|
|
return ret;
|
|
}
|